use of org.eclipse.xtend.lib.macro.declaration.MethodDeclaration in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testRemove.
@Test
public void testRemove() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import com.google.common.base.Preconditions");
_builder.newLine();
_builder.append("import java.util.List");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.Active");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableNamedElement");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(MyAnnotationProcessor)");
_builder.newLine();
_builder.append("annotation MyAnnotation {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class MyAnnotationProcessor implements TransformationParticipant<MutableNamedElement> {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableNamedElement> annotatedTargetElements,");
_builder.newLine();
_builder.append("\t\t");
_builder.append("extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (annotatedTargetElement : annotatedTargetElements) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("remove(annotatedTargetElement, context)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("def dispatch void remove(MutableNamedElement annotatedTargetElement, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val sourceElement = annotatedTargetElement.primarySourceElement");
_builder.newLine();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(sourceElement != null,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'a source element should not be null before removing, but: ");
_builder.append("�", "\t\t\t");
_builder.append("sourceElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(sourceElement.primaryGeneratedJavaElement == annotatedTargetElement,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'expected: ");
_builder.append("�", "\t\t\t");
_builder.append("annotatedTargetElement");
_builder.append("�", "\t\t\t");
_builder.append(", but: ");
_builder.append("�", "\t\t\t");
_builder.append("sourceElement.primaryGeneratedJavaElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedTargetElement.remove");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(annotatedTargetElement.primarySourceElement == null,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'a source element should be null after removing, but: ");
_builder.append("�", "\t\t\t");
_builder.append("annotatedTargetElement.primarySourceElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(sourceElement.primaryGeneratedJavaElement == null,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'a target element should be null after removing, but: ");
_builder.append("�", "\t\t\t");
_builder.append("sourceElement.primaryGeneratedJavaElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("annotatedTargetElement.remove");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(false, \'");
_builder.append("\'\'");
_builder.append("�", "\t\t\t");
_builder.append("IllegalArgumentException");
_builder.append("�", "\t\t\t");
_builder.append(" is expected\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("} catch (IllegalStateException e) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(e.message.startsWith(\"This element has already been removed: \"),");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("\'");
_builder.append("\'\'Wrong error message: ");
_builder.append("�", "\t\t\t\t");
_builder.append("e.message");
_builder.append("�", "\t\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("def dispatch void remove(MutableTypeDeclaration annotatedTargetElement, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("annotatedTargetElement.remove");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(false, \'");
_builder.append("\'\'");
_builder.append("�", "\t\t\t");
_builder.append("UnsupportedOperationException");
_builder.append("�", "\t\t\t");
_builder.append(" is expected\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("} catch (UnsupportedOperationException e) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(e.message == \"The type cannot be removed.\", \'\'");
_builder.append("\'Wrong error message: ");
_builder.append("�", "\t\t\t");
_builder.append("e.message");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import myannotation.MyAnnotation");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("class UserClass {");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("String fieldToRemove");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("new(String arg) {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("new(@MyAnnotation Integer argToRemove) {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def methodToRemove() {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static class NestedClass {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static interface NestedInteface {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static annotation NestedAnnotation {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static enum NestedEnum {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("enum UserEnum {");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("interface UserInterface {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def String methodToRemove();");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("annotation UserAnnotation {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("String fieldToRemove");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.UserClass");
Assert.assertNotNull(clazz);
Assert.assertEquals(5, IterableExtensions.size(clazz.getDeclaredMembers()));
final MutableConstructorDeclaration constructor = IterableExtensions.head(clazz.getDeclaredConstructors());
Assert.assertNotNull(clazz);
Assert.assertEquals(0, IterableExtensions.size(constructor.getParameters()));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<ClassDeclaration>filter(clazz.getDeclaredMembers(), ClassDeclaration.class)));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<InterfaceDeclaration>filter(clazz.getDeclaredMembers(), InterfaceDeclaration.class)));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<EnumerationTypeDeclaration>filter(clazz.getDeclaredMembers(), EnumerationTypeDeclaration.class)));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<AnnotationTypeDeclaration>filter(clazz.getDeclaredMembers(), AnnotationTypeDeclaration.class)));
final MutableEnumerationTypeDeclaration enum_ = it.getTypeLookup().findEnumerationType("myusercode.UserEnum");
Assert.assertNotNull(enum_);
final MutableInterfaceDeclaration interface_ = it.getTypeLookup().findInterface("myusercode.UserInterface");
Assert.assertNotNull(interface_);
Assert.assertEquals(0, IterableExtensions.size(interface_.getDeclaredMembers()));
final MutableAnnotationTypeDeclaration annotation = it.getTypeLookup().findAnnotationType("myusercode.UserAnnotation");
Assert.assertNotNull(annotation);
Assert.assertEquals(0, IterableExtensions.size(annotation.getDeclaredMembers()));
Element _primarySourceElement = it.getTracability().getPrimarySourceElement(clazz);
final MethodDeclaration removedMethod = ((ClassDeclaration) _primarySourceElement).findDeclaredMethod("methodToRemove");
Expression _body = removedMethod.getBody();
final XExpression expression = ((ExpressionImpl) _body).getDelegate();
Assert.assertNull(this.logicalContainerProvider.getLogicalContainer(expression));
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
use of org.eclipse.xtend.lib.macro.declaration.MethodDeclaration in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testRemove.
@Test
public void testRemove() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import com.google.common.base.Preconditions");
_builder.newLine();
_builder.append("import java.util.List");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.Active");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableNamedElement");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(MyAnnotationProcessor)");
_builder.newLine();
_builder.append("annotation MyAnnotation {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class MyAnnotationProcessor implements TransformationParticipant<MutableNamedElement> {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableNamedElement> annotatedTargetElements,");
_builder.newLine();
_builder.append("\t\t");
_builder.append("extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (annotatedTargetElement : annotatedTargetElements) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("remove(annotatedTargetElement, context)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("def dispatch void remove(MutableNamedElement annotatedTargetElement, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val sourceElement = annotatedTargetElement.primarySourceElement");
_builder.newLine();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(sourceElement != null,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'a source element should not be null before removing, but: ");
_builder.append("�", "\t\t\t");
_builder.append("sourceElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(sourceElement.primaryGeneratedJavaElement == annotatedTargetElement,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'expected: ");
_builder.append("�", "\t\t\t");
_builder.append("annotatedTargetElement");
_builder.append("�", "\t\t\t");
_builder.append(", but: ");
_builder.append("�", "\t\t\t");
_builder.append("sourceElement.primaryGeneratedJavaElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedTargetElement.remove");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(annotatedTargetElement.primarySourceElement == null,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'a source element should be null after removing, but: ");
_builder.append("�", "\t\t\t");
_builder.append("annotatedTargetElement.primarySourceElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("Preconditions.checkState(sourceElement.primaryGeneratedJavaElement == null,");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("\'");
_builder.append("\'\'a target element should be null after removing, but: ");
_builder.append("�", "\t\t\t");
_builder.append("sourceElement.primaryGeneratedJavaElement");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("annotatedTargetElement.remove");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(false, \'");
_builder.append("\'\'");
_builder.append("�", "\t\t\t");
_builder.append("IllegalArgumentException");
_builder.append("�", "\t\t\t");
_builder.append(" is expected\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("} catch (IllegalStateException e) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(e.message.startsWith(\"This element has already been removed: \"),");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("\'");
_builder.append("\'\'Wrong error message: ");
_builder.append("�", "\t\t\t\t");
_builder.append("e.message");
_builder.append("�", "\t\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("def dispatch void remove(MutableTypeDeclaration annotatedTargetElement, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("annotatedTargetElement.remove");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(false, \'");
_builder.append("\'\'");
_builder.append("�", "\t\t\t");
_builder.append("UnsupportedOperationException");
_builder.append("�", "\t\t\t");
_builder.append(" is expected\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("} catch (UnsupportedOperationException e) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("Preconditions.checkState(e.message == \"The type cannot be removed.\", \'\'");
_builder.append("\'Wrong error message: ");
_builder.append("�", "\t\t\t");
_builder.append("e.message");
_builder.append("�", "\t\t\t");
_builder.append("\'");
_builder.append("\'\')");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import myannotation.MyAnnotation");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("class UserClass {");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("String fieldToRemove");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("new(String arg) {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("new(@MyAnnotation Integer argToRemove) {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def methodToRemove() {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static class NestedClass {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static interface NestedInteface {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static annotation NestedAnnotation {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("static enum NestedEnum {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("enum UserEnum {");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("interface UserInterface {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def String methodToRemove();");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("annotation UserAnnotation {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("String fieldToRemove");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.UserClass");
Assert.assertNotNull(clazz);
Assert.assertEquals(5, IterableExtensions.size(clazz.getDeclaredMembers()));
final MutableConstructorDeclaration constructor = IterableExtensions.head(clazz.getDeclaredConstructors());
Assert.assertNotNull(clazz);
Assert.assertEquals(0, IterableExtensions.size(constructor.getParameters()));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<ClassDeclaration>filter(clazz.getDeclaredMembers(), ClassDeclaration.class)));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<InterfaceDeclaration>filter(clazz.getDeclaredMembers(), InterfaceDeclaration.class)));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<EnumerationTypeDeclaration>filter(clazz.getDeclaredMembers(), EnumerationTypeDeclaration.class)));
Assert.assertEquals(1, IterableExtensions.size(Iterables.<AnnotationTypeDeclaration>filter(clazz.getDeclaredMembers(), AnnotationTypeDeclaration.class)));
final MutableEnumerationTypeDeclaration enum_ = it.getTypeLookup().findEnumerationType("myusercode.UserEnum");
Assert.assertNotNull(enum_);
final MutableInterfaceDeclaration interface_ = it.getTypeLookup().findInterface("myusercode.UserInterface");
Assert.assertNotNull(interface_);
Assert.assertEquals(0, IterableExtensions.size(interface_.getDeclaredMembers()));
final MutableAnnotationTypeDeclaration annotation = it.getTypeLookup().findAnnotationType("myusercode.UserAnnotation");
Assert.assertNotNull(annotation);
Assert.assertEquals(0, IterableExtensions.size(annotation.getDeclaredMembers()));
Element _primarySourceElement = it.getTracability().getPrimarySourceElement(clazz);
final MethodDeclaration removedMethod = ((ClassDeclaration) _primarySourceElement).findDeclaredMethod("methodToRemove");
Expression _body = removedMethod.getBody();
final XExpression expression = ((ExpressionImpl) _body).getDelegate();
Assert.assertNull(this.logicalContainerProvider.getLogicalContainer(expression));
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
use of org.eclipse.xtend.lib.macro.declaration.MethodDeclaration in project xtext-xtend by eclipse.
the class DeclarationsTest method testXtendClassWithMethodFieldAndConstructor.
@Test
public void testXtendClassWithMethodFieldAndConstructor() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package foo");
_builder.newLine();
_builder.newLine();
_builder.append("class MyClass<T extends CharSequence> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("String myField");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("new(String initial) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("this.myField = initial");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("def <T2 extends CharSequence> MyClass myMethod(T2 a, T b) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("myField = myField + a + b");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return this");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
Assert.assertEquals("foo", it.getPackageName());
TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
final ClassDeclaration clazz = ((ClassDeclaration) _head);
final MutableClassDeclaration genClazz = it.getTypeLookup().findClass("foo.MyClass");
Assert.assertEquals("foo.MyClass", clazz.getQualifiedName());
Assert.assertNull(clazz.getExtendedClass());
Assert.assertTrue(IterableExtensions.isEmpty(clazz.getImplementedInterfaces()));
Assert.assertEquals(3, IterableExtensions.size(clazz.getDeclaredMembers()));
Assert.assertEquals("T", IterableExtensions.head(clazz.getTypeParameters()).getSimpleName());
Assert.assertEquals("CharSequence", IterableExtensions.head(IterableExtensions.head(clazz.getTypeParameters()).getUpperBounds()).toString());
Assert.assertSame(clazz, IterableExtensions.head(clazz.getTypeParameters()).getTypeParameterDeclarator());
final FieldDeclaration field = IterableExtensions.head(clazz.getDeclaredFields());
Assert.assertSame(clazz, field.getDeclaringType());
Assert.assertEquals("myField", field.getSimpleName());
Assert.assertEquals("String", field.getType().toString());
Assert.assertFalse(field.isFinal());
final ConstructorDeclaration constructor = IterableExtensions.head(clazz.getDeclaredConstructors());
Assert.assertSame(clazz, constructor.getDeclaringType());
Assert.assertEquals("MyClass", constructor.getSimpleName());
Assert.assertEquals("initial", IterableExtensions.head(constructor.getParameters()).getSimpleName());
Assert.assertEquals("String", IterableExtensions.head(constructor.getParameters()).getType().toString());
final MethodDeclaration method = IterableExtensions.head(clazz.getDeclaredMethods());
final MutableMethodDeclaration genMethod = IterableExtensions.head(genClazz.getDeclaredMethods());
Assert.assertSame(clazz, method.getDeclaringType());
Assert.assertEquals("a", IterableExtensions.head(method.getParameters()).getSimpleName());
Assert.assertEquals("T2", IterableExtensions.head(method.getParameters()).getType().toString());
Assert.assertSame(IterableExtensions.head(genMethod.getTypeParameters()), IterableExtensions.head(method.getParameters()).getType().getType());
Assert.assertEquals("T", ((ParameterDeclaration[]) Conversions.unwrapArray(method.getParameters(), ParameterDeclaration.class))[1].getType().toString());
Assert.assertSame(IterableExtensions.head(genClazz.getTypeParameters()), ((ParameterDeclaration[]) Conversions.unwrapArray(method.getParameters(), ParameterDeclaration.class))[1].getType().getType());
Assert.assertSame(genClazz, method.getReturnType().getType());
Assert.assertEquals("T2", IterableExtensions.head(method.getTypeParameters()).getSimpleName());
Assert.assertEquals("CharSequence", IterableExtensions.head(IterableExtensions.head(method.getTypeParameters()).getUpperBounds()).toString());
Assert.assertSame(IterableExtensions.head(method.getTypeParameters()), IterableExtensions.head(method.getTypeParameters()));
Assert.assertSame(method, IterableExtensions.head(method.getTypeParameters()).getTypeParameterDeclarator());
Assert.assertSame(field, ((Object[]) Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[0]);
Assert.assertSame(constructor, ((Object[]) Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[1]);
Assert.assertSame(method, ((Object[]) Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[2]);
};
this.asCompilationUnit(this.validFile(_builder), _function);
}
use of org.eclipse.xtend.lib.macro.declaration.MethodDeclaration in project xtext-core by eclipse.
the class TracedAccessorsProcessor method doTransform.
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
annotatedClass.setExtendedClass(context.newTypeReference(TracingSugar.class));
final TypeReference iterableType = context.newTypeReference(Iterable.class, context.newWildcardTypeReference());
final TypeReference annotationType = context.newTypeReference(TracedAccessors.class);
AnnotationReference _findAnnotation = annotatedClass.findAnnotation(annotationType.getType());
TypeReference[] _classArrayValue = null;
if (_findAnnotation != null) {
_classArrayValue = _findAnnotation.getClassArrayValue("value");
}
final TypeReference[] factories = _classArrayValue;
if ((factories == null)) {
return;
}
final Function1<TypeReference, Type> _function = (TypeReference it) -> {
return it.getType();
};
Iterable<InterfaceDeclaration> _filter = Iterables.<InterfaceDeclaration>filter(ListExtensions.<TypeReference, Type>map(((List<TypeReference>) Conversions.doWrapArray(factories)), _function), InterfaceDeclaration.class);
for (final InterfaceDeclaration f : _filter) {
final Function1<MethodDeclaration, Boolean> _function_1 = (MethodDeclaration it) -> {
return Boolean.valueOf((it.getSimpleName().startsWith("create") && IterableExtensions.isEmpty(it.getParameters())));
};
final Function1<MethodDeclaration, TypeReference> _function_2 = (MethodDeclaration it) -> {
return it.getReturnType();
};
Iterable<TypeReference> _map = IterableExtensions.map(IterableExtensions.filter(f.getDeclaredMethods(), _function_1), _function_2);
for (final TypeReference t : _map) {
final Function1<ResolvedMethod, Boolean> _function_3 = (ResolvedMethod it) -> {
return Boolean.valueOf(this.isSupportedGetter(it));
};
final Function1<ResolvedMethod, Boolean> _function_4 = (ResolvedMethod it) -> {
boolean _isAssignableFrom = iterableType.isAssignableFrom(it.getDeclaration().getReturnType());
return Boolean.valueOf((!_isAssignableFrom));
};
Iterable<? extends ResolvedMethod> _filter_1 = IterableExtensions.filter(IterableExtensions.filter(t.getAllResolvedMethods(), _function_3), _function_4);
for (final ResolvedMethod getter : _filter_1) {
{
final TypeReference rt = getter.getResolvedReturnType();
boolean _contains = TracedAccessorsProcessor.TYPES_WITH_GOOD_TO_STRING.contains(rt.getType().getSimpleName().toLowerCase());
if (_contains) {
final Procedure1<MutableMethodDeclaration> _function_5 = (MutableMethodDeclaration it) -> {
it.setReturnType(context.newTypeReference(IGeneratorNode.class));
it.addParameter("target", t);
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append(EStructuralFeature.class);
_builder.append(" feature = target.eClass().getEStructuralFeature(\"");
String _featureName = TracedAccessorsProcessor.this.featureName(getter);
_builder.append(_featureName);
_builder.append("\");");
_builder.newLineIfNotEmpty();
_builder.append(ILocationData.class);
_builder.append(" location = this.location(target, feature, -1);");
_builder.newLineIfNotEmpty();
_builder.append(CompositeGeneratorNode.class);
_builder.append(" trace = this.trace(location);");
_builder.newLineIfNotEmpty();
_builder.append("this.append(trace, target.");
String _simpleName = getter.getDeclaration().getSimpleName();
_builder.append(_simpleName);
_builder.append("());");
_builder.newLineIfNotEmpty();
_builder.append("return trace;");
_builder.newLine();
}
};
it.setBody(_client);
};
annotatedClass.addMethod(this.tracerName(getter), _function_5);
final Procedure1<MutableMethodDeclaration> _function_6 = (MutableMethodDeclaration it) -> {
it.setReturnType(context.newTypeReference(IGeneratorNode.class));
it.addParameter("target", t);
it.addParameter("useForDebugging", context.newTypeReference(Boolean.TYPE));
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append(EStructuralFeature.class);
_builder.append(" feature = target.eClass().getEStructuralFeature(\"");
String _featureName = TracedAccessorsProcessor.this.featureName(getter);
_builder.append(_featureName);
_builder.append("\");");
_builder.newLineIfNotEmpty();
_builder.append(ILocationData.class);
_builder.append(" location = this.location(target, feature, -1);");
_builder.newLineIfNotEmpty();
_builder.append(CompositeGeneratorNode.class);
_builder.append(" trace = this.trace(location, useForDebugging);");
_builder.newLineIfNotEmpty();
_builder.append("this.append(trace, target.");
String _simpleName = getter.getDeclaration().getSimpleName();
_builder.append(_simpleName);
_builder.append("());");
_builder.newLineIfNotEmpty();
_builder.append("return trace;");
_builder.newLine();
}
};
it.setBody(_client);
};
annotatedClass.addMethod(this.tracerName(getter), _function_6);
}
final Procedure1<MutableMethodDeclaration> _function_7 = (MutableMethodDeclaration it) -> {
it.setReturnType(context.newTypeReference(IGeneratorNode.class));
it.addParameter("target", t);
final TypeReference stringProvider = context.newTypeReference(Function.class, rt, context.getString());
it.addParameter("stringProvider", stringProvider);
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append(EStructuralFeature.class);
_builder.append(" feature = target.eClass().getEStructuralFeature(\"");
String _featureName = TracedAccessorsProcessor.this.featureName(getter);
_builder.append(_featureName);
_builder.append("\");");
_builder.newLineIfNotEmpty();
_builder.append(ILocationData.class);
_builder.append(" location = this.location(target, feature, -1);");
_builder.newLineIfNotEmpty();
_builder.append(CompositeGeneratorNode.class);
_builder.append(" trace = this.trace(location);");
_builder.newLineIfNotEmpty();
_builder.append("this.append(trace, stringProvider.apply(target.");
String _simpleName = getter.getDeclaration().getSimpleName();
_builder.append(_simpleName);
_builder.append("()));");
_builder.newLineIfNotEmpty();
_builder.append("return trace;");
_builder.newLine();
}
};
it.setBody(_client);
};
annotatedClass.addMethod(this.tracerName(getter), _function_7);
}
}
}
}
}
Aggregations