Search in sources :

Example 86 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testDeterministicExecutionOrder_01.

@Test
public void testDeterministicExecutionOrder_01() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import myannotation.*");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyClass {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@_A @_B @_C String field");
    _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("field_A_B_C", IterableExtensions.head(myClass.getDeclaredFields()).getSimpleName());
    };
    this.assertProcessing(this.THREE_ANNOTATIONS, _mappedTo, _function);
}
Also used : 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 87 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testAnnotationValueSetting_AsExpression.

@Ignore("Setting annotation values of type Expression is not possible")
@Test
public void testAnnotationValueSetting_AsExpression() {
    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.newLine();
    _builder.append("@Active(MoveAnnotationValuesProcessor)");
    _builder.newLine();
    _builder.append("annotation MoveValues {}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MoveAnnotationValuesProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val classAnnotation = annotatedClass.annotations.head");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val field = annotatedClass.declaredFields.head");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val fieldAnnotation = field.annotations.head");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("field.removeAnnotation(fieldAnnotation)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("field.addAnnotation(fieldAnnotation.annotationTypeDeclaration.qualifiedName.newAnnotationReference [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'booleanValue\', classAnnotation.getExpression(\'booleanValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'intValue\', classAnnotation.getExpression(\'intValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'longValue\', classAnnotation.getExpression(\'longValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'stringValue\', classAnnotation.getExpression(\'stringValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'booleanArrayValue\', classAnnotation.getExpression(\'booleanArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'intArrayValue\', classAnnotation.getExpression(\'intArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'longArrayValue\', classAnnotation.getExpression(\'longArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'stringArrayValue\', classAnnotation.getExpression(\'stringArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'typeValue\', classAnnotation.getExpression(\'typeValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'typeArrayValue\', classAnnotation.getExpression(\'typeArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'annotation2Value\', classAnnotation.getExpression(\'annotation2Value\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'annotation2ArrayValue\', classAnnotation.getExpression(\'annotation2ArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'enumValue\', classAnnotation.getExpression(\'enumValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'enumArrayValue\', classAnnotation.getExpression(\'enumArrayValue\'))");
    _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/MoveValues.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import test.Annotation");
    _builder_1.newLine();
    _builder_1.append("import test.Annotation2");
    _builder_1.newLine();
    _builder_1.append("import myannotation.MoveValues");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@Annotation(");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("intValue = 2 / 2 + 2 * 3 - 4 % 1,");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("longValue = 42 + 4 + 6 * 42 - 4 / 45,");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("stringValue = \'foo\' + \'baz\',");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("booleanValue = true,");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("booleanArrayValue = #[true, false],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("intArrayValue = #[ -1, 34 + 45, 2 - 6 ],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("longArrayValue = #[42, 5 * -3],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("stringArrayValue = #[\'foo\', \'bla\' + \'buzz\'],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("typeValue = String,");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("typeArrayValue = #[String, Integer],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("annotation2Value = @Annotation2(\'foo\' + \'wuppa\'),");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("annotation2ArrayValue = #[@Annotation2, @Annotation2(\'foo\'+\'wuppa\')],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("enumValue = test.Enum1.YELLOW,");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("enumArrayValue = #[test.Enum1.YELLOW, test.Enum1.RED]");
    _builder_1.newLine();
    _builder_1.append(")");
    _builder_1.newLine();
    _builder_1.append("@MoveValues ");
    _builder_1.newLine();
    _builder_1.append("class UserCode {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@Annotation() String foo");
    _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 clazz = it.getTypeLookup().findClass("myusercode.UserCode");
        final AnnotationReference annoRef = IterableExtensions.head(IterableExtensions.head(clazz.getDeclaredFields()).getAnnotations());
        Assert.assertEquals(Integer.valueOf((((2 / 2) + (2 * 3)) - (4 % 1))), annoRef.getValue("intValue"));
        Assert.assertEquals(Long.valueOf(((long) (((42 + 4) + (6 * 42)) - (4 / 45)))), annoRef.getValue("longValue"));
        Assert.assertEquals("foobaz", annoRef.getValue("stringValue"));
        Object _value = annoRef.getValue("booleanValue");
        Assert.assertTrue((((Boolean) _value)).booleanValue());
        Object _value_1 = annoRef.getValue("booleanArrayValue");
        final boolean[] bools = ((boolean[]) _value_1);
        Assert.assertTrue(bools[0]);
        Assert.assertFalse(bools[1]);
        Object _value_2 = annoRef.getValue("intArrayValue");
        Assert.assertArrayEquals(new int[] { (-1), (34 + 45), (2 - 6) }, ((int[]) _value_2));
        Object _value_3 = annoRef.getValue("typeArrayValue");
        final TypeReference[] type = ((TypeReference[]) _value_3);
        Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(Integer.class), type[1]);
        Object _value_4 = annoRef.getValue("annotation2Value");
        final AnnotationReference anno = ((AnnotationReference) _value_4);
        Assert.assertEquals("foowuppa", anno.getValue("value"));
        Object _value_5 = annoRef.getValue("annotation2ArrayValue");
        final AnnotationReference[] annoArray = ((AnnotationReference[]) _value_5);
        Assert.assertEquals("HUBBA BUBBA!", annoArray[0].getValue("value"));
        Object _value_6 = annoRef.getValue("enumValue");
        final EnumerationValueDeclaration enum1 = ((EnumerationValueDeclaration) _value_6);
        Assert.assertEquals("YELLOW", enum1.getSimpleName());
        Object _value_7 = annoRef.getValue("enumArrayValue");
        final EnumerationValueDeclaration[] enumArray = ((EnumerationValueDeclaration[]) _value_7);
        Assert.assertEquals("YELLOW", IterableExtensions.<EnumerationValueDeclaration>head(((Iterable<EnumerationValueDeclaration>) Conversions.doWrapArray(enumArray))).getSimpleName());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) EObject(org.eclipse.emf.ecore.EObject) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) EnumerationValueDeclaration(org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration) MutableEnumerationValueDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 88 with MutableClassDeclaration

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

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

the class AccessObjectProcessorTest method testWithPackage.

@Test
public void testWithPackage() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package my.pack");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.core.tests.macro.Accessors");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@org.eclipse.xtend.core.tests.macro.AccessObjectAnn");
    _builder.newLine();
    _builder.append("class A {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String field");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
        final TransformationContext ctx = it.getTransformationContext();
        final MutableClassDeclaration classA = ctx.findClass("my.pack.A");
        Assert.assertNotNull(classA.findDeclaredMethod("getField"));
        final MutableClassDeclaration classPA = ctx.findClass("my.pack.PA");
        final Function1<TypeReference, Boolean> _function_1 = (TypeReference it_1) -> {
            Type _type = it_1.getType();
            Type _type_1 = ctx.newTypeReference(Serializable.class).getType();
            return Boolean.valueOf(Objects.equal(_type, _type_1));
        };
        Assert.assertNotNull(IterableExtensions.findFirst(classPA.getImplementedInterfaces(), _function_1));
        final MutableClassDeclaration classGA = ctx.findClass("my.pack.GA");
        final Function1<TypeReference, Boolean> _function_2 = (TypeReference it_1) -> {
            Type _type = it_1.getType();
            Type _type_1 = ctx.newTypeReference(Serializable.class).getType();
            return Boolean.valueOf(Objects.equal(_type, _type_1));
        };
        Assert.assertNotNull(IterableExtensions.findFirst(classGA.getImplementedInterfaces(), _function_2));
    };
    this._xtendCompilerTester.compile(_builder, _function);
}
Also used : Type(org.eclipse.xtend.lib.macro.declaration.Type) Serializable(java.io.Serializable) XtendCompilerTester(org.eclipse.xtend.core.compiler.batch.XtendCompilerTester) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) TransformationContext(org.eclipse.xtend.lib.macro.TransformationContext) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) AbstractActiveAnnotationTest(org.eclipse.xtend.core.tests.macro.AbstractActiveAnnotationTest) Test(org.junit.Test)

