Search in sources :

Example 81 with CompilationUnitImpl

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

the class AbstractReusableActiveAnnotationTests method testFileSystemSupport_01.

@Test
public void testFileSystemSupport_01() {
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/FileSystemSupportTest.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.RegisterGlobalsContext\n\t\t\t\timport org.eclipse.xtend.lib.macro.RegisterGlobalsParticipant\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.ClassDeclaration\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration\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.AbstractClassProcessor\n\t\t\t\t\n\t\t\t\t@Active(FileSystemUsingProcessor)\n\t\t\t\tannotation FileSystemSupportTest { }\n\t\t\t\t\n\t\t\t\tclass FileSystemUsingProcessor extends AbstractClassProcessor {\n\t\n\t\t\t\t\toverride doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {\n\t\t\t\t\t\tval path = annotatedClass.compilationUnit.filePath\n\t\t\t\t\t\tannotatedClass.docComment = \'\'\'\n\t\t\t\t\t\t\tPath \'�path.toString�\' {\n\t\t\t\t\t\t\t\texists: �path.exists�\n\t\t\t\t\t\t\t\tisFolder: �path.isFolder�\n\t\t\t\t\t\t\t\tisFile: �path.isFile�\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tsourceFolder : �path.sourceFolder�\n\t\t\t\t\t\t\ttargetFolder : �path.targetFolder�\n\t\t\t\t\t\t\tprojectFolder: �path.projectFolder�\n\t\t\t\t\t\t\'\'\'\n\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("@myannotation.FileSystemSupportTest");
    _builder.newLine();
    _builder.append("class MyClass {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration declaredClass = it.getTypeLookup().findClass("myusercode.MyClass");
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("Path \'/userProject/src/myusercode/UserCode.xtend\' {");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("exists: true");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("isFolder: false");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("isFile: true");
        _builder_1.newLine();
        _builder_1.append("}");
        _builder_1.newLine();
        _builder_1.append("sourceFolder : /userProject/src");
        _builder_1.newLine();
        _builder_1.append("targetFolder : /userProject/xtend-gen");
        _builder_1.newLine();
        _builder_1.append("projectFolder: /userProject");
        _builder_1.newLine();
        Assert.assertEquals(_builder_1.toString(), declaredClass.getDocComment());
    };
    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 82 with CompilationUnitImpl

use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl 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 83 with CompilationUnitImpl

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

the class AbstractReusableActiveAnnotationTests method testAddConstructor.

@Test
public void testAddConstructor() {
    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.AbstractClassProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(typeof(AddConstructorProcessor))");
    _builder.newLine();
    _builder.append("annotation AddConstructor{ }");
    _builder.newLine();
    _builder.append("class AddConstructorProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration clazz, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("clazz.addConstructor [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("addParameter(\"foo\", 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();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AddConstructorAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@myannotation.AddConstructor 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");
        Assert.assertEquals(1, IterableExtensions.size(clazz.getDeclaredConstructors()));
        Assert.assertEquals("foo", IterableExtensions.head(IterableExtensions.head(clazz.getDeclaredConstructors()).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)

Example 84 with CompilationUnitImpl

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

the class AbstractReusableActiveAnnotationTests method testTracing4.

@Test
public void testTracing4() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.AbstractClassProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnoProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnno {}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnoProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addMethod(\"foo\") [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("primarySourceElement = cls.annotations.head");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("body = [\"return;\"]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MyAnno.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import myannotation.MyAnno");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnno");
    _builder_1.newLine();
    _builder_1.append("class Client extends Object{}");
    _builder_1.newLine();
    Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder_1.toString());
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration cls = it.getTypeLookup().findClass("myusercode.Client");
        final MutableMethodDeclaration fooMethod = cls.findDeclaredMethod("foo");
        final Element anno = it.getTracability().getPrimarySourceElement(IterableExtensions.head(cls.getAnnotations()));
        Assert.assertTrue((anno instanceof AnnotationReference));
        Assert.assertEquals(anno, it.getTracability().getPrimarySourceElement(((Element) fooMethod)));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) Element(org.eclipse.xtend.lib.macro.declaration.Element) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 85 with CompilationUnitImpl

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

the class AbstractReusableActiveAnnotationTests method testModifyTypeParameters.

@Test
public void testModifyTypeParameters() {
    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.MutableTypeParameterDeclarator");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(MyAnnotationProcessor)");
    _builder.newLine();
    _builder.append("annotation MyAnnotation {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MyAnnotationProcessor implements TransformationParticipant<MutableTypeParameterDeclarator> {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableTypeParameterDeclarator> annotatedTargetElements,");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("for (annotatedTargetElement : annotatedTargetElements) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("doTransform(annotatedTargetElement, context)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def doTransform(MutableTypeParameterDeclarator it, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (typeParameters.size != 0) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new IllegalStateException(\"Before: typeParameters.size != 0\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("addTypeParameter(\"T\", String.newTypeReference)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (typeParameters.size != 1) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new IllegalStateException(\"After: typeParameters.size != 1\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val typeParameter = typeParameters.head");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (!typeParameter.simpleName.equals(\"T\")) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new IllegalStateException(\"After: expected type name: \'T\', actual type name: \'\" + typeParameter.simpleName + \"\'\")");
    _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.MyAnnotation");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("class MyClass {");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("new() {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("interface MyInterface {");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@MyAnnotation");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("abstract def void foo();");
    _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 Procedure1<MutableTypeParameterDeclarator> _function_1 = (MutableTypeParameterDeclarator it_1) -> {
            Assert.assertEquals(1, IterableExtensions.size(it_1.getTypeParameters()));
            final MutableTypeParameterDeclaration typeParameter = IterableExtensions.head(it_1.getTypeParameters());
            Assert.assertEquals("T", typeParameter.getSimpleName());
            Assert.assertEquals(1, IterableExtensions.size(typeParameter.getUpperBounds()));
            Assert.assertEquals("java.lang.String", IterableExtensions.head(typeParameter.getUpperBounds()).getName());
        };
        final Procedure1<? super MutableTypeParameterDeclarator> assertMyAnnotationChanges = _function_1;
        final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.MyClass");
        assertMyAnnotationChanges.apply(clazz);
        assertMyAnnotationChanges.apply(IterableExtensions.head(clazz.getDeclaredConstructors()));
        final MutableInterfaceDeclaration myInterface = it.getTypeLookup().findInterface("myusercode.MyInterface");
        assertMyAnnotationChanges.apply(myInterface);
        assertMyAnnotationChanges.apply(IterableExtensions.head(myInterface.getDeclaredMethods()));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableTypeParameterDeclarator(org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclarator) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableTypeParameterDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableTypeParameterDeclaration) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Aggregations

CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)139 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)130 Test (org.junit.Test)128 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)90 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)21 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)17 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)15 EObject (org.eclipse.emf.ecore.EObject)13 Resource (org.eclipse.emf.ecore.resource.Resource)11 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)11 Type (org.eclipse.xtend.lib.macro.declaration.Type)10 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)10 List (java.util.List)9 AnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)9 ClassDeclaration (org.eclipse.xtend.lib.macro.declaration.ClassDeclaration)9 Element (org.eclipse.xtend.lib.macro.declaration.Element)9 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)9 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)9 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)8 AccessibleObject (java.lang.reflect.AccessibleObject)7