Search in sources :

Example 6 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 7 with MutableMethodDeclaration

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

the class AddInterfaceWithDefaultProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    super.doTransform(annotatedClass, context);
    Type _findTypeGlobally = context.findTypeGlobally("de.test.Test");
    final MutableInterfaceDeclaration ifType = ((MutableInterfaceDeclaration) _findTypeGlobally);
    final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("System.out.println(\"Hello World\");");
            }
        };
        it.setBody(_client);
        it.setDefault(true);
    };
    ifType.addMethod("sayHello", _function);
}
Also used : MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) Type(org.eclipse.xtend.lib.macro.declaration.Type) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)

Example 8 with MutableMethodDeclaration

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

the class Bug446364Processor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    String _stringValue = IterableExtensions.head(annotatedClass.getAnnotations()).getStringValue("value");
    if (_stringValue != null) {
        switch(_stringValue) {
            case "rename":
                final Consumer<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
                    String _simpleName = it.getSimpleName();
                    String _plus = ("prefix_" + _simpleName);
                    it.setSimpleName(_plus);
                };
                annotatedClass.getDeclaredMethods().forEach(_function);
                break;
            case "changeBody":
                final Consumer<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it) -> {
                    StringConcatenationClient _client = new StringConcatenationClient() {

                        @Override
                        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                            _builder.append("return null;");
                        }
                    };
                    it.setBody(_client);
                };
                annotatedClass.getDeclaredMethods().forEach(_function_1);
                break;
        }
    }
}
Also used : StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)

Example 9 with MutableMethodDeclaration

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

the class InferredTypeReferencesCheckProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration it, @Extension final TransformationContext context) {
    final TypeReference type = it.findDeclaredField("foo").getType();
    Assert.assertTrue(type.isInferred());
    final Procedure0 _function = () -> {
        it.setExtendedClass(type);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as extended class.", _function);
    final Procedure0 _function_1 = () -> {
        Iterable<? extends TypeReference> _implementedInterfaces = it.getImplementedInterfaces();
        Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_implementedInterfaces, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(type)));
        it.setImplementedInterfaces(_plus);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as implemented interface.", _function_1);
    final Procedure1<MutableMethodDeclaration> _function_2 = (MutableMethodDeclaration it_1) -> {
        it_1.setReturnType(type);
        final Procedure0 _function_3 = () -> {
            it_1.addParameter("bar", type);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_3);
        final Procedure0 _function_4 = () -> {
            it_1.setExceptions(type);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as exception type.", _function_4);
        final Procedure0 _function_5 = () -> {
            it_1.addTypeParameter("T", type);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_5);
        MutableTypeParameterDeclaration _addTypeParameter = it_1.addTypeParameter("T");
        final Procedure1<MutableTypeParameterDeclaration> _function_6 = (MutableTypeParameterDeclaration it_2) -> {
            final Procedure0 _function_7 = () -> {
                Iterable<? extends TypeReference> _upperBounds = it_2.getUpperBounds();
                Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_upperBounds, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(type)));
                it_2.setUpperBounds(_plus);
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_7);
        };
        ObjectExtensions.<MutableTypeParameterDeclaration>operator_doubleArrow(_addTypeParameter, _function_6);
    };
    it.addMethod("bar", _function_2);
    final Procedure1<MutableFieldDeclaration> _function_3 = (MutableFieldDeclaration field) -> {
        field.setType(type);
    };
    it.addField("bar2", _function_3);
    MutableInterfaceDeclaration _findInterface = context.findInterface("Bar");
    final Procedure1<MutableInterfaceDeclaration> _function_4 = (MutableInterfaceDeclaration it_1) -> {
        final Procedure0 _function_5 = () -> {
            Iterable<? extends TypeReference> _extendedInterfaces = it_1.getExtendedInterfaces();
            Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_extendedInterfaces, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(type)));
            it_1.setExtendedInterfaces(_plus);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as extended interface.", _function_5);
        final Procedure0 _function_6 = () -> {
            it_1.addTypeParameter("T", type);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_6);
        MutableTypeParameterDeclaration _addTypeParameter = it_1.addTypeParameter("T");
        final Procedure1<MutableTypeParameterDeclaration> _function_7 = (MutableTypeParameterDeclaration it_2) -> {
            final Procedure0 _function_8 = () -> {
                Iterable<? extends TypeReference> _upperBounds = it_2.getUpperBounds();
                Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_upperBounds, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(type)));
                it_2.setUpperBounds(_plus);
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_8);
        };
        ObjectExtensions.<MutableTypeParameterDeclaration>operator_doubleArrow(_addTypeParameter, _function_7);
    };
    ObjectExtensions.<MutableInterfaceDeclaration>operator_doubleArrow(_findInterface, _function_4);
    final Procedure0 _function_5 = () -> {
        it.addTypeParameter("T", type);
    };
    MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_5);
    MutableTypeParameterDeclaration _addTypeParameter = it.addTypeParameter("T");
    final Procedure1<MutableTypeParameterDeclaration> _function_6 = (MutableTypeParameterDeclaration it_1) -> {
        final Procedure0 _function_7 = () -> {
            Iterable<? extends TypeReference> _upperBounds = it_1.getUpperBounds();
            Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_upperBounds, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(type)));
            it_1.setUpperBounds(_plus);
        };
        MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot use inferred type as parameter type.", _function_7);
    };
    ObjectExtensions.<MutableTypeParameterDeclaration>operator_doubleArrow(_addTypeParameter, _function_6);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableTypeParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration) MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) Procedure0(org.eclipse.xtext.xbase.lib.Procedures.Procedure0) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference)

Example 10 with MutableMethodDeclaration

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

the class AbstractReusableActiveAnnotationTests method testThrowsAndTypeParam.

@Test
public void testThrowsAndTypeParam() {
    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.MutableMethodDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(typeof(ThrowsAndTypeParamProcessor))");
    _builder.newLine();
    _builder.append("annotation ThrowsAndTypeParam { }");
    _builder.newLine();
    _builder.append("class ThrowsAndTypeParamProcessor implements TransformationParticipant<MutableMethodDeclaration> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableMethodDeclaration> annotatedMethods, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedMethods.forEach [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("val type = addTypeParameter(\'A\')");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("addParameter(\'myParam\', newTypeReference(type))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setExceptions(newTypeReference(\'java.lang.Exception\'))");
    _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/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.ThrowsAndTypeParam");
    _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("}");
    _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());
        Assert.assertEquals("A", IterableExtensions.head(method.getTypeParameters()).getSimpleName());
        Assert.assertEquals("myParam", IterableExtensions.head(method.getParameters()).getSimpleName());
        Assert.assertSame(IterableExtensions.head(method.getTypeParameters()), IterableExtensions.head(method.getParameters()).getType().getType());
        Assert.assertEquals(1, IterableExtensions.size(method.getExceptions()));
    };
    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