Skip to content
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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
}

}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,8 @@ class JvmFieldDeclarationImpl extends JvmMemberDeclarationImpl<JvmField> impleme
}

override remove() {
compilationUnit.jvmModelAssociator.removeAllAssociation(delegate.type)
Copy link
Contributor

@szarnekow szarnekow Aug 10, 2023

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?

Copy link
Contributor Author

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

Copy link
Contributor

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.

compilationUnit.jvmModelAssociator.removeAllAssociation(delegate)
compilationUnit.jvmModelAssociator.removeLogicalChildAssociation(delegate)
super.remove()
}
Expand Down
Loading