Search in sources :

Example 6 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testMarkReadAndInitialized.

@Test
public void testMarkReadAndInitialized() {
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/InitAnnotation.xtend", "\n\t\t\t\tpackage myannotation\n\t\t\t\t\n\t\t\t\timport java.util.List\n\t\t\t\timport org.eclipse.xtend.lib.macro.Active\n\t\t\t\timport org.eclipse.xtend.lib.macro.TransformationContext\n\t\t\t\timport org.eclipse.xtend.lib.macro.TransformationParticipant\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration\n\n\t\t\t\t@Active(InitProcessor)\n\t\t\t\tannotation Init { }\n\t\t\t\tclass InitProcessor implements TransformationParticipant<MutableFieldDeclaration> {\n\t\t\t\t\t\n\t\t\t\t\toverride doTransform(List<? extends MutableFieldDeclaration> annotatedTargetFields, extension TransformationContext context) {\n\t\t\t\t\t\tval ctor = annotatedTargetFields.head.declaringType.addConstructor [\n\t\t\t\t\t\t\tprimarySourceElement = declaringType\n\t\t\t\t\t\t\tbody = [\'\'\'\n\t\t\t\t\t\t\t\t�FOR f : annotatedTargetFields�\n\t\t\t\t\t\t\t\t\tthis.�f.simpleName� = \"foo\";\n\t\t\t\t\t\t\t\t�ENDFOR�\n\t\t\t\t\t\t\t\'\'\']\n\t\t\t\t\t\t]\n\t\t\t\t\t\tannotatedTargetFields.forEach [ field |\n\t\t\t\t\t\t\tfield.setFinal(true)\n\t\t\t\t\t\t\tfield.markAsRead\n\t\t\t\t\t\t\tfield.markAsInitializedBy(ctor)\n\t\t\t\t\t\t]\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t");
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myusercode");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyClass {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@myannotation.Init String myField");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/MyClass.xtend", _builder.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        MutableClassDeclaration _findClass = it.getTypeLookup().findClass("myusercode.MyClass");
        final JvmGenericType clazz = ((MutableJvmClassDeclarationImpl) _findClass).getDelegate();
        this.validator.assertNoIssues(clazz);
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 7 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testAnnotationValueSetting_1.

@Test
public void testAnnotationValueSetting_1() {
    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.*");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.*");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(ConfigurableAnnotationProcessor)");
    _builder.newLine();
    _builder.append("annotation ConfigurableAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("BlackOrWhite color");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("BlackOrWhite[] colors");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Class<?> type");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Class<?>[] types");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("SomeAnnotation annotation");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("SomeAnnotation[] annotations");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("annotation SomeAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("boolean value");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("enum BlackOrWhite {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("BLACK, WHITE");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class ConfigurableAnnotationProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val anno = annotatedClass.annotations.head");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val someAnnotationType = findTypeGlobally(SomeAnnotation)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val enumType = findTypeGlobally(\'myannotation.BlackOrWhite\') as EnumerationTypeDeclaration");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val white = enumType.findDeclaredValue(\'WHITE\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val black = enumType.findDeclaredValue(\'BLACK\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingValue = anno.getValue(\'color\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingValue != white)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"color\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithColor = annotatedClass.addAnnotation(newAnnotationReference(anno) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setEnumValue(\'color\', black)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(anno)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingColorsValue = annoWithColor.getValue(\'colors\') as Object[]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingColorsValue.get(0) != white && existingColorsValue.get(1) != black && existingColorsValue.length != 2)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"colors\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithColors = annotatedClass.addAnnotation(newAnnotationReference(annoWithColor) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setEnumValue(\'colors\', black, white)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithColor)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingType = annoWithColors.getValue(\'type\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingType != string)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"type\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithType = annotatedClass.addAnnotation(newAnnotationReference(annoWithColors) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setClassValue(\'type\', annotatedClass.newTypeReference)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithColors)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingTypes = annoWithType.getValue(\'types\') as Object[]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingTypes.get(0) != primitiveInt && existingTypes.get(1) != annotatedClass.newTypeReference && existingTypes.length != 2)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"types\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithTypes = annotatedClass.addAnnotation(newAnnotationReference(annoWithType) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setClassValue(\'types\', primitiveBoolean)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithType)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annotationReference = annoWithTypes.getAnnotationValue(\'annotation\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (someAnnotationType != annotationReference.annotationTypeDeclaration)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"someAnnotationType != annotationReference.annotationTypeDeclaration\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithAnnotation = annotatedClass.addAnnotation(newAnnotationReference(annoWithTypes) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setAnnotationValue(\'annotation\', ");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("newAnnotationReference(someAnnotationType) [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("setBooleanValue(\'value\', false)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append(")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithTypes)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annotationReferences = annoWithAnnotation.getAnnotationArrayValue(\'annotations\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (annotationReferences.size != 2)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"annotationReferences.size != 2\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotationReferences.forEach [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("if (someAnnotationType != annotationTypeDeclaration)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("throw new AssertionError(\"someAnnotationType != annotationTypeDeclaration\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.addAnnotation(newAnnotationReference(annoWithAnnotation) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setAnnotationValue(\'annotations\', ");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("newAnnotationReference(someAnnotationType) [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("setBooleanValue(\'value\', false)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("],");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("newAnnotationReference(someAnnotationType) [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("setBooleanValue(\'value\', false)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append(")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithAnnotation)");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/ConfigurableAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.*");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@ConfigurableAnnotation(");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("color=BlackOrWhite.WHITE, ");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("colors=#[BlackOrWhite.WHITE, BlackOrWhite.BLACK], ");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("type = String, ");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("types=#[Integer, MyClass],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("annotation=@SomeAnnotation(true),");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("annotations=#[@SomeAnnotation(true), @SomeAnnotation(true)]");
    _builder_1.newLine();
    _builder_1.append(")");
    _builder_1.newLine();
    _builder_1.append("class MyClass {");
    _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.MyClass");
        Type _findTypeGlobally = it.getTypeLookup().findTypeGlobally("myannotation.BlackOrWhite");
        final EnumerationTypeDeclaration colorEnum = ((EnumerationTypeDeclaration) _findTypeGlobally);
        final Type annotationType = it.getTypeLookup().findTypeGlobally("myannotation.ConfigurableAnnotation");
        final AnnotationReference annotation = clazz.findAnnotation(annotationType);
        Assert.assertEquals(colorEnum.findDeclaredValue("BLACK"), annotation.getValue("color"));
        Object _value = annotation.getValue("colors");
        final Object[] colors = ((Object[]) _value);
        Assert.assertEquals(2, colors.length);
        Assert.assertEquals(colorEnum.findDeclaredValue("BLACK"), colors[0]);
        Assert.assertEquals(colorEnum.findDeclaredValue("WHITE"), colors[1]);
        Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(clazz), annotation.getValue("type"));
        final TypeReference[] types = annotation.getClassArrayValue("types");
        Assert.assertEquals(1, types.length);
        Assert.assertEquals(it.getTypeReferenceProvider().getPrimitiveBoolean(), types[0]);
        final Type someAnnotationType = it.getTypeLookup().findTypeGlobally("myannotation.SomeAnnotation");
        final AnnotationReference annotationValue = annotation.getAnnotationValue("annotation");
        Assert.assertNotNull(annotationValue);
        Assert.assertEquals(someAnnotationType, annotationValue.getAnnotationTypeDeclaration());
        Assert.assertFalse(annotationValue.getBooleanValue("value"));
        final AnnotationReference[] annotationsValue = annotation.getAnnotationArrayValue("annotations");
        Assert.assertNotNull(annotationsValue);
        Assert.assertEquals(2, ((List<AnnotationReference>) Conversions.doWrapArray(annotationsValue)).size());
        final Consumer<AnnotationReference> _function_1 = (AnnotationReference it_1) -> {
            Assert.assertEquals(someAnnotationType, annotationValue.getAnnotationTypeDeclaration());
            Assert.assertFalse(annotationValue.getBooleanValue("value"));
        };
        ((List<AnnotationReference>) Conversions.doWrapArray(annotationsValue)).forEach(_function_1);
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) Type(org.eclipse.xtend.lib.macro.declaration.Type) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) Consumer(java.util.function.Consumer) 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) List(java.util.List) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) EnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.EnumerationTypeDeclaration) MutableEnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration) Test(org.junit.Test)

