Search in sources :

Example 6 with MutableFieldDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration 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 7 with MutableFieldDeclaration

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

the class EmfAdaptableProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    final MutableClassDeclaration adapterClass = context.findClass(this.getAdapterClassName(annotatedClass));
    final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
        it.addParameter("emfObject", context.newTypeReference(Notifier.class));
        it.setReturnType(context.newTypeReference(annotatedClass));
        it.setStatic(true);
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("for (");
                _builder.append(Adapter.class);
                _builder.append(" adapter : emfObject.eAdapters()) {");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("if (adapter instanceof ");
                _builder.append(adapterClass, "\t");
                _builder.append(") {");
                _builder.newLineIfNotEmpty();
                _builder.append("\t\t");
                _builder.append("return ((");
                _builder.append(adapterClass, "\t\t");
                _builder.append(") adapter).get();");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.append("}");
                _builder.newLine();
                _builder.append("return null;");
                _builder.newLine();
            }
        };
        it.setBody(_client);
    };
    annotatedClass.addMethod("findInEmfObject", _function);
    final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it) -> {
        it.addParameter("emfObject", context.newTypeReference(Notifier.class));
        it.setReturnType(context.newTypeReference(annotatedClass));
        it.setStatic(true);
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append(List.class);
                _builder.append("<");
                _builder.append(Adapter.class);
                _builder.append("> adapters = emfObject.eAdapters();");
                _builder.newLineIfNotEmpty();
                _builder.append("for(int i = 0, max = adapters.size(); i < max; i++) {");
                _builder.newLine();
                _builder.append("\t");
                _builder.append(Adapter.class, "\t");
                _builder.append(" adapter = adapters.get(i);");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("if (adapter instanceof ");
                _builder.append(adapterClass, "\t");
                _builder.append(") {");
                _builder.newLineIfNotEmpty();
                _builder.append("\t\t");
                _builder.append("emfObject.eAdapters().remove(i);");
                _builder.newLine();
                _builder.append("\t\t");
                _builder.append("return ((");
                _builder.append(adapterClass, "\t\t");
                _builder.append(") adapter).get();");
                _builder.newLineIfNotEmpty();
                _builder.append("\t");
                _builder.append("}");
                _builder.newLine();
                _builder.append("}");
                _builder.newLine();
                _builder.append("return null;");
                _builder.newLine();
            }
        };
        it.setBody(_client);
    };
    annotatedClass.addMethod("removeFromEmfObject", _function_1);
    final Procedure1<MutableMethodDeclaration> _function_2 = (MutableMethodDeclaration it) -> {
        it.addParameter("emfObject", context.newTypeReference(Notifier.class));
        it.setReturnType(context.getPrimitiveVoid());
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                String _simpleName = annotatedClass.getSimpleName();
                _builder.append(_simpleName);
                _builder.append(" result = findInEmfObject(emfObject);");
                _builder.newLineIfNotEmpty();
                _builder.append("if (result != null)");
                _builder.newLine();
                _builder.append("\t");
                _builder.append("throw new IllegalStateException(\"The given EMF object already contains an adapter for ");
                String _simpleName_1 = annotatedClass.getSimpleName();
                _builder.append(_simpleName_1, "\t");
                _builder.append("\");");
                _builder.newLineIfNotEmpty();
                _builder.append(adapterClass);
                _builder.append(" adapter = new ");
                _builder.append(adapterClass);
                _builder.append("(this);");
                _builder.newLineIfNotEmpty();
                _builder.append("emfObject.eAdapters().add(adapter);");
                _builder.newLine();
            }
        };
        it.setBody(_client);
    };
    annotatedClass.addMethod("attachToEmfObject", _function_2);
    adapterClass.setExtendedClass(context.newTypeReference(AdapterImpl.class));
    final Procedure1<MutableFieldDeclaration> _function_3 = (MutableFieldDeclaration it) -> {
        it.setType(context.newTypeReference(annotatedClass));
    };
    adapterClass.addField("element", _function_3);
    final Procedure1<MutableConstructorDeclaration> _function_4 = (MutableConstructorDeclaration it) -> {
        it.addParameter("element", context.newTypeReference(annotatedClass));
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("this.element = element;");
                _builder.newLine();
            }
        };
        it.setBody(_client);
    };
    adapterClass.addConstructor(_function_4);
    final Procedure1<MutableMethodDeclaration> _function_5 = (MutableMethodDeclaration it) -> {
        it.setReturnType(context.newTypeReference(annotatedClass));
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("return this.element;");
                _builder.newLine();
            }
        };
        it.setBody(_client);
    };
    adapterClass.addMethod("get", _function_5);
    final Procedure1<MutableMethodDeclaration> _function_6 = (MutableMethodDeclaration it) -> {
        it.addAnnotation(context.newAnnotationReference(Override.class));
        it.addParameter("object", context.newTypeReference(Object.class));
        it.setReturnType(context.getPrimitiveBoolean());
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append("return object == ");
                _builder.append(annotatedClass);
                _builder.append(".class;");
                _builder.newLineIfNotEmpty();
            }
        };
        it.setBody(_client);
    };
    adapterClass.addMethod("isAdapterForType", _function_6);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) AdapterImpl(org.eclipse.emf.common.notify.impl.AdapterImpl) Adapter(org.eclipse.emf.common.notify.Adapter) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) MutableConstructorDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration) List(java.util.List)

