Search in sources :

Example 21 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 22 with MutableMethodDeclaration

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

the class AbstractReusableActiveAnnotationTests method testTracing.

@Test
public void testTracing() {
    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.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(GettersProcessor)");
    _builder.newLine();
    _builder.append("annotation Getters {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class GettersProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.declaredFields.filter[isThePrimaryGeneratedJavaElement].forEach [field|");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("cls.addMethod(\"get\" + field.simpleName.toFirstUpper) [");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("primarySourceElement = field");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("returnType = field.type");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("body = [\"return this.\" + field.simpleName + \";\"]");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("field.markAsRead");
    _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/Getters.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.Getters");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@Getters");
    _builder_1.newLine();
    _builder_1.append("class Client {");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val int bar = 1");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def create new Integer(1) foo() {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _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 cls = it.getTypeLookup().findClass("myusercode.Client");
        final MutableFieldDeclaration barField = cls.findDeclaredField("bar");
        final Function1<MutableMethodDeclaration, Boolean> _function_1 = (MutableMethodDeclaration it_1) -> {
            return Boolean.valueOf(it_1.getSimpleName().startsWith("get"));
        };
        final Iterable<? extends MutableMethodDeclaration> getters = IterableExtensions.filter(cls.getDeclaredMethods(), _function_1);
        Assert.assertEquals(1, IterableExtensions.size(getters));
        final MutableMethodDeclaration getBar = IterableExtensions.head(getters);
        Assert.assertEquals("getBar", getBar.getSimpleName());
        final JvmField barJvmField = ((MutableJvmFieldDeclarationImpl) barField).getDelegate();
        final JvmOperation getBarJvmMethod = ((MutableJvmMethodDeclarationImpl) getBar).getDelegate();
        final Set<EObject> elementsAssociatedWithBarField = it.getJvmModelAssociations().getJvmElements(it.getJvmModelAssociations().getPrimarySourceElement(barJvmField));
        Assert.assertEquals(2, elementsAssociatedWithBarField.size());
        Assert.assertEquals(barJvmField, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[0]);
        Assert.assertEquals(getBarJvmMethod, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[1]);
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : Set(java.util.Set) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EObject(org.eclipse.emf.ecore.EObject) JvmField(org.eclipse.xtext.common.types.JvmField) Test(org.junit.Test)

Example 23 with MutableMethodDeclaration

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

the class AccessObjectProcessor method doTransform.

@Override
public void doTransform(final List<? extends MutableClassDeclaration> annotatedSourceClasses, @Extension final TransformationContext ctx) {
    final Consumer<MutableClassDeclaration> _function = (MutableClassDeclaration it) -> {
        final Consumer<MutableFieldDeclaration> _function_1 = (MutableFieldDeclaration field) -> {
            MutableTypeDeclaration _declaringType = field.getDeclaringType();
            String _firstUpper = StringExtensions.toFirstUpper(field.getSimpleName());
            String _plus = ("get" + _firstUpper);
            final Procedure1<MutableMethodDeclaration> _function_2 = (MutableMethodDeclaration it_1) -> {
                it_1.setReturnType(field.getType());
                final CompilationStrategy _function_3 = (CompilationStrategy.CompilationContext it_2) -> {
                    StringConcatenation _builder = new StringConcatenation();
                    _builder.append("return this.");
                    String _simpleName = field.getSimpleName();
                    _builder.append(_simpleName);
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                    return _builder;
                };
                it_1.setBody(_function_3);
            };
            _declaringType.addMethod(_plus, _function_2);
        };
        it.getDeclaredFields().forEach(_function_1);
        String _qualifiedName = it.getQualifiedName();
        int _length = it.getQualifiedName().length();
        int _length_1 = it.getSimpleName().length();
        int _minus = (_length - _length_1);
        final String pkg = _qualifiedName.substring(0, _minus);
        final TypeReference ser = ctx.newTypeReference(Serializable.class);
        if ((ser == null)) {
            ctx.addError(it, "Cannot find Serializable");
        }
        String _simpleName = it.getSimpleName();
        final String PVersionName = ((pkg + "P") + _simpleName);
        final MutableClassDeclaration p = ctx.findClass(PVersionName);
        if ((p == null)) {
            ctx.addError(it, (("Class " + PVersionName) + " not found"));
        }
        if (((p != null) && (ser != null))) {
            final LinkedList<TypeReference> pIfcs = new LinkedList<TypeReference>();
            pIfcs.add(ser);
            p.setImplementedInterfaces(pIfcs);
        }
        String _simpleName_1 = it.getSimpleName();
        final String GVersionName = ((pkg + "G") + _simpleName_1);
        final MutableClassDeclaration g = ctx.findClass(GVersionName);
        if ((g == null)) {
            ctx.addError(it, (("Class " + GVersionName) + " not found"));
        }
        if (((g != null) && (ser != null))) {
            final LinkedList<TypeReference> gIfcs = new LinkedList<TypeReference>();
            gIfcs.add(ser);
            g.setImplementedInterfaces(gIfcs);
        }
    };
    annotatedSourceClasses.forEach(_function);
}
Also used : Serializable(java.io.Serializable) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) MutableTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) LinkedList(java.util.LinkedList) Consumer(java.util.function.Consumer) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) CompilationStrategy(org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)