Example 8 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testAddAnnotationValue.

@Test
public void testAddAnnotationValue() {
    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.MutableAnnotationTarget");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(AddAnnotationValueProcessor)");
    _builder.newLine();
    _builder.append("annotation AddAnnotationValue { }");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class AddAnnotationValueProcessor implements TransformationParticipant<MutableAnnotationTarget> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableAnnotationTarget> annotationTargets, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotationTargets.forEach [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("addAnnotation(");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("MyAnnotation.findTypeGlobally.newAnnotationReference [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'value\', #[\'foo\', \'bar\', \'baz\'] as String[])");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'singleValue\', \'foo\')");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'booleans\', #[true, false, true] as boolean[])");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'singleBoolean\', true)");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'numbers\', #[1, 2, 3] as int[])");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'singleNumber\', 1)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("])");
    _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("String[] value");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String singleValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("boolean[] booleans");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("boolean singleBoolean");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("int[] numbers");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("int singleNumber");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AddAnnotationValue.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@myannotation.AddAnnotationValue");
    _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 AnnotationReference annotation = clazz.findAnnotation(it.getTypeReferenceProvider().newTypeReference("myannotation.MyAnnotation").getType());
        Object _value = annotation.getValue("value");
        Assert.assertArrayEquals(((String[]) ((String[]) Conversions.unwrapArray(Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("foo", "bar", "baz")), String.class))), ((String[]) _value));
        Assert.assertEquals("foo", annotation.getValue("singleValue"));
        Object _value_1 = annotation.getValue("booleans");
        final boolean[] booleans = ((boolean[]) _value_1);
        Assert.assertTrue(booleans[2]);
        Assert.assertEquals(Boolean.valueOf(true), annotation.getValue("singleBoolean"));
        Object _value_2 = annotation.getValue("numbers");
        Assert.assertArrayEquals(new int[] { 1, 2, 3 }, ((int[]) _value_2));
        Assert.assertEquals(Integer.valueOf(1), annotation.getValue("singleNumber"));
    };
    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) Test(org.junit.Test)

