Search in sources :

Example 1 with MutableFieldDeclaration

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

the class DeclarationsTest method testAnnotation2.

@Test
public void testAnnotation2() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class MyClass {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@com.google.inject.Inject() MyClass foo");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
        final ClassDeclaration sourceClazz = ((ClassDeclaration) _head);
        final MutableClassDeclaration javaClass = it.getTypeLookup().findClass("MyClass");
        Assert.assertEquals(javaClass.getQualifiedName(), sourceClazz.getQualifiedName());
        final FieldDeclaration field = IterableExtensions.head(sourceClazz.getDeclaredFields());
        Assert.assertEquals(Boolean.FALSE, IterableExtensions.head(field.getAnnotations()).getValue("optional"));
        final MutableFieldDeclaration javaField = IterableExtensions.head(javaClass.getDeclaredFields());
        Object _value = IterableExtensions.head(javaField.getAnnotations()).getValue("optional");
        Assert.assertFalse((((Boolean) _value)).booleanValue());
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : ClassDeclaration(org.eclipse.xtend.lib.macro.declaration.ClassDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AccessibleObject(java.lang.reflect.AccessibleObject) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration) TypeDeclaration(org.eclipse.xtend.lib.macro.declaration.TypeDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) FieldDeclaration(org.eclipse.xtend.lib.macro.declaration.FieldDeclaration) Test(org.junit.Test)

Example 2 with MutableFieldDeclaration

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

the class AbstractReusableActiveAnnotationTests method testAnnotationDefaultValuesBug463161.

@Test
public void testAnnotationDefaultValuesBug463161() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import java.util.List");
    _builder.newLine();
    _builder.append("import java.lang.annotation.RetentionPolicy");
    _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.AbstractClassProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(AnnotationDefaultValuesProcessor)");
    _builder.newLine();
    _builder.append("annotation AnnotationDefaultValues { }");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class AnnotationDefaultValuesProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration mutableClass, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annotationRef = mutableClass.findAnnotation(findTypeGlobally(MyAnnotation))");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("mutableClass.addField(annotationRef.getExpression(\'value\')?.toString ?: \'wasNull\') [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("type = string");
    _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();
    _builder.append("annotation MyAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("int value = 1");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AnnotationDefaultValues.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@myannotation.AnnotationDefaultValues");
    _builder_1.newLine();
    _builder_1.append("@myannotation.MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("class MyClass {}");
    _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.MyClass");
        final MutableFieldDeclaration field = IterableExtensions.head(clazz.getDeclaredFields());
        Assert.assertEquals("wasNull", field.getSimpleName());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _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)

Example 3 with MutableFieldDeclaration

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

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

the class _TESTDATA_InternalClassProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    String _qualifiedName = annotatedClass.getQualifiedName();
    String _plus = (_qualifiedName + ".InternalClass");
    MutableClassDeclaration _findClass = context.findClass(_plus);
    final Procedure1<MutableClassDeclaration> _function = (MutableClassDeclaration it) -> {
        final Procedure1<MutableFieldDeclaration> _function_1 = (MutableFieldDeclaration it_1) -> {
            it_1.setType(context.getString());
        };
        it.addField("myField", _function_1);
    };
    ObjectExtensions.<MutableClassDeclaration>operator_doubleArrow(_findClass, _function);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)

Example 5 with MutableFieldDeclaration

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

the class FileProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    final Path path = annotatedClass.getCompilationUnit().getFilePath();
    final String contents = context.getContents(context.getProjectFolder(path).append("res/template.txt")).toString();
    final String[] segments = contents.trim().split(",");
    for (final String segment : segments) {
        final Procedure1<MutableFieldDeclaration> _function = (MutableFieldDeclaration it) -> {
            it.setType(context.getString());
        };
        annotatedClass.addField(segment, _function);
    }
}
Also used : Path(org.eclipse.xtend.lib.macro.file.Path) MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)

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