Example 90 with MutableClassDeclaration

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

the class AccessorsAnnotationTest method testProperty.

@Test
public void testProperty() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import org.eclipse.xtend.core.tests.macro.Accessors");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class A {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@Accessors String field");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@Accessors val String finalField = \'foo\'");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
        final TransformationContext ctx = it.getTransformationContext();
        final MutableClassDeclaration classA = ctx.findClass("A");
        Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("getField").getVisibility());
        Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("setField", ctx.getString()).getVisibility());
        Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("getFinalField").getVisibility());
        Assert.assertNull(classA.findDeclaredMethod("setFinalField", ctx.getString()));
    };
    this._xtendCompilerTester.compile(_builder, _function);
}
Also used : XtendCompilerTester(org.eclipse.xtend.core.compiler.batch.XtendCompilerTester) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TransformationContext(org.eclipse.xtend.lib.macro.TransformationContext) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) AbstractActiveAnnotationTest(org.eclipse.xtend.core.tests.macro.AbstractActiveAnnotationTest) Test(org.junit.Test)

Aggregations

MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)97 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)94 Test (org.junit.Test)91 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)87 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)24 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)17 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)14 EObject (org.eclipse.emf.ecore.EObject)10 Type (org.eclipse.xtend.lib.macro.declaration.Type)10 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)10 List (java.util.List)9 Element (org.eclipse.xtend.lib.macro.declaration.Element)8 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)8 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)7 AnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)6 ClassDeclaration (org.eclipse.xtend.lib.macro.declaration.ClassDeclaration)6 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)6 AccessibleObject (java.lang.reflect.AccessibleObject)5 Consumer (java.util.function.Consumer)4 XtendCompilerTester (org.eclipse.xtend.core.compiler.batch.XtendCompilerTester)4