use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testAddAnnotationValue.
@Test
public void testAddAnnotationValue() {
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.MutableAnnotationTarget");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(AddAnnotationValueProcessor)");
_builder.newLine();
_builder.append("annotation AddAnnotationValue { }");
_builder.newLine();
_builder.newLine();
_builder.append("class AddAnnotationValueProcessor implements TransformationParticipant<MutableAnnotationTarget> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableAnnotationTarget> annotationTargets, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotationTargets.forEach [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("addAnnotation(");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("MyAnnotation.findTypeGlobally.newAnnotationReference [");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("set(\'value\', #[\'foo\', \'bar\', \'baz\'] as String[])");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("set(\'singleValue\', \'foo\')");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("set(\'booleans\', #[true, false, true] as boolean[])");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("set(\'singleBoolean\', true)");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("set(\'numbers\', #[1, 2, 3] as int[])");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("set(\'singleNumber\', 1)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("])");
_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("String singleValue");
_builder.newLine();
_builder.append("\t");
_builder.append("boolean[] booleans");
_builder.newLine();
_builder.append("\t");
_builder.append("boolean singleBoolean");
_builder.newLine();
_builder.append("\t");
_builder.append("int[] numbers");
_builder.newLine();
_builder.append("\t");
_builder.append("int singleNumber");
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AddAnnotationValue.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@myannotation.AddAnnotationValue");
_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 AnnotationReference annotation = clazz.findAnnotation(it.getTypeReferenceProvider().newTypeReference("myannotation.MyAnnotation").getType());
Object _value = annotation.getValue("value");
Assert.assertArrayEquals(((String[]) ((String[]) Conversions.unwrapArray(Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("foo", "bar", "baz")), String.class))), ((String[]) _value));
Assert.assertEquals("foo", annotation.getValue("singleValue"));
Object _value_1 = annotation.getValue("booleans");
final boolean[] booleans = ((boolean[]) _value_1);
Assert.assertTrue(booleans[2]);
Assert.assertEquals(Boolean.valueOf(true), annotation.getValue("singleBoolean"));
Object _value_2 = annotation.getValue("numbers");
Assert.assertArrayEquals(new int[] { 1, 2, 3 }, ((int[]) _value_2));
Assert.assertEquals(Integer.valueOf(1), annotation.getValue("singleNumber"));
};
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 testConstantExpressionEvaluation.
@Test
public void testConstantExpressionEvaluation() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package annotations");
_builder.newLine();
_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.TransformationContext");
_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.declaration.Visibility");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(Processor)");
_builder.newLine();
_builder.append("annotation MyAnnotation {");
_builder.newLine();
_builder.append("\t");
_builder.append("String value");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class Processor extends AbstractClassProcessor {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doRegisterGlobals(ClassDeclaration annotatedClass, extension RegisterGlobalsContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val value = annotatedClass.annotations.head.getValue(\'value\') as String");
_builder.newLine();
_builder.append("\t\t");
_builder.append("registerClass(annotatedClass.qualifiedName+\'.\'+value)");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val value = annotatedClass.annotations.head.getValue(\'value\') as String");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val newClass = findClass(annotatedClass.qualifiedName+\'.\'+value)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (method : annotatedClass.declaredMethods) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("method.addParameter(value, newClass.newTypeReference)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("newClass.addField(value) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("type = string");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("visibility = Visibility.PUBLIC");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("initializer = \'");
_builder.append("\'\'\"FOO\"\'");
_builder.append("\'\'");
_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("annotations/MyAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package application");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import annotations.MyAnnotation");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation(\"FOO\") class MyClient {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def myMethod() {");
_builder_1.newLine();
_builder_1.append("\t\t");
_builder_1.append("FOO.FOO");
_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("application/MyAnnotation.xtend", _builder_1.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final MutableClassDeclaration clazz = it.getTypeLookup().findClass("application.MyClient.FOO");
Assert.assertEquals("FOO", IterableExtensions.head(clazz.getDeclaredFields()).getSimpleName());
final MutableClassDeclaration clazz2 = it.getTypeLookup().findClass("application.MyClient");
Assert.assertEquals("FOO", IterableExtensions.head(IterableExtensions.head(clazz2.getDeclaredMethods()).getParameters()).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 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 testThrowsAndTypeParam.
@Test
public void testThrowsAndTypeParam() {
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(typeof(ThrowsAndTypeParamProcessor))");
_builder.newLine();
_builder.append("annotation ThrowsAndTypeParam { }");
_builder.newLine();
_builder.append("class ThrowsAndTypeParamProcessor 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 [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("val type = addTypeParameter(\'A\')");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("addParameter(\'myParam\', newTypeReference(type))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setExceptions(newTypeReference(\'java.lang.Exception\'))");
_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("@myannotation.ThrowsAndTypeParam");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def void foo(){");
_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 type = it.getTypeLookup().findClass("myusercode.MyClass");
final MutableMethodDeclaration method = IterableExtensions.head(type.getDeclaredMethods());
Assert.assertEquals("A", IterableExtensions.head(method.getTypeParameters()).getSimpleName());
Assert.assertEquals("myParam", IterableExtensions.head(method.getParameters()).getSimpleName());
Assert.assertSame(IterableExtensions.head(method.getTypeParameters()), IterableExtensions.head(method.getParameters()).getType().getType());
Assert.assertEquals(1, IterableExtensions.size(method.getExceptions()));
};
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 testMovingComputedTypes.
@Test
public void testMovingComputedTypes() {
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.MutableFieldDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(Field2MethodProcessor)");
_builder.newLine();
_builder.append("annotation Field2Method { }");
_builder.newLine();
_builder.append("class Field2MethodProcessor implements TransformationParticipant<MutableFieldDeclaration> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableFieldDeclaration> annotatedFields, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedFields.forEach [ field |");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("field.declaringType.addMethod(field.simpleName) [");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("returnType = field.type");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("body = field.initializer");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("field.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.Field2Method");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("val x = foo");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@myannotation.Field2Method");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("val 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 MutableMethodDeclaration method = ((MutableMethodDeclaration[]) Conversions.unwrapArray(type.getDeclaredMethods(), MutableMethodDeclaration.class))[1];
Assert.assertEquals("java.lang.String", method.getReturnType().getName());
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Aggregations