Example 9 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testInferredTypeReferences.

@Test
public void testInferredTypeReferences() {
    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.ClassDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnotationProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Class<?> value");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnotationProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration it, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("for (field : declaredFields) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("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 = [\'return 1;\']");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val myAnnotation = findAnnotation(MyAnnotation.findTypeGlobally)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val type = myAnnotation.getClassValue(\"value\").type as ClassDeclaration");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("for (field : type.declaredFields) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("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 = [\'return 1;\']");
    _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.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("import myannotation.*");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation(Bar)");
    _builder_1.newLine();
    _builder_1.append("class Foo {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val y = 1");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("class Bar {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("val x = 1");
    _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 foo = it.getTypeLookup().findClass("myusercode.Foo");
        final MutableClassDeclaration bar = it.getTypeLookup().findClass("myusercode.Bar");
        this.assertSameType(foo.findDeclaredField("y").getType(), foo.findDeclaredMethod("y").getReturnType());
        this.assertSameType(bar.findDeclaredField("x").getType(), foo.findDeclaredMethod("x").getReturnType());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _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 10 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testConstantExpressionEvaluation.

@Test
public void testConstantExpressionEvaluation() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package annotations");
    _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.RegisterGlobalsContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.ClassDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.Visibility");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(Processor)");
    _builder.newLine();
    _builder.append("annotation MyAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String value");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Processor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doRegisterGlobals(ClassDeclaration annotatedClass, extension RegisterGlobalsContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val value = annotatedClass.annotations.head.getValue(\'value\') as String");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("registerClass(annotatedClass.qualifiedName+\'.\'+value)");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _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 value = annotatedClass.annotations.head.getValue(\'value\') as String");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val newClass = findClass(annotatedClass.qualifiedName+\'.\'+value)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("for (method : annotatedClass.declaredMethods) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("method.addParameter(value, newClass.newTypeReference)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("newClass.addField(value) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("type = string");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("visibility = Visibility.PUBLIC");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("initializer = \'");
    _builder.append("\'\'\"FOO\"\'");
    _builder.append("\'\'");
    _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("annotations/MyAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package application");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import annotations.MyAnnotation");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation(\"FOO\") class MyClient {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("def myMethod() {");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.append("FOO.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("application/MyAnnotation.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration clazz = it.getTypeLookup().findClass("application.MyClient.FOO");
        Assert.assertEquals("FOO", IterableExtensions.head(clazz.getDeclaredFields()).getSimpleName());
        final MutableClassDeclaration clazz2 = it.getTypeLookup().findClass("application.MyClient");
        Assert.assertEquals("FOO", IterableExtensions.head(IterableExtensions.head(clazz2.getDeclaredMethods()).getParameters()).getSimpleName());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _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)

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