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);
}
use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration 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.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);
}
use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration 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.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testMovingComputedTypes_02.
@Test
public void testMovingComputedTypes_02() {
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.MutableMethodDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(Method2FieldProcessor)");
_builder.newLine();
_builder.append("annotation Method2Field { }");
_builder.newLine();
_builder.append("class Method2FieldProcessor implements TransformationParticipant<MutableMethodDeclaration> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableMethodDeclaration> annotatedMethods, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedMethods.forEach [ method |");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("method.declaringType.addField(method.simpleName) [");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("type = method.returnType");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("initializer = method.body");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("method.remove");
_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/MyAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("class MyClass {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("val foo = \'foo\'");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@myannotation.Method2Field");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def x() { foo }");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@myannotation.Method2Field");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def y() { x }");
_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 type = it.getTypeLookup().findClass("myusercode.MyClass");
final MutableFieldDeclaration field = ((MutableFieldDeclaration[]) Conversions.unwrapArray(type.getDeclaredFields(), MutableFieldDeclaration.class))[1];
Assert.assertEquals("java.lang.String", field.getType().getName());
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Aggregations