use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testChangeJavaDoc.
@Test
public void testChangeJavaDoc() {
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(ChangeDocProcessor))");
_builder.newLine();
_builder.append("annotation ChangeDoc{ }");
_builder.newLine();
_builder.append("class ChangeDocProcessor 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.docComment = clazz.docComment.toLowerCase");
_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/ChangeDocAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("/**");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("* MAKE ME LOWER CASE!");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("*/");
_builder_1.newLine();
_builder_1.append("@myannotation.ChangeDoc 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");
Assert.assertEquals("make me lower case!", clazz.getDocComment());
};
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 testAnnotationValueSetting_3.
@Test
public void testAnnotationValueSetting_3() {
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(MoveAnnotationValuesProcessor)");
_builder.newLine();
_builder.append("annotation MoveValues {}");
_builder.newLine();
_builder.newLine();
_builder.append("class MoveAnnotationValuesProcessor 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 classAnnotation = annotatedClass.annotations.head");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val field = annotatedClass.declaredFields.head");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val fieldAnnotation = field.annotations.head");
_builder.newLine();
_builder.append("\t\t");
_builder.append("field.removeAnnotation(fieldAnnotation)");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("field.addAnnotation(fieldAnnotation.annotationTypeDeclaration.qualifiedName.newAnnotationReference [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'booleanValue\', classAnnotation.getValue(\'booleanValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'intValue\', classAnnotation.getValue(\'intValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'longValue\', classAnnotation.getValue(\'longValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'stringValue\', classAnnotation.getValue(\'stringValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'booleanArrayValue\', classAnnotation.getValue(\'booleanArrayValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'intArrayValue\', classAnnotation.getValue(\'intArrayValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'longArrayValue\', classAnnotation.getValue(\'longArrayValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'stringArrayValue\', classAnnotation.getValue(\'stringArrayValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'typeValue\', classAnnotation.getValue(\'typeValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'typeArrayValue\', classAnnotation.getValue(\'typeArrayValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'annotation2Value\', classAnnotation.getValue(\'annotation2Value\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'annotation2ArrayValue\', classAnnotation.getValue(\'annotation2ArrayValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'enumValue\', classAnnotation.getValue(\'enumValue\'))");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("set(\'enumArrayValue\', classAnnotation.getValue(\'enumArrayValue\'))");
_builder.newLine();
_builder.append("\t\t");
_builder.append("])");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/MoveValues.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import test.Annotation");
_builder_1.newLine();
_builder_1.append("import test.Annotation2");
_builder_1.newLine();
_builder_1.append("import myannotation.MoveValues");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@Annotation(");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("intValue = 2 / 2 + 2 * 3 - 4 % 1,");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("longValue = 42 + 4 + 6 * 42 - 4 / 45,");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("stringValue = \'foo\' + \'baz\',");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("booleanArrayValue = #[true, false],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("intArrayValue = #[ -1, 34 + 45, 2 - 6 ],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("longArrayValue = #[42, 5 * -3],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("stringArrayValue = #[\'foo\', \'bla\' + \'buzz\'],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("typeValue = String,");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("typeArrayValue = #[String, Integer],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("annotation2Value = @Annotation2(\'foo\' + \'wuppa\'),");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("annotation2ArrayValue = #[@Annotation2, @Annotation2(\'foo\'+\'wuppa\')],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("enumValue = test.Enum1.YELLOW,");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("enumArrayValue = #[test.Enum1.YELLOW, test.Enum1.RED]");
_builder_1.newLine();
_builder_1.append(")");
_builder_1.newLine();
_builder_1.append("@MoveValues ");
_builder_1.newLine();
_builder_1.append("class UserCode {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@Annotation() String foo");
_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.UserCode");
final AnnotationReference annoRef = IterableExtensions.head(IterableExtensions.head(clazz.getDeclaredFields()).getAnnotations());
Assert.assertEquals(Integer.valueOf((((2 / 2) + (2 * 3)) - (4 % 1))), annoRef.getValue("intValue"));
Assert.assertEquals(Long.valueOf(((long) (((42 + 4) + (6 * 42)) - (4 / 45)))), annoRef.getValue("longValue"));
Assert.assertEquals("foobaz", annoRef.getValue("stringValue"));
Object _value = annoRef.getValue("booleanArrayValue");
final boolean[] bools = ((boolean[]) _value);
Assert.assertTrue(bools[0]);
Assert.assertFalse(bools[1]);
Object _value_1 = annoRef.getValue("intArrayValue");
Assert.assertArrayEquals(new int[] { (-1), (34 + 45), (2 - 6) }, ((int[]) _value_1));
Object _value_2 = annoRef.getValue("typeArrayValue");
final TypeReference[] type = ((TypeReference[]) _value_2);
Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(Integer.class), type[1]);
Object _value_3 = annoRef.getValue("annotation2Value");
final AnnotationReference anno = ((AnnotationReference) _value_3);
Assert.assertEquals("foowuppa", anno.getValue("value"));
Object _value_4 = annoRef.getValue("annotation2ArrayValue");
final AnnotationReference[] annoArray = ((AnnotationReference[]) _value_4);
Assert.assertEquals("HUBBA BUBBA!", annoArray[0].getValue("value"));
Object _value_5 = annoRef.getValue("enumValue");
final EnumerationValueDeclaration enum1 = ((EnumerationValueDeclaration) _value_5);
Assert.assertEquals("YELLOW", enum1.getSimpleName());
Object _value_6 = annoRef.getValue("enumArrayValue");
final EnumerationValueDeclaration[] enumArray = ((EnumerationValueDeclaration[]) _value_6);
Assert.assertEquals("YELLOW", IterableExtensions.<EnumerationValueDeclaration>head(((Iterable<EnumerationValueDeclaration>) Conversions.doWrapArray(enumArray))).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 testTracing3.
@Test
public void testTracing3() {
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.extendedClass");
_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 extendsClause = it.getTracability().getPrimarySourceElement(cls.getExtendedClass());
Assert.assertTrue((extendsClause instanceof TypeReference));
Assert.assertEquals(extendsClause, it.getTracability().getPrimarySourceElement(((Element) 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 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);
}
use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testIntroduceNewTypeAndWorkWithIt.
@Test
public void testIntroduceNewTypeAndWorkWithIt() {
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.RegisterGlobalsContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.RegisterGlobalsParticipant");
_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.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(typeof(NewTypesAddingAnnotationProcessor))");
_builder.newLine();
_builder.append("annotation NewTypesAddingAnnotation { }");
_builder.newLine();
_builder.append("class NewTypesAddingAnnotationProcessor implements RegisterGlobalsParticipant<ClassDeclaration>, TransformationParticipant<MutableClassDeclaration> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doRegisterGlobals(List<? extends ClassDeclaration> sourceClasses, RegisterGlobalsContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (clazz : sourceClasses) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("context.registerClass(clazz.qualifiedName+\"Derived\")");
_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 doTransform(List<? extends MutableClassDeclaration> classes, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("classes.forEach [ ele |");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("val cl = context.findClass(ele.qualifiedName+\"Derived\")");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("cl.extendedClass = newTypeReference(ele)");
_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/NewTypesAddingAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@myannotation.NewTypesAddingAnnotation");
_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 declaredClass = it.getTypeLookup().findClass("myusercode.MyClass");
Assert.assertNotNull(declaredClass);
final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.MyClassDerived");
Assert.assertNotNull(clazz);
Assert.assertEquals(declaredClass.getQualifiedName(), clazz.getExtendedClass().getType().getQualifiedName());
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Aggregations