Search in sources :

Example 16 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testAnnotationDefaultValuesBug463161.

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

Example 17 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testAddDefaultConstructorWithTemplate.

@Test
public void testAddDefaultConstructorWithTemplate() {
    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("body=");
    _builder.append("\'\'\'", "\t\t\t");
    _builder.append("System.out.println(\"Hello World\");");
    _builder.append("\'\'\'", "\t\t\t");
    _builder.newLineIfNotEmpty();
    _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) -> {
        it.getCompilationUnit();
        final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.MyClass");
        Assert.assertEquals(1, IterableExtensions.size(clazz.getDeclaredConstructors()));
        Assert.assertTrue(IterableExtensions.isEmpty(IterableExtensions.head(clazz.getDeclaredConstructors()).getParameters()));
    };
    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 18 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testValidationPhase.

@Test
public void testValidationPhase() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.Active");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.AbstractClassProcessor");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.ClassDeclaration");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.ValidationContext");
    _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.newLine();
    _builder.append("@Active(ValidatingProcessor)");
    _builder.newLine();
    _builder.append("annotation ValidatingAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class ValidatingProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addWarning(\"Foo\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addWarning(\"Bar\")");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doValidate(ClassDeclaration cls, extension ValidationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addWarning(\"Baz\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("cls.addWarning(\"There were \" + cls.problems.size + \" problems\")");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/ValidatingAnnotation.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("@ValidatingAnnotation");
    _builder_1.newLine();
    _builder_1.append("class 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 cls = it.getTypeLookup().findClass("myusercode.Foo");
        final List<? extends Problem> problems = it.getProblemSupport().getProblems(cls);
        Assert.assertEquals(4, problems.size());
        Assert.assertEquals("Foo", problems.get(0).getMessage());
        Assert.assertEquals("Bar", problems.get(1).getMessage());
        Assert.assertEquals("Baz", problems.get(2).getMessage());
        Assert.assertEquals("There were 3 problems", problems.get(3).getMessage());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) List(java.util.List) Problem(org.eclipse.xtend.lib.macro.services.Problem) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 19 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testSetDocumentation.

@Test
public void testSetDocumentation() {
    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.MutableClassDeclaration");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(typeof(AbstractProcessor))");
    _builder.newLine();
    _builder.append("annotation Abstract { }");
    _builder.newLine();
    _builder.append("class AbstractProcessor implements TransformationParticipant<MutableClassDeclaration> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableClassDeclaration> annotatedSourceClasses, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedSourceClasses.forEach [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("docComment = docComment.toCharArray.reverse.join");
    _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/AbstractAnnotation.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("/**");
    _builder_1.newLine();
    _builder_1.append(" ");
    _builder_1.append("* dlroW olleH");
    _builder_1.newLine();
    _builder_1.append(" ");
    _builder_1.append("*/");
    _builder_1.newLine();
    _builder_1.append("@myannotation.Abstract");
    _builder_1.newLine();
    _builder_1.append("class MyClass {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _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("Hello World", clazz.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 20 with MutableClassDeclaration

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

the class AbstractReusableActiveAnnotationTests method testAddDefaultConstructor.

@Test
public void testAddDefaultConstructor() {
    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("body=[\'System.out.println(\"Hello World\");\']");
    _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.assertTrue(IterableExtensions.isEmpty(IterableExtensions.head(clazz.getDeclaredConstructors()).getParameters()));
    };
    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