-
Notifications
You must be signed in to change notification settings - Fork 320
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2898 from LorenzoBettini/temp-xtend-anonymous-cla…
…ss-2 Xtend refactoring handling of anonymous class
- Loading branch information
Showing
14 changed files
with
215 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
org.eclipse.xtend.core/src/org/eclipse/xtend/core/compiler/AnonymousClassCompilerHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 itemis AG (http://www.itemis.eu) and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
package org.eclipse.xtend.core.compiler; | ||
|
||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.xtend.core.xtend.AnonymousClass; | ||
import org.eclipse.xtend.core.xtend.XtendField; | ||
import org.eclipse.xtend.core.xtend.XtendFunction; | ||
import org.eclipse.xtend.core.xtend.XtendMember; | ||
import org.eclipse.xtext.common.types.JvmType; | ||
import org.eclipse.xtext.util.IResourceScopeCache; | ||
import org.eclipse.xtext.util.Tuples; | ||
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations; | ||
|
||
import com.google.inject.Inject; | ||
|
||
/** | ||
* @author Lorenzo Bettini - Initial contribution and API | ||
*/ | ||
public class AnonymousClassCompilerHelper { | ||
|
||
@Inject | ||
private IResourceScopeCache cache; | ||
|
||
@Inject | ||
private IJvmModelAssociations associations; | ||
|
||
/** | ||
* Assumes that the passed type is anonymous. | ||
*/ | ||
public boolean canCompileToJavaAnonymousClass(JvmType type) { | ||
return cache.get(Tuples.pair("anonymousJava", type), type.eResource(), () -> { | ||
EObject sourceElement = associations.getPrimarySourceElement(type); | ||
return sourceElement instanceof AnonymousClass && | ||
canCompileToJavaAnonymousClass((AnonymousClass) sourceElement); | ||
}); | ||
} | ||
|
||
public boolean canCompileToJavaAnonymousClass(AnonymousClass anonymousClass) { | ||
return cache.get(Tuples.pair("anonymousJava", anonymousClass), anonymousClass.eResource(), () -> { | ||
for(XtendMember member: anonymousClass.getMembers()) { | ||
if(member instanceof XtendField || | ||
(member instanceof XtendFunction && !((XtendFunction) member).isOverride())) | ||
return false; | ||
} | ||
return true; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...nd.core/src/org/eclipse/xtend/core/compiler/output/AnonymousClassAwareTreeAppendable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 itemis AG (http://www.itemis.eu) and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
package org.eclipse.xtend.core.compiler.output; | ||
|
||
import java.util.Set; | ||
|
||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.xtend.core.compiler.AnonymousClassCompilerHelper; | ||
import org.eclipse.xtext.generator.trace.ILocationData; | ||
import org.eclipse.xtext.generator.trace.ITraceURIConverter; | ||
import org.eclipse.xtext.resource.ILocationInFileProvider; | ||
import org.eclipse.xtext.xbase.compiler.ImportManager; | ||
import org.eclipse.xtext.xbase.compiler.output.SharedAppendableState; | ||
import org.eclipse.xtext.xbase.compiler.output.TreeAppendable; | ||
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations; | ||
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReferenceSerializer; | ||
import org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference; | ||
|
||
/** | ||
* A custom implementation that takes into consideration anonymous classes, | ||
* which, in some cases, cannot be compiled into standard Java anonymous classes: | ||
* they are compiled into nested local classes. | ||
* | ||
* It uses a custom {@link LightweightTypeReferenceSerializer}. | ||
* | ||
* @author Lorenzo Bettini - Initial contribution and API | ||
*/ | ||
public class AnonymousClassAwareTreeAppendable extends TreeAppendable { | ||
|
||
private AnonymousClassCompilerHelper compilerHelper; | ||
|
||
public AnonymousClassAwareTreeAppendable(AnonymousClassCompilerHelper compilerHelper, ImportManager importManager, ITraceURIConverter converter, | ||
ILocationInFileProvider locationProvider, IJvmModelAssociations jvmModelAssociations, EObject source, String indentation, | ||
String lineSeparator) { | ||
super(importManager, converter, locationProvider, jvmModelAssociations, source, indentation, lineSeparator); | ||
this.compilerHelper = compilerHelper; | ||
} | ||
|
||
protected AnonymousClassAwareTreeAppendable(AnonymousClassCompilerHelper compilerHelper, SharedAppendableState state, ITraceURIConverter converter, | ||
ILocationInFileProvider locationProvider, IJvmModelAssociations jvmModelAssociations, Set<ILocationData> sourceLocations, | ||
boolean useForDebugging) { | ||
super(state, converter, locationProvider, jvmModelAssociations, sourceLocations, useForDebugging); | ||
this.compilerHelper = compilerHelper; | ||
} | ||
|
||
@Override | ||
protected LightweightTypeReferenceSerializer createLightweightTypeReferenceSerializer() { | ||
return new LightweightTypeReferenceSerializer(this) { | ||
@Override | ||
protected void doVisitParameterizedTypeReference(ParameterizedTypeReference reference) { | ||
if (reference.isAnonymous() && | ||
!compilerHelper.canCompileToJavaAnonymousClass(reference.getType())) { | ||
// serialize the type of the generated nested local class | ||
AnonymousClassAwareTreeAppendable.this.append(reference.getType()); | ||
} else { | ||
super.doVisitParameterizedTypeReference(reference); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
protected TreeAppendable createChild(SharedAppendableState state, ITraceURIConverter converter, ILocationInFileProvider locationProvider, | ||
IJvmModelAssociations jvmModelAssociations, Set<ILocationData> newData, boolean useForDebugging) { | ||
return new AnonymousClassAwareTreeAppendable(compilerHelper, state, converter, locationProvider, jvmModelAssociations, newData, useForDebugging); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.