use of org.eclipse.xtend.lib.macro.declaration.AnnotationReference in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testAnnotationValueSetting_1.
@Test
public void testAnnotationValueSetting_1() {
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.*");
_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("BlackOrWhite color");
_builder.newLine();
_builder.append("\t");
_builder.append("BlackOrWhite[] colors");
_builder.newLine();
_builder.append("\t");
_builder.append("Class<?> type");
_builder.newLine();
_builder.append("\t");
_builder.append("Class<?>[] types");
_builder.newLine();
_builder.append("\t");
_builder.append("SomeAnnotation annotation");
_builder.newLine();
_builder.append("\t");
_builder.append("SomeAnnotation[] annotations");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("annotation SomeAnnotation {");
_builder.newLine();
_builder.append("\t");
_builder.append("boolean value");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("enum BlackOrWhite {");
_builder.newLine();
_builder.append("\t");
_builder.append("BLACK, WHITE");
_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.append("val someAnnotationType = findTypeGlobally(SomeAnnotation)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val enumType = findTypeGlobally(\'myannotation.BlackOrWhite\') as EnumerationTypeDeclaration");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val white = enumType.findDeclaredValue(\'WHITE\')");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val black = enumType.findDeclaredValue(\'BLACK\')");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val existingValue = anno.getValue(\'color\')");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (existingValue != white)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"color\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annoWithColor = annotatedClass.addAnnotation(newAnnotationReference(anno) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setEnumValue(\'color\', black)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(anno)");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val existingColorsValue = annoWithColor.getValue(\'colors\') as Object[]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (existingColorsValue.get(0) != white && existingColorsValue.get(1) != black && existingColorsValue.length != 2)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"colors\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annoWithColors = annotatedClass.addAnnotation(newAnnotationReference(annoWithColor) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setEnumValue(\'colors\', black, white)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(annoWithColor)");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val existingType = annoWithColors.getValue(\'type\')");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (existingType != string)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"type\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annoWithType = annotatedClass.addAnnotation(newAnnotationReference(annoWithColors) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setClassValue(\'type\', annotatedClass.newTypeReference)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(annoWithColors)");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val existingTypes = annoWithType.getValue(\'types\') as Object[]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (existingTypes.get(0) != primitiveInt && existingTypes.get(1) != annotatedClass.newTypeReference && existingTypes.length != 2)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"types\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annoWithTypes = annotatedClass.addAnnotation(newAnnotationReference(annoWithType) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setClassValue(\'types\', primitiveBoolean)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(annoWithType)");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annotationReference = annoWithTypes.getAnnotationValue(\'annotation\')");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (someAnnotationType != annotationReference.annotationTypeDeclaration)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"someAnnotationType != annotationReference.annotationTypeDeclaration\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annoWithAnnotation = annotatedClass.addAnnotation(newAnnotationReference(annoWithTypes) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setAnnotationValue(\'annotation\', ");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("newAnnotationReference(someAnnotationType) [");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("setBooleanValue(\'value\', false)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append(")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(annoWithTypes)");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annotationReferences = annoWithAnnotation.getAnnotationArrayValue(\'annotations\')");
_builder.newLine();
_builder.append("\t\t");
_builder.append("if (annotationReferences.size != 2)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("throw new AssertionError(\"annotationReferences.size != 2\")");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotationReferences.forEach [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("if (someAnnotationType != annotationTypeDeclaration)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("throw new AssertionError(\"someAnnotationType != annotationTypeDeclaration\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.addAnnotation(newAnnotationReference(annoWithAnnotation) [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setAnnotationValue(\'annotations\', ");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("newAnnotationReference(someAnnotationType) [");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("setBooleanValue(\'value\', false)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("],");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("newAnnotationReference(someAnnotationType) [");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("setBooleanValue(\'value\', false)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append(")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedClass.removeAnnotation(annoWithAnnotation)");
_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(");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("color=BlackOrWhite.WHITE, ");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("colors=#[BlackOrWhite.WHITE, BlackOrWhite.BLACK], ");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("type = String, ");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("types=#[Integer, MyClass],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("annotation=@SomeAnnotation(true),");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("annotations=#[@SomeAnnotation(true), @SomeAnnotation(true)]");
_builder_1.newLine();
_builder_1.append(")");
_builder_1.newLine();
_builder_1.append("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");
Type _findTypeGlobally = it.getTypeLookup().findTypeGlobally("myannotation.BlackOrWhite");
final EnumerationTypeDeclaration colorEnum = ((EnumerationTypeDeclaration) _findTypeGlobally);
final Type annotationType = it.getTypeLookup().findTypeGlobally("myannotation.ConfigurableAnnotation");
final AnnotationReference annotation = clazz.findAnnotation(annotationType);
Assert.assertEquals(colorEnum.findDeclaredValue("BLACK"), annotation.getValue("color"));
Object _value = annotation.getValue("colors");
final Object[] colors = ((Object[]) _value);
Assert.assertEquals(2, colors.length);
Assert.assertEquals(colorEnum.findDeclaredValue("BLACK"), colors[0]);
Assert.assertEquals(colorEnum.findDeclaredValue("WHITE"), colors[1]);
Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(clazz), annotation.getValue("type"));
final TypeReference[] types = annotation.getClassArrayValue("types");
Assert.assertEquals(1, types.length);
Assert.assertEquals(it.getTypeReferenceProvider().getPrimitiveBoolean(), types[0]);
final Type someAnnotationType = it.getTypeLookup().findTypeGlobally("myannotation.SomeAnnotation");
final AnnotationReference annotationValue = annotation.getAnnotationValue("annotation");
Assert.assertNotNull(annotationValue);
Assert.assertEquals(someAnnotationType, annotationValue.getAnnotationTypeDeclaration());
Assert.assertFalse(annotationValue.getBooleanValue("value"));
final AnnotationReference[] annotationsValue = annotation.getAnnotationArrayValue("annotations");
Assert.assertNotNull(annotationsValue);
Assert.assertEquals(2, ((List<AnnotationReference>) Conversions.doWrapArray(annotationsValue)).size());
final Consumer<AnnotationReference> _function_1 = (AnnotationReference it_1) -> {
Assert.assertEquals(someAnnotationType, annotationValue.getAnnotationTypeDeclaration());
Assert.assertFalse(annotationValue.getBooleanValue("value"));
};
((List<AnnotationReference>) Conversions.doWrapArray(annotationsValue)).forEach(_function_1);
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
use of org.eclipse.xtend.lib.macro.declaration.AnnotationReference 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.lib.macro.declaration.AnnotationReference in project xtext-core by eclipse.
the class TracedAccessorsProcessor method doTransform.
@Override
public void doTransform(MutableClassDeclaration annotatedClass, @Extension TransformationContext context) {
annotatedClass.setExtendedClass(context.newTypeReference(TracingSugar.class));
TypeReference iterableType = context.newTypeReference(Iterable.class, context.newWildcardTypeReference());
TypeReference annotationType = context.newTypeReference(TracedAccessors.class);
AnnotationReference annotation = annotatedClass.findAnnotation(annotationType.getType());
TypeReference[] factories = annotation == null ? null : annotation.getClassArrayValue("value");
if (factories == null)
return;
for (InterfaceDeclaration f : Iterables.filter(Lists.transform(Arrays.asList(factories), it -> it.getType()), InterfaceDeclaration.class)) {
for (TypeReference t : FluentIterable.from(f.getDeclaredMethods()).filter(it -> it.getSimpleName().startsWith("create") && Iterables.isEmpty(it.getParameters())).transform(MethodDeclaration::getReturnType).toSortedList(Comparator.comparing(TypeReference::getSimpleName))) {
for (ResolvedMethod getter : FluentIterable.from(t.getAllResolvedMethods()).filter(it -> isSupportedGetter(it)).filter(it -> !iterableType.isAssignableFrom(it.getDeclaration().getReturnType())).toSortedList(Comparator.comparing(ResolvedMethod::getSimpleSignature))) {
TypeReference rt = getter.getResolvedReturnType();
if (TracedAccessorsProcessor.TYPES_WITH_GOOD_TO_STRING.contains(rt.getType().getSimpleName().toLowerCase())) {
annotatedClass.addMethod(tracerName(getter), it -> {
it.setReturnType(context.newTypeReference(IGeneratorNode.class));
it.addParameter("target", t);
it.setBody(new StringConcatenationClient() {
@Override
protected void appendTo(TargetStringConcatenation builder) {
builder.append(EStructuralFeature.class);
builder.append(" feature = target.eClass().getEStructuralFeature(\"");
builder.append(TracedAccessorsProcessor.this.featureName(getter));
builder.append("\");");
builder.newLineIfNotEmpty();
builder.append(ILocationData.class);
builder.append(" location = this.location(target, feature, -1);");
builder.newLineIfNotEmpty();
builder.append(CompositeGeneratorNode.class);
builder.append(" trace = this.trace(location);");
builder.newLineIfNotEmpty();
builder.append("this.append(trace, target.");
builder.append(getter.getDeclaration().getSimpleName());
builder.append("());");
builder.newLineIfNotEmpty();
builder.append("return trace;");
builder.newLine();
}
});
});
annotatedClass.addMethod(tracerName(getter), it -> {
it.setReturnType(context.newTypeReference(IGeneratorNode.class));
it.addParameter("target", t);
it.addParameter("useForDebugging", context.newTypeReference(Boolean.TYPE));
it.setBody(new StringConcatenationClient() {
@Override
protected void appendTo(TargetStringConcatenation builder) {
builder.append(EStructuralFeature.class);
builder.append(" feature = target.eClass().getEStructuralFeature(\"");
builder.append(TracedAccessorsProcessor.this.featureName(getter));
builder.append("\");");
builder.newLineIfNotEmpty();
builder.append(ILocationData.class);
builder.append(" location = this.location(target, feature, -1);");
builder.newLineIfNotEmpty();
builder.append(CompositeGeneratorNode.class);
builder.append(" trace = this.trace(location, useForDebugging);");
builder.newLineIfNotEmpty();
builder.append("this.append(trace, target.");
builder.append(getter.getDeclaration().getSimpleName());
builder.append("());");
builder.newLineIfNotEmpty();
builder.append("return trace;");
builder.newLine();
}
});
});
}
annotatedClass.addMethod(this.tracerName(getter), (MutableMethodDeclaration it) -> {
it.setReturnType(context.newTypeReference(IGeneratorNode.class));
it.addParameter("target", t);
it.addParameter("stringProvider", context.newTypeReference(Function.class, rt, context.getString()));
it.setBody(new StringConcatenationClient() {
@Override
protected void appendTo(TargetStringConcatenation builder) {
builder.append(EStructuralFeature.class);
builder.append(" feature = target.eClass().getEStructuralFeature(\"");
builder.append(TracedAccessorsProcessor.this.featureName(getter));
builder.append("\");");
builder.newLineIfNotEmpty();
builder.append(ILocationData.class);
builder.append(" location = this.location(target, feature, -1);");
builder.newLineIfNotEmpty();
builder.append(CompositeGeneratorNode.class);
builder.append(" trace = this.trace(location);");
builder.newLineIfNotEmpty();
builder.append("this.append(trace, stringProvider.apply(target.");
builder.append(getter.getDeclaration().getSimpleName());
builder.append("()));");
builder.newLineIfNotEmpty();
builder.append("return trace;");
builder.newLine();
}
});
});
}
}
}
}
Aggregations