Example 8 with MutableFieldDeclaration

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

the class LogProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration cls, @Extension final TransformationContext context) {
    final Procedure1<MutableFieldDeclaration> _function = (MutableFieldDeclaration it) -> {
        it.setStatic(true);
        it.setFinal(true);
        it.setType(context.newTypeReference(Logger.class));
        StringConcatenationClient _client = new StringConcatenationClient() {

            @Override
            protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                _builder.append(Logger.class);
                _builder.append(".getLogger(");
                String _simpleName = cls.getSimpleName();
                _builder.append(_simpleName);
                _builder.append(".class)");
                _builder.newLineIfNotEmpty();
            }
        };
        it.setInitializer(_client);
        context.setPrimarySourceElement(it, cls);
    };
    cls.addField("LOG", _function);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) Logger(org.apache.log4j.Logger)

Example 9 with MutableFieldDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration 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 10 with MutableFieldDeclaration

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

the class AbstractReusableActiveAnnotationTests method testDeterministicExecutionOrder_02.

@Test
public void testDeterministicExecutionOrder_02() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import myannotation.*");
    _builder.newLine();
    _builder.append("class MyClass {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@_A @_B @_C String field1");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@_C @_B @_A String field2");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("MyClass.xtend", _builder.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration myClass = it.getTypeLookup().findClass("MyClass");
        Assert.assertEquals("field1_A_B_C", IterableExtensions.head(myClass.getDeclaredFields()).getSimpleName());
        Assert.assertEquals("field2_A_B_C", ((MutableFieldDeclaration[]) Conversions.unwrapArray(myClass.getDeclaredFields(), MutableFieldDeclaration.class))[1].getSimpleName());
    };
    this.assertProcessing(this.THREE_ANNOTATIONS, _mappedTo, _function);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Aggregations

MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)23 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)16 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)16 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)12 Test (org.junit.Test)12 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)11 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)5 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)5 CompilationStrategy (org.eclipse.xtend.lib.macro.declaration.CompilationStrategy)3 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)3 JvmField (org.eclipse.xtext.common.types.JvmField)3 Set (java.util.Set)2 EObject (org.eclipse.emf.ecore.EObject)2 MutableConstructorDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration)2 MutableTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableTypeDeclaration)2 MutableTypeParameterDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration)2 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)2 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)2 Procedure0 (org.eclipse.xtext.xbase.lib.Procedures.Procedure0)2 Serializable (java.io.Serializable)1