Example 24 with MutableMethodDeclaration

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

the class AccessorsProcessor method tryAddMethod.

public MutableMethodDeclaration tryAddMethod(final MutableTypeDeclaration it, final String name, final Procedure1<? super MutableMethodDeclaration> initializer) {
    MutableMethodDeclaration _elvis = null;
    MutableMethodDeclaration _findDeclaredMethod = it.findDeclaredMethod(name);
    if (_findDeclaredMethod != null) {
        _elvis = _findDeclaredMethod;
    } else {
        MutableMethodDeclaration _addMethod = it.addMethod(name, ((Procedure1<MutableMethodDeclaration>) initializer));
        _elvis = _addMethod;
    }
    return _elvis;
}
Also used : MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1)

Example 25 with MutableMethodDeclaration

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

the class AccessorsProcessor method doTransform.

@Override
public void doTransform(final List<? extends MutableFieldDeclaration> javaFields, @Extension final TransformationContext context) {
    for (final MutableFieldDeclaration f : javaFields) {
        {
            String _firstUpper = StringExtensions.toFirstUpper(f.getSimpleName());
            final String getterName = ("get" + _firstUpper);
            String _firstUpper_1 = StringExtensions.toFirstUpper(f.getSimpleName());
            final String setterName = ("set" + _firstUpper_1);
            final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
                it.setReturnType(f.getType());
                final CompilationStrategy _function_1 = (CompilationStrategy.CompilationContext it_1) -> {
                    StringConcatenation _builder = new StringConcatenation();
                    _builder.append("return ");
                    String _simpleName = f.getSimpleName();
                    _builder.append(_simpleName);
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                    return _builder;
                };
                it.setBody(_function_1);
            };
            this.tryAddMethod(f.getDeclaringType(), getterName, _function);
            boolean _isFinal = f.isFinal();
            boolean _not = (!_isFinal);
            if (_not) {
                final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it) -> {
                    it.setReturnType(context.getPrimitiveVoid());
                    it.addParameter(f.getSimpleName(), f.getType());
                    final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_1) -> {
                        StringConcatenation _builder = new StringConcatenation();
                        _builder.append("this.");
                        String _simpleName = f.getSimpleName();
                        _builder.append(_simpleName);
                        _builder.append(" = ");
                        String _simpleName_1 = f.getSimpleName();
                        _builder.append(_simpleName_1);
                        _builder.append(";");
                        _builder.newLineIfNotEmpty();
                        return _builder;
                    };
                    it.setBody(_function_2);
                };
                this.tryAddMethod(f.getDeclaringType(), setterName, _function_1);
            }
        }
    }
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) CompilationStrategy(org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)

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