Search in sources :

Example 16 with MutableMethodDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testTracing3.

@Test
public void testTracing3() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.AbstractClassProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnoProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnno {}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnoProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addMethod(\"foo\") [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("primarySourceElement = cls.extendedClass");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("body = [\"return;\"]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnno.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.MyAnno");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnno");
    _builder_1.newLine();
    _builder_1.append("class Client extends Object{}");
    _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 cls = it.getTypeLookup().findClass("myusercode.Client");
        final MutableMethodDeclaration fooMethod = cls.findDeclaredMethod("foo");
        final Element extendsClause = it.getTracability().getPrimarySourceElement(cls.getExtendedClass());
        Assert.assertTrue((extendsClause instanceof TypeReference));
        Assert.assertEquals(extendsClause, it.getTracability().getPrimarySourceElement(((Element) fooMethod)));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Element(org.eclipse.xtend.lib.macro.declaration.Element) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 17 with MutableMethodDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testTracing2.

@Test
public void testTracing2() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.AbstractClassProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnoProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnno {}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnoProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addMethod(\"foo\") [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("primarySourceElement = cls.typeParameters.head");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("body = [\"return;\"]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnno.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.MyAnno");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnno");
    _builder_1.newLine();
    _builder_1.append("class Client<A> {}");
    _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 cls = it.getTypeLookup().findClass("myusercode.Client");
        final MutableMethodDeclaration fooMethod = cls.findDeclaredMethod("foo");
        final Element typeParameter = it.getTracability().getPrimarySourceElement(IterableExtensions.head(cls.getTypeParameters()));
        Assert.assertEquals(typeParameter, it.getTracability().getPrimarySourceElement(fooMethod));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Element(org.eclipse.xtend.lib.macro.declaration.Element) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 18 with MutableMethodDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testMovingComputedTypes.

@Test
public void testMovingComputedTypes() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _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.MutableFieldDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(Field2MethodProcessor)");
    _builder.newLine();
    _builder.append("annotation Field2Method { }");
    _builder.newLine();
    _builder.append("class Field2MethodProcessor implements TransformationParticipant<MutableFieldDeclaration> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableFieldDeclaration> annotatedFields, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedFields.forEach [ field |");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("field.declaringType.addMethod(field.simpleName) [");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("returnType = field.type");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("body = field.initializer");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("field.remove");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _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("class MyClass {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val foo = \'foo\'");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@myannotation.Field2Method");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val x = foo");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@myannotation.Field2Method");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val y = x");
    _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 type = it.getTypeLookup().findClass("myusercode.MyClass");
        final MutableMethodDeclaration method = ((MutableMethodDeclaration[]) Conversions.unwrapArray(type.getDeclaredMethods(), MutableMethodDeclaration.class))[1];
        Assert.assertEquals("java.lang.String", method.getReturnType().getName());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 19 with MutableMethodDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testValidation.

@Test
public void testValidation() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _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.MutableFieldDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(typeof(ValidatedProcessor))");
    _builder.newLine();
    _builder.append("annotation Validated { }");
    _builder.newLine();
    _builder.append("class ValidatedProcessor implements TransformationParticipant<MutableNamedElement> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableNamedElement> annotatedMethods, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedMethods.forEach [ ele |");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("switch ele {");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("MutableFieldDeclaration : ele.addWarning(\'field-warning\')");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("default : ele.addWarning(\'warning\')");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AbstractAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("class MyClass {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@myannotation.Validated");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def void foo() {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@myannotation.Validated");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("String name");
    _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 type = it.getTypeLookup().findClass("myusercode.MyClass");
        final MutableMethodDeclaration method = IterableExtensions.head(type.getDeclaredMethods());
        final MutableFieldDeclaration field = IterableExtensions.head(type.getDeclaredFields());
        Assert.assertEquals("field-warning", IterableExtensions.head(it.getProblemSupport().getProblems(field)).getMessage());
        Assert.assertEquals("warning", IterableExtensions.head(it.getProblemSupport().getProblems(method)).getMessage());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 20 with MutableMethodDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testPropertyAnnotation.

@Test
public void testPropertyAnnotation() {
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/PropertyAnnotation.xtend", "\n\t\t\t\tpackage myannotation\n\t\t\t\t\n\t\t\t\timport java.util.List\n\t\t\t\timport org.eclipse.xtend.lib.macro.Active\n\t\t\t\timport org.eclipse.xtend.lib.macro.TransformationContext\n\t\t\t\timport org.eclipse.xtend.lib.macro.TransformationParticipant\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration\n\n\t\t\t\t@Active(typeof(PropertyProcessor))\n\t\t\t\tannotation Property2 { }\n\t\t\t\tclass PropertyProcessor implements TransformationParticipant<MutableFieldDeclaration> {\n\t\t\t\t\t\n\t\t\t\t\toverride doTransform(List<? extends MutableFieldDeclaration> annotatedTargetFields, extension TransformationContext context) {\n\t\t\t\t\t\tannotatedTargetFields.forEach [ field |\n\t\t\t\t\t\t\tval declaringType = field.declaringType \n\t\t\t\t\t\t\tdeclaringType.addMethod(field.getterName) [\n\t\t\t\t\t\t\t\treturnType = field.type\n\t\t\t\t\t\t\t\tbody = [\'\'\'\n\t\t\t\t\t\t\t\t\treturn this.�field.simpleName�;\n\t\t\t\t\t\t\t\t\'\'\']\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\tdeclaringType.addMethod(\'set\'+field.simpleName.toFirstUpper) [\n\t\t\t\t\t\t\t\taddParameter(field.simpleName, field.type)\n\t\t\t\t\t\t\t\tbody = [\'\'\'\n\t\t\t\t\t\t\t\t\tthis.�field.simpleName� = �field.simpleName�;\n\t\t\t\t\t\t\t\t\'\'\']\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t]\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tdef private String getterName(MutableFieldDeclaration field) {\n\t\t\t\t\t\treturn \'get\'+field.simpleName.toFirstUpper\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t");
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myusercode");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyClass {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@myannotation.Property2 String myField");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.MyClass");
        final MutableMethodDeclaration getter = IterableExtensions.head(clazz.getDeclaredMethods());
        Assert.assertEquals("getMyField", getter.getSimpleName());
        Assert.assertEquals("String", getter.getReturnType().toString());
        final MutableMethodDeclaration setter = clazz.findDeclaredMethod("setMyField", getter.getReturnType());
        Assert.assertEquals("setMyField", setter.getSimpleName());
        Assert.assertEquals("void", setter.getReturnType().toString());
        Assert.assertEquals("myField", IterableExtensions.head(setter.getParameters()).getSimpleName());
        Assert.assertEquals("String", IterableExtensions.head(setter.getParameters()).getType().toString());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Aggregations

MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)34 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)24 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)22 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)19 Test (org.junit.Test)19 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)12 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)9 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)8 Element (org.eclipse.xtend.lib.macro.declaration.Element)6 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)6 CompilationStrategy (org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)5 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)4 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)3 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)3 AccessibleObject (java.lang.reflect.AccessibleObject)2 List (java.util.List)2 Set (java.util.Set)2 EObject (org.eclipse.emf.ecore.EObject)2 MemberDeclaration (org.eclipse.xtend.lib.macro.declaration.MemberDeclaration)2 MethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MethodDeclaration)2