use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testSimpleModification.
@Test
public void testSimpleModification() {
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("^abstract = true");
_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("@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.assertTrue(clazz.isAbstract());
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl 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);
}
use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testBug453273.
@Test
public void testBug453273() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import java.lang.annotation.ElementType");
_builder.newLine();
_builder.append("import java.lang.annotation.Target");
_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.declaration.ClassDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Target(ElementType.TYPE)");
_builder.newLine();
_builder.append("@Active(AddNestedTypesProcessor)");
_builder.newLine();
_builder.append("annotation AddNestedTypes {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class AddNestedTypesProcessor extends AbstractClassProcessor {");
_builder.newLine();
_builder.append(" ");
_builder.append("override doRegisterGlobals(ClassDeclaration it, RegisterGlobalsContext registerGlobalsContext) {");
_builder.newLine();
_builder.append(" ");
_builder.append("registerGlobalsContext.registerClass(qualifiedName + \".NestedType\")");
_builder.newLine();
_builder.append(" ");
_builder.append("registerGlobalsContext.registerClass(qualifiedName + \".NestedType.NestedType2\")");
_builder.newLine();
_builder.append(" ");
_builder.append("registerGlobalsContext.registerClass(packageName + \".OtherTopLevelClass\")");
_builder.newLine();
_builder.append(" ");
_builder.append("registerGlobalsContext.registerClass(packageName + \".OtherTopLevelClass.NestedType\")");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append(" ");
_builder.newLine();
_builder.append(" ");
_builder.append("def String getPackageName(ClassDeclaration it) {");
_builder.newLine();
_builder.append(" \t ");
_builder.append("qualifiedName.substring(0,qualifiedName.lastIndexOf(\'.\'))");
_builder.newLine();
_builder.append(" ");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AddNestedTypes.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package my.client");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@myannotation.AddNestedTypes");
_builder_1.newLine();
_builder_1.append("class TopLevelClass {");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("my/client/UserCode.xtend", _builder_1.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
this.validator.assertNoErrors(it.getXtendFile());
Assert.assertNotNull(it.getTypeLookup().findClass("my.client.TopLevelClass"));
Assert.assertNotNull(it.getTypeLookup().findClass("my.client.TopLevelClass.NestedType"));
Assert.assertNotNull(it.getTypeLookup().findClass("my.client.TopLevelClass.NestedType.NestedType2"));
Assert.assertNotNull(it.getTypeLookup().findClass("my.client.OtherTopLevelClass"));
Assert.assertNotNull(it.getTypeLookup().findClass("my.client.OtherTopLevelClass.NestedType"));
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testMoveExpressionToTypeParameterDeclarator.
@Test
public void testMoveExpressionToTypeParameterDeclarator() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import java.lang.annotation.ElementType");
_builder.newLine();
_builder.append("import java.lang.annotation.Target");
_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.MethodDeclaration");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Target(ElementType.TYPE)");
_builder.newLine();
_builder.append("@Active(SomeAnnotationProcessor)");
_builder.newLine();
_builder.append("annotation SomeAnnotation {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class SomeAnnotationProcessor extends AbstractClassProcessor {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("static def copyMethod(MutableClassDeclaration clazz, MethodDeclaration method, extension TransformationContext context) {");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("val methodDeclaration = clazz.addMethod(method.simpleName) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("it.abstract = method.abstract");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("it.docComment = method.docComment");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("it.visibility = method.visibility");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("it.synchronized = method.synchronized");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("it.deprecated = method.deprecated");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("it.static = method.static");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("it.exceptions = method.exceptions");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("it.varArgs = method.varArgs");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("it.strictFloatingPoint = method.strictFloatingPoint");
_builder.newLine();
_builder.append("\t\t");
_builder.append("]");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (t : method.typeParameters) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("val result = methodDeclaration.addTypeParameter(t.simpleName, t.upperBounds)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("result.primarySourceElement = t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("methodDeclaration.returnType = method.returnType");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (p : method.parameters) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("methodDeclaration.addParameter(p.simpleName, p.type)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("return methodDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (m : annotatedClass.declaredMethods) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("val newMethod = annotatedClass.copyMethod(m, context)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("newMethod.simpleName = newMethod.simpleName + \"_copiedMethod\"");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("newMethod.body = m.body");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("m.body = \'\'");
_builder.append("\'// new method\'\'");
_builder.append("\'");
_builder.newLine();
_builder.append("\t\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_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("@myannotation.SomeAnnotation");
_builder_1.newLine();
_builder_1.append("class SomeAnnotationTest {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def <T> void myMethod() {");
_builder_1.newLine();
_builder_1.append("\t\t");
_builder_1.append("var T myObj = null;");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_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) -> {
this.validator.assertNoErrors(it.getXtendFile());
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testTracing2.
@Test
public void testTracing2() {
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.typeParameters.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<A> {}");
_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 typeParameter = it.getTracability().getPrimarySourceElement(IterableExtensions.head(cls.getTypeParameters()));
Assert.assertEquals(typeParameter, it.getTracability().getPrimarySourceElement(fooMethod));
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Aggregations