-
Notifications
You must be signed in to change notification settings - Fork 320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Xtend refactoring handling of anonymous class #2898
Merged
LorenzoBettini
merged 46 commits into
eclipse-xtext:main
from
LorenzoBettini:temp-xtend-anonymous-class-2
Feb 6, 2024
Merged
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
94cbea8
Added an Xtend validator test for cycles in internal types
LorenzoBettini 2494947
also verify the error's position for hierarchy cycles
LorenzoBettini 2d1328e
also verify error's position for INTERFACE_EXPECTED
LorenzoBettini 839182e
also verify error's position for OVERRIDDEN_FINAL
LorenzoBettini b6d2aca
also verify error's position for CLASS_EXPECTED
LorenzoBettini b20135a
also verify error's position for field INVALID_USE_OF_TYPE
LorenzoBettini 461db35
also verify error's position for parameter INVALID_USE_OF_TYPE
LorenzoBettini eb38b9d
fixed verify error's position for parameter INVALID_USE_OF_TYPE
LorenzoBettini 5390514
also verify error's position for DUPLICATE_PARAMETER_NAME
LorenzoBettini 061170c
also verify error's position for duplicates
LorenzoBettini ae07565
also verify error's position for duplicate methods
LorenzoBettini af65508
also verify error's message parts for duplicate methods
LorenzoBettini 665d49c
added missing tests for doCheckOverriddenMethods
LorenzoBettini ab8c3fb
also verify error position for CONFLICTING_DEFAULT_METHODS
LorenzoBettini 5d39e36
also verify error position for CLASS_MUST_BE_ABSTRACT
LorenzoBettini c2789f6
also verify error position for ANONYMOUS_CLASS_MISSING_MEMBERS
LorenzoBettini e424b1a
also verify error position for anonymous class override final class
LorenzoBettini 944efb0
added test with anonymous class not implementing abstract methods
LorenzoBettini b2401fe
some experiments
LorenzoBettini 85a0ea9
AnonymousClassAwareTreeAppendable
LorenzoBettini 63d58e8
removed some customizations
LorenzoBettini d1d795b
simplified appendConstructedTypeName
LorenzoBettini e5b112f
simplified AnonymousClassAwareTreeAppendable
LorenzoBettini ad1b04c
added another createChild to make it easier for subclassing
LorenzoBettini 1d822ef
don't store the converter
LorenzoBettini 92242b1
simplified isVariableDeclarationRequired
LorenzoBettini c4491d4
simplified internalCanCompileToJavaExpression
LorenzoBettini c8b774d
simplified constructorCallToJavaExpression
LorenzoBettini ff120dc
simplified appendConstructedTypeName
LorenzoBettini 34096f7
XtendCompilerUtil -> XtendCompilerHelper
LorenzoBettini d6a1a8b
use IResourceScopeCache in XtendCompilerHelper
LorenzoBettini f87ce5e
simplified appendConstructedTypeName
LorenzoBettini 8f85eb7
canCompileToJavaAnonymousClass
LorenzoBettini f5251c1
check isAnonymousClassConstructorCall
LorenzoBettini e74011b
Xtend uses canCompileToJavaAnonymousClass
LorenzoBettini 26d6c23
simplified isVariableDeclarationRequired
LorenzoBettini 07a2f7e
simplified isVariableDeclarationRequired 2
LorenzoBettini 723d944
isVariableDeclarationRequired for constructor in Xbase
LorenzoBettini 74faea2
removed duplication by constructorCallToJavaExpression
LorenzoBettini 15b244d
reduced visibility of some methods
LorenzoBettini 4f44872
Merge branch 'lb_xbase_cleanup-1' into temp-xtend-anonymous-class-2
LorenzoBettini eb6665b
Added @since to JvmModelGenerator's new method
LorenzoBettini bc766b0
Merge branch 'main' into temp-xtend-anonymous-class-2
LorenzoBettini f8d46fd
XtendCompilerHelper -> AnonymousClassCompilerHelper
LorenzoBettini 5d926c2
the added createAppendable is protected
LorenzoBettini 6af742c
simplified check in generateVisibilityModifier
LorenzoBettini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yet this code here doesn't do both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least in the current implementation anonymous classes are local classes