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 testMarkReadAndInitialized.
@Test
public void testMarkReadAndInitialized() {
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/InitAnnotation.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.TransformationContext\n\t\t\t\timport org.eclipse.xtend.lib.macro.TransformationParticipant\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration\n\n\t\t\t\t@Active(InitProcessor)\n\t\t\t\tannotation Init { }\n\t\t\t\tclass InitProcessor implements TransformationParticipant<MutableFieldDeclaration> {\n\t\t\t\t\t\n\t\t\t\t\toverride doTransform(List<? extends MutableFieldDeclaration> annotatedTargetFields, extension TransformationContext context) {\n\t\t\t\t\t\tval ctor = annotatedTargetFields.head.declaringType.addConstructor [\n\t\t\t\t\t\t\tprimarySourceElement = declaringType\n\t\t\t\t\t\t\tbody = [\'\'\'\n\t\t\t\t\t\t\t\t�FOR f : annotatedTargetFields�\n\t\t\t\t\t\t\t\t\tthis.�f.simpleName� = \"foo\";\n\t\t\t\t\t\t\t\t�ENDFOR�\n\t\t\t\t\t\t\t\'\'\']\n\t\t\t\t\t\t]\n\t\t\t\t\t\tannotatedTargetFields.forEach [ field |\n\t\t\t\t\t\t\tfield.setFinal(true)\n\t\t\t\t\t\t\tfield.markAsRead\n\t\t\t\t\t\t\tfield.markAsInitializedBy(ctor)\n\t\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("class MyClass {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("@myannotation.Init String myField");
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/MyClass.xtend", _builder.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
MutableClassDeclaration _findClass = it.getTypeLookup().findClass("myusercode.MyClass");
final JvmGenericType clazz = ((MutableJvmClassDeclarationImpl) _findClass).getDelegate();
this.validator.assertNoIssues(clazz);
};
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 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);
}
use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testParameterAnnotation.
@Test
public void testParameterAnnotation() {
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.TransformationParticipant");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.RegisterGlobalsParticipant");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.RegisterGlobalsContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.ParameterDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(typeof(ParamProcessor))");
_builder.newLine();
_builder.append("annotation Param { }");
_builder.newLine();
_builder.append("class ParamProcessor implements TransformationParticipant<MutableParameterDeclaration>, RegisterGlobalsParticipant<ParameterDeclaration> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableParameterDeclaration> params, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("params.forEach[");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("simpleName = simpleName+\'foo\'");
_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 doRegisterGlobals(List<? extends ParameterDeclaration> annotatedSourceElements, extension RegisterGlobalsContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedSourceElements.forEach [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("context.registerInterface(\"demo.I\"+it.simpleName.toFirstUpper)");
_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("class MyClass {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def void foo(@myannotation.Param String a, @myannotation.Param String b) {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_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");
final Function1<MutableParameterDeclaration, Boolean> _function_1 = (MutableParameterDeclaration it_1) -> {
return Boolean.valueOf(it_1.getSimpleName().endsWith("foo"));
};
Assert.assertTrue(IterableExtensions.forall(IterableExtensions.head(clazz.getDeclaredMethods()).getParameters(), _function_1));
Assert.assertNotNull(it.getTypeLookup().findInterface("demo.IA"));
Assert.assertNotNull(it.getTypeLookup().findInterface("demo.IB"));
};
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 testAnnotationDefaultValues_02.
@Test
public void testAnnotationDefaultValues_02() {
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.TransformationParticipant");
_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 implements TransformationParticipant<MutableClassDeclaration> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableClassDeclaration> annotationTargets, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotationTargets.forEach[ annotationTarget |");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annotation = annotationTarget.findAnnotation(findTypeGlobally(MyAnnotation))");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val strings = annotation.getStringArrayValue(\'value\').map[ toString ]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("strings.forEach [ annotationTarget.addField(it) [ type = string ] ]");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val booleans = annotation.getBooleanArrayValue(\'booleans\').map[ toString ]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("booleans.forEach [ annotationTarget.addField(it) [ type = string ] ]");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val types = annotation.getClassArrayValue(\'types\').map[ toString ]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("types.forEach [ annotationTarget.addField(it) [ type = string ] ]");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val policies = annotation.getEnumArrayValue(\'policies\').map[ toString ]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("policies.forEach [ annotationTarget.addField(it) [ type = string ] ]");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val nested = annotation.getAnnotationArrayValue(\'nested\').map[ toString ]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("nested.forEach [ annotationTarget.addField(it) [ 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("String[] value = #[]");
_builder.newLine();
_builder.append("\t");
_builder.append("boolean[] booleans = #[]");
_builder.newLine();
_builder.append("\t");
_builder.append("Class<?>[] types = #[]");
_builder.newLine();
_builder.append("\t");
_builder.append("RetentionPolicy[] policies = #[]");
_builder.newLine();
_builder.append("\t");
_builder.append("Active[] nested = #[]");
_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("\t");
_builder_1.append("value = #[],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("booleans = #[],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("types = #[],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("policies = #[],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("nested = #[]");
_builder_1.newLine();
_builder_1.append(")");
_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");
Assert.assertTrue(IterableExtensions.isEmpty(clazz.getDeclaredFields()));
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Aggregations