-
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
[#2318] Clean AllAssociation when removing field with active annotation #2776
Open
dvojtise
wants to merge
2
commits into
eclipse-xtext:main
Choose a base branch
from
dvojtise:2318-remove-field
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
94 changes: 94 additions & 0 deletions
94
...tend.core.tests/src/org/eclipse/xtend/core/tests/macro/AnnotationPropertyRemoveTest.xtend
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,94 @@ | ||
/******************************************************************************* | ||
* 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.tests.macro | ||
|
||
import java.io.ByteArrayOutputStream | ||
import java.lang.annotation.Target | ||
import java.util.List | ||
import org.eclipse.emf.ecore.EObject | ||
import org.eclipse.xtend.lib.macro.AbstractClassProcessor | ||
import org.eclipse.xtend.lib.macro.Active | ||
import org.eclipse.xtend.lib.macro.RegisterGlobalsContext | ||
import org.eclipse.xtend.lib.macro.TransformationContext | ||
import org.eclipse.xtend.lib.macro.declaration.ClassDeclaration | ||
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration | ||
import org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration | ||
import org.eclipse.xtext.resource.persistence.StorageAwareResource | ||
import org.eclipse.xtext.xbase.resource.BatchLinkableResourceStorageWritable | ||
import org.junit.Test | ||
|
||
/** | ||
* @author Didier Vojtisek - Initial contribution and API | ||
*/ | ||
class AnnotationPropertyRemoveTest extends AbstractActiveAnnotationTest { | ||
|
||
@Test def void testAnnotationPropertyRemove_01() { | ||
''' | ||
@org.eclipse.xtend.core.tests.macro.RemoveUnderscoreAnnotation() | ||
class TestClass { | ||
public String foo | ||
|
||
public String _ba | ||
} | ||
'''.assertCompilesTo(''' | ||
import org.eclipse.xtend.core.tests.macro.RemoveUnderscoreAnnotation; | ||
|
||
@RemoveUnderscoreAnnotation | ||
@SuppressWarnings("all") | ||
public class TestClass { | ||
public String foo; | ||
} | ||
''') | ||
} | ||
|
||
@Test def void testresourceStorageWrite() { | ||
val contents = ''' | ||
@org.eclipse.xtend.core.tests.macro.RemoveUnderscoreAnnotation() | ||
class AnnotationPropertyRemoveTestClass { | ||
public String foo | ||
|
||
public String _ba | ||
} | ||
''' | ||
|
||
val file = file(contents) | ||
new BatchLinkableResourceStorageWritable(new ByteArrayOutputStream, false) { | ||
override String getFragment(EObject obj) { | ||
assertTrue(obj.toString + " is not contained in any resource ", obj.eResource() !== null) | ||
return super.getFragment(obj) | ||
} | ||
}.writeResource(file.eResource as StorageAwareResource) | ||
|
||
} | ||
} | ||
|
||
@Active(RemoveUnderscoreAnnotationProcessor) | ||
@Target(TYPE) | ||
annotation RemoveUnderscoreAnnotation { | ||
} | ||
|
||
class RemoveUnderscoreAnnotationProcessor extends AbstractClassProcessor { | ||
|
||
override doRegisterGlobals(ClassDeclaration annotatedClass, extension RegisterGlobalsContext context) { | ||
super.doRegisterGlobals(annotatedClass, context) | ||
} | ||
|
||
override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) { | ||
super.doTransform(annotatedClass, context) | ||
val List<MutableFieldDeclaration> toRemove = newArrayList | ||
// remove fields starting with "_" | ||
for (f : annotatedClass.declaredFields) { | ||
if (f.simpleName.startsWith("_")) { | ||
toRemove.add(f) | ||
} | ||
} | ||
toRemove.forEach[f|f.remove] | ||
} | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
...core.tests/xtend-gen/org/eclipse/xtend/core/tests/macro/AnnotationPropertyRemoveTest.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,99 @@ | ||
/** | ||
* 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.tests.macro; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.resource.Resource; | ||
import org.eclipse.xtend.core.xtend.XtendFile; | ||
import org.eclipse.xtend2.lib.StringConcatenation; | ||
import org.eclipse.xtext.resource.persistence.StorageAwareResource; | ||
import org.eclipse.xtext.xbase.lib.Exceptions; | ||
import org.eclipse.xtext.xbase.resource.BatchLinkableResourceStorageWritable; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author Didier Vojtisek - Initial contribution and API | ||
*/ | ||
@SuppressWarnings("all") | ||
public class AnnotationPropertyRemoveTest extends AbstractActiveAnnotationTest { | ||
@Test | ||
public void testAnnotationPropertyRemove_01() { | ||
StringConcatenation _builder = new StringConcatenation(); | ||
_builder.append("@org.eclipse.xtend.core.tests.macro.RemoveUnderscoreAnnotation()"); | ||
_builder.newLine(); | ||
_builder.append("class TestClass {"); | ||
_builder.newLine(); | ||
_builder.append("\t"); | ||
_builder.append("public String foo"); | ||
_builder.newLine(); | ||
_builder.append("\t"); | ||
_builder.newLine(); | ||
_builder.append("\t"); | ||
_builder.append("public String _ba "); | ||
_builder.newLine(); | ||
_builder.append("}"); | ||
_builder.newLine(); | ||
StringConcatenation _builder_1 = new StringConcatenation(); | ||
_builder_1.append("import org.eclipse.xtend.core.tests.macro.RemoveUnderscoreAnnotation;"); | ||
_builder_1.newLine(); | ||
_builder_1.newLine(); | ||
_builder_1.append("@RemoveUnderscoreAnnotation"); | ||
_builder_1.newLine(); | ||
_builder_1.append("@SuppressWarnings(\"all\")"); | ||
_builder_1.newLine(); | ||
_builder_1.append("public class TestClass {"); | ||
_builder_1.newLine(); | ||
_builder_1.append(" "); | ||
_builder_1.append("public String foo;"); | ||
_builder_1.newLine(); | ||
_builder_1.append("}"); | ||
_builder_1.newLine(); | ||
this._xtendCompilerTester.assertCompilesTo(_builder, _builder_1); | ||
} | ||
|
||
@Test | ||
public void testresourceStorageWrite() { | ||
try { | ||
StringConcatenation _builder = new StringConcatenation(); | ||
_builder.append("@org.eclipse.xtend.core.tests.macro.RemoveUnderscoreAnnotation()"); | ||
_builder.newLine(); | ||
_builder.append("class AnnotationPropertyRemoveTestClass {"); | ||
_builder.newLine(); | ||
_builder.append("\t"); | ||
_builder.append("public String foo"); | ||
_builder.newLine(); | ||
_builder.append("\t"); | ||
_builder.newLine(); | ||
_builder.append("\t"); | ||
_builder.append("public String _ba "); | ||
_builder.newLine(); | ||
_builder.append("}"); | ||
_builder.newLine(); | ||
final String contents = _builder.toString(); | ||
final XtendFile file = this.file(contents); | ||
ByteArrayOutputStream _byteArrayOutputStream = new ByteArrayOutputStream(); | ||
Resource _eResource = file.eResource(); | ||
new BatchLinkableResourceStorageWritable(_byteArrayOutputStream, false) { | ||
@Override | ||
public String getFragment(final EObject obj) { | ||
String _string = obj.toString(); | ||
String _plus = (_string + " is not contained in any resource "); | ||
Resource _eResource = obj.eResource(); | ||
boolean _tripleNotEquals = (_eResource != null); | ||
Assert.assertTrue(_plus, _tripleNotEquals); | ||
return super.getFragment(obj); | ||
} | ||
}.writeResource(((StorageAwareResource) _eResource)); | ||
} catch (Throwable _e) { | ||
throw Exceptions.sneakyThrow(_e); | ||
} | ||
} | ||
} |
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
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.
There might be other
removevAllAssociations(..)
missing in this file, no?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.
yes probably on other kind of remove
but since it make silently crash other remove (see the new failed test. after the first remove. the other ones aren't processed)
I'm not sure anymore that it should be done that way
I must admit I don't master xtend internal structures and I'm not efficient in finding a solution for cleaning these structures on all remove situations
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.
I'll give it a stab later this week.