use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testAddConstructor.
@Test
public void testAddConstructor() {
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(AddConstructorProcessor))");
_builder.newLine();
_builder.append("annotation AddConstructor{ }");
_builder.newLine();
_builder.append("class AddConstructorProcessor 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.addConstructor [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("addParameter(\"foo\", 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();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AddConstructorAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@myannotation.AddConstructor 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(1, IterableExtensions.size(clazz.getDeclaredConstructors()));
Assert.assertEquals("foo", IterableExtensions.head(IterableExtensions.head(clazz.getDeclaredConstructors()).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 testAddDefaultConstructorWithTemplate.
@Test
public void testAddDefaultConstructorWithTemplate() {
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(AddConstructorProcessor))");
_builder.newLine();
_builder.append("annotation AddConstructor{ }");
_builder.newLine();
_builder.append("class AddConstructorProcessor 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.addConstructor [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("body=");
_builder.append("\'\'\'", "\t\t\t");
_builder.append("System.out.println(\"Hello World\");");
_builder.append("\'\'\'", "\t\t\t");
_builder.newLineIfNotEmpty();
_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/AddConstructorAnnotation.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@myannotation.AddConstructor 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) -> {
it.getCompilationUnit();
final MutableClassDeclaration clazz = it.getTypeLookup().findClass("myusercode.MyClass");
Assert.assertEquals(1, IterableExtensions.size(clazz.getDeclaredConstructors()));
Assert.assertTrue(IterableExtensions.isEmpty(IterableExtensions.head(clazz.getDeclaredConstructors()).getParameters()));
};
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 testInferredTypeReferences.
@Test
public void testInferredTypeReferences() {
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.ClassDeclaration");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(MyAnnotationProcessor)");
_builder.newLine();
_builder.append("annotation MyAnnotation {");
_builder.newLine();
_builder.append("\t");
_builder.append("Class<?> value");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class MyAnnotationProcessor extends AbstractClassProcessor {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(MutableClassDeclaration it, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (field : declaredFields) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("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 = [\'return 1;\']");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t\t");
_builder.append("val myAnnotation = findAnnotation(MyAnnotation.findTypeGlobally)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val type = myAnnotation.getClassValue(\"value\").type as ClassDeclaration");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (field : type.declaredFields) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("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 = [\'return 1;\']");
_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.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("import myannotation.*");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation(Bar)");
_builder_1.newLine();
_builder_1.append("class Foo {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("val y = 1");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("class Bar {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("val x = 1");
_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 foo = it.getTypeLookup().findClass("myusercode.Foo");
final MutableClassDeclaration bar = it.getTypeLookup().findClass("myusercode.Bar");
this.assertSameType(foo.findDeclaredField("y").getType(), foo.findDeclaredMethod("y").getReturnType());
this.assertSameType(bar.findDeclaredField("x").getType(), foo.findDeclaredMethod("x").getReturnType());
};
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 testFileSystemSupport_01.
@Ignore("FIXME: Issue 1311 - Make system encoding independent")
@Test
public void testFileSystemSupport_01() {
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/FileSystemSupportTest.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.RegisterGlobalsContext\n\t\t\t\timport org.eclipse.xtend.lib.macro.RegisterGlobalsParticipant\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.ClassDeclaration\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration\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.AbstractClassProcessor\n\t\t\t\t\n\t\t\t\t@Active(FileSystemUsingProcessor)\n\t\t\t\tannotation FileSystemSupportTest { }\n\t\t\t\t\n\t\t\t\tclass FileSystemUsingProcessor extends AbstractClassProcessor {\n\t\n\t\t\t\t\toverride doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {\n\t\t\t\t\t\tval path = annotatedClass.compilationUnit.filePath\n\t\t\t\t\t\tannotatedClass.docComment = \'\'\'\n\t\t\t\t\t\t\tPath \'�path.toString�\' {\n\t\t\t\t\t\t\t\texists: �path.exists�\n\t\t\t\t\t\t\t\tisFolder: �path.isFolder�\n\t\t\t\t\t\t\t\tisFile: �path.isFile�\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tsourceFolder : �path.sourceFolder�\n\t\t\t\t\t\t\ttargetFolder : �path.targetFolder�\n\t\t\t\t\t\t\tprojectFolder: �path.projectFolder�\n\t\t\t\t\t\t\'\'\'\n\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("@myannotation.FileSystemSupportTest");
_builder.newLine();
_builder.append("class MyClass {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("myusercode/UserCode.xtend", _builder.toString());
final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
final MutableClassDeclaration declaredClass = it.getTypeLookup().findClass("myusercode.MyClass");
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("Path \'/userProject/src/myusercode/UserCode.xtend\' {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("exists: true");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("isFolder: false");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("isFile: true");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.append("sourceFolder : /userProject/src");
_builder_1.newLine();
_builder_1.append("targetFolder : /userProject/xtend-gen");
_builder_1.newLine();
_builder_1.append("projectFolder: /userProject");
_builder_1.newLine();
Assert.assertEquals(_builder_1.toString(), declaredClass.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 testAccessAndModifyEnumerationValueDeclaration.
@Test
public void testAccessAndModifyEnumerationValueDeclaration() {
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.CodeGenerationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.CodeGenerationParticipant");
_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.TransformationContext");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.TransformationParticipant");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.EnumerationTypeDeclaration");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.TypeDeclaration");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.Visibility");
_builder.newLine();
_builder.newLine();
_builder.append("import static com.google.common.base.Preconditions.*");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(MyAnnotationProcessor)");
_builder.newLine();
_builder.append("annotation MyAnnotation {");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class MyAnnotationProcessor implements RegisterGlobalsParticipant<EnumerationTypeDeclaration>, TransformationParticipant<MutableEnumerationTypeDeclaration>, CodeGenerationParticipant<EnumerationTypeDeclaration> {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doGenerateCode(List<? extends EnumerationTypeDeclaration> annotatedSourceElements,");
_builder.newLine();
_builder.append("\t\t");
_builder.append("extension CodeGenerationContext context) {");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doRegisterGlobals(List<? extends EnumerationTypeDeclaration> annotatedSourceElements,");
_builder.newLine();
_builder.append("\t\t");
_builder.append("RegisterGlobalsContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (enumeration : annotatedSourceElements) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("enumeration.checkState");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("def checkState(EnumerationTypeDeclaration enumeration) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val values = enumeration.declaredValues");
_builder.newLine();
_builder.append("\t\t");
_builder.append("checkState(values.size == 3, \"enumeration.declaredValues.size != 3\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("enumeration.findDeclaredValue(values.get(0).simpleName).checkState(\"A\", enumeration)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("enumeration.findDeclaredValue(values.get(1).simpleName).checkState(\"B\", enumeration)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("enumeration.findDeclaredValue(values.get(2).simpleName).checkState(\"C\", enumeration)");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("def checkState(EnumerationValueDeclaration value, String expectedSimpleName, TypeDeclaration expectedType) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("checkState(value.simpleName == expectedSimpleName, \"value.simpleName != expectedSimpleName\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("checkState(value.declaringType == expectedType, \"value.declaringType != expectedType\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("checkState(value.visibility == Visibility.PUBLIC, \"value.visibility != Visibility.PUBLIC\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("checkState(value.annotations.size == 0, \"value.annotations.size != 0\")");
_builder.newLine();
_builder.append("\t\t");
_builder.append("checkState(value.findAnnotation(null) == null, \"value.findAnnotation(null) != null\")");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableEnumerationTypeDeclaration> annotatedTargetElements,");
_builder.newLine();
_builder.append("\t\t");
_builder.append("extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for (enumeration : annotatedTargetElements) {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("enumeration.checkState");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("for (value : enumeration.declaredValues) {");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("checkState(value.annotations.size == 0, value.annotations.size != 0)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("value.addAnnotation(Deprecated.newAnnotationReference)");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("checkState(value.annotations.size == 1, value.annotations.size != 1)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("enumeration.addValue(\"D\") [");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("addAnnotation(Deprecated.newAnnotationReference)");
_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.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("import myannotation.MyAnnotation");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("enum UserCode {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("A, ");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("B,");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("C");
_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 MutableEnumerationTypeDeclaration enumerationType = it.getTypeLookup().findEnumerationType("myusercode.UserCode");
Assert.assertEquals(4, IterableExtensions.size(enumerationType.getDeclaredValues()));
final Type deprecatedAnnotationType = it.getTypeReferenceProvider().newTypeReference(Deprecated.class).getType();
Iterable<? extends MutableEnumerationValueDeclaration> _declaredValues = enumerationType.getDeclaredValues();
for (final MutableEnumerationValueDeclaration value : _declaredValues) {
{
Assert.assertEquals(1, IterableExtensions.size(value.getAnnotations()));
Assert.assertNotNull(value.findAnnotation(deprecatedAnnotationType));
}
}
Assert.assertEquals("D", IterableExtensions.last(enumerationType.getDeclaredValues()).getSimpleName());
Assert.assertNotNull(enumerationType.findDeclaredValue("D"));
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Aggregations