use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testRemoveTypeParameters.
@Test
public void testRemoveTypeParameters() {
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("typeParameters.head.remove");
_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<T extends String> {");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("interface MyInterface<T extends String> {");
_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 <T extends String> 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(0, IterableExtensions.size(it_1.getTypeParameters()));
};
final Procedure1<? super MutableTypeParameterDeclarator> assertMyAnnotationChanges = _function_1;
assertMyAnnotationChanges.apply(it.getTypeLookup().findClass("myusercode.MyClass"));
final MutableInterfaceDeclaration myInterface = it.getTypeLookup().findInterface("myusercode.MyInterface");
assertMyAnnotationChanges.apply(myInterface);
assertMyAnnotationChanges.apply(IterableExtensions.head(myInterface.getDeclaredMethods()));
};
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 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);
}
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);
}
use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl 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);
}
use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class ActiveAnnotationsRuntimeTest method testDetectOrphanedElements.
@Test
public void testDetectOrphanedElements() {
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.AbstractClassProcessor");
_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.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.RegisterGlobalsContext");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(EvilProcessor)");
_builder.newLine();
_builder.append("annotation EvilAnnotation {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class EvilProcessor extends AbstractClassProcessor {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doRegisterGlobals(List<? extends ClassDeclaration> classes, extension RegisterGlobalsContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("classes.forEach[");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("registerClass(qualifiedName+\'.Inner\')");
_builder.newLine();
_builder.append("\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableClassDeclaration> classes, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("classes.forEach[");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("addField(\"foo\")[");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("type = object");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("markAsRead");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("addMethod(\"foo\")[");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("returnType = object");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("body = [\"return null;\"]");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("addParameter(\"x\", Integer.newTypeReference)");
_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.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/EvilAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import myannotation.EvilAnnotation");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@EvilAnnotation");
_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) -> {
this.validator.assertIssue(it.getXtendFile(), XtendPackage.Literals.XTEND_FILE, IssueCodes.ORPHAN_ELEMENT, Severity.WARNING, "The generated field \'myusercode.Foo.foo\' is not associated with a source element.");
this.validator.assertIssue(it.getXtendFile(), XtendPackage.Literals.XTEND_FILE, IssueCodes.ORPHAN_ELEMENT, Severity.WARNING, "The generated method \'myusercode.Foo.foo(Integer)\' is not associated with a source element.");
this.validator.assertIssue(it.getXtendFile(), XtendPackage.Literals.XTEND_FILE, IssueCodes.ORPHAN_ELEMENT, Severity.WARNING, "The generated type \'myusercode.Foo.Inner\' is not associated with a source element.");
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Aggregations