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 testAnnotationDefaultValues_01.
@Test
public void testAnnotationDefaultValues_01() {
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("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);
}
use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testAnnotationValueSetting_2.
@Test
public void testAnnotationValueSetting_2() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.*");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.*");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(ConfigurableAnnotationProcessor)");
_builder.newLine();
_builder.append("annotation ConfigurableAnnotation {");
_builder.newLine();
_builder.append("\t");
_builder.append("int someValue");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class Constants {");
_builder.newLine();
_builder.append("\t");
_builder.append("public static val int MYCONSTANT = Integer.MAX_VALUE - 42");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class ConfigurableAnnotationProcessor extends AbstractClassProcessor {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val anno = annotatedClass.annotations.head");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val existingValue = anno.getValue(\'someValue\')");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.docComment = \'\'+existingValue");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/ConfigurableAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import myannotation.*");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@ConfigurableAnnotation(someValue=MoreConstants.MY_CONSTANT * 1)");
_builder_1.newLine();
_builder_1.append("class MyClass {");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("class MoreConstants {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("public static val int MY_CONSTANT = myannotation.Constants.MYCONSTANT - Integer.MAX_VALUE + 42 * 2");
_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("42", 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 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.TransformationContext");
_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.newLine();
_builder.append("@Active(typeof(ParamProcessor))");
_builder.newLine();
_builder.append("annotation Param { }");
_builder.newLine();
_builder.append("class ParamProcessor implements TransformationParticipant<MutableParameterDeclaration> {");
_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("}");
_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));
};
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 testTracing.
@Test
public void testTracing() {
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.Active");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(GettersProcessor)");
_builder.newLine();
_builder.append("annotation Getters {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class GettersProcessor extends AbstractClassProcessor {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("cls.declaredFields.filter[isThePrimaryGeneratedJavaElement].forEach [field|");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("cls.addMethod(\"get\" + field.simpleName.toFirstUpper) [");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("primarySourceElement = field");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("returnType = field.type");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("body = [\"return this.\" + field.simpleName + \";\"]");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("field.markAsRead");
_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/Getters.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import myannotation.Getters");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@Getters");
_builder_1.newLine();
_builder_1.append("class Client {");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("val int bar = 1");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def create new Integer(1) foo() {");
_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) -> {
final MutableClassDeclaration cls = it.getTypeLookup().findClass("myusercode.Client");
final MutableFieldDeclaration barField = cls.findDeclaredField("bar");
final Function1<MutableMethodDeclaration, Boolean> _function_1 = (MutableMethodDeclaration it_1) -> {
return Boolean.valueOf(it_1.getSimpleName().startsWith("get"));
};
final Iterable<? extends MutableMethodDeclaration> getters = IterableExtensions.filter(cls.getDeclaredMethods(), _function_1);
Assert.assertEquals(1, IterableExtensions.size(getters));
final MutableMethodDeclaration getBar = IterableExtensions.head(getters);
Assert.assertEquals("getBar", getBar.getSimpleName());
final JvmField barJvmField = ((MutableJvmFieldDeclarationImpl) barField).getDelegate();
final JvmOperation getBarJvmMethod = ((MutableJvmMethodDeclarationImpl) getBar).getDelegate();
final Set<EObject> elementsAssociatedWithBarField = it.getJvmModelAssociations().getJvmElements(it.getJvmModelAssociations().getPrimarySourceElement(barJvmField));
Assert.assertEquals(2, elementsAssociatedWithBarField.size());
Assert.assertEquals(barJvmField, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[0]);
Assert.assertEquals(getBarJvmMethod, ((Object[]) Conversions.unwrapArray(elementsAssociatedWithBarField, Object.class))[1]);
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Aggregations