use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testPropertyAnnotation.
@Test
public void testPropertyAnnotation() {
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/PropertyAnnotation.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.TransformationContext\n\t\t\t\timport org.eclipse.xtend.lib.macro.TransformationParticipant\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration\n\t\t\t\timport org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration\n\n\t\t\t\t@Active(typeof(PropertyProcessor))\n\t\t\t\tannotation Property2 { }\n\t\t\t\tclass PropertyProcessor implements TransformationParticipant<MutableFieldDeclaration> {\n\t\t\t\t\t\n\t\t\t\t\toverride doTransform(List<? extends MutableFieldDeclaration> annotatedTargetFields, extension TransformationContext context) {\n\t\t\t\t\t\tannotatedTargetFields.forEach [ field |\n\t\t\t\t\t\t\tval declaringType = field.declaringType \n\t\t\t\t\t\t\tdeclaringType.addMethod(field.getterName) [\n\t\t\t\t\t\t\t\treturnType = field.type\n\t\t\t\t\t\t\t\tbody = [\'\'\'\n\t\t\t\t\t\t\t\t\treturn this.�field.simpleName�;\n\t\t\t\t\t\t\t\t\'\'\']\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\tdeclaringType.addMethod(\'set\'+field.simpleName.toFirstUpper) [\n\t\t\t\t\t\t\t\taddParameter(field.simpleName, field.type)\n\t\t\t\t\t\t\t\tbody = [\'\'\'\n\t\t\t\t\t\t\t\t\tthis.�field.simpleName� = �field.simpleName�;\n\t\t\t\t\t\t\t\t\'\'\']\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t]\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tdef private String getterName(MutableFieldDeclaration field) {\n\t\t\t\t\t\treturn \'get\'+field.simpleName.toFirstUpper\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("class MyClass {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("@myannotation.Property2 String myField");
_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 clazz = it.getTypeLookup().findClass("myusercode.MyClass");
final MutableMethodDeclaration getter = IterableExtensions.head(clazz.getDeclaredMethods());
Assert.assertEquals("getMyField", getter.getSimpleName());
Assert.assertEquals("String", getter.getReturnType().toString());
final MutableMethodDeclaration setter = clazz.findDeclaredMethod("setMyField", getter.getReturnType());
Assert.assertEquals("setMyField", setter.getSimpleName());
Assert.assertEquals("void", setter.getReturnType().toString());
Assert.assertEquals("myField", IterableExtensions.head(setter.getParameters()).getSimpleName());
Assert.assertEquals("String", IterableExtensions.head(setter.getParameters()).getType().toString());
};
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 testNoMutationInValidationPhase.
@Test
public void testNoMutationInValidationPhase() {
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.TransformationParticipant");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.ValidationParticipant");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.ValidationContext");
_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.newLine();
_builder.append("@Active(EvilProcessor)");
_builder.newLine();
_builder.append("annotation EvilAnnotation {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class EvilProcessor implements TransformationParticipant<MutableClassDeclaration>, ValidationParticipant<MutableClassDeclaration> {");
_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[");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("final = false");
_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 doValidate(List<? extends MutableClassDeclaration> classes, extension ValidationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("classes.forEach[");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("final = true");
_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/EvilAnnotation.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("@EvilAnnotation");
_builder_1.newLine();
_builder_1.append("class 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 Function1<Resource.Diagnostic, Boolean> _function_1 = (Resource.Diagnostic it_1) -> {
return Boolean.valueOf(it_1.getMessage().contains("cannot be modified"));
};
IterableExtensions.<Resource.Diagnostic>exists(it.getXtendFile().eResource().getErrors(), _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 testAnnotationArrayValueGetting_02.
@Test
public void testAnnotationArrayValueGetting_02() {
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.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.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 doRegisterGlobals(ClassDeclaration it, extension RegisterGlobalsContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("check");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(MutableClassDeclaration it, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("check");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("def check(ClassDeclaration it) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("switch qualifiedName {");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("case \'myusercode.UserCode\',");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("case \'myusercode.UserCode2\',");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("case \'myusercode.UserCode4\': {");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("val value = annotations.head.getClassArrayValue(\"value\")");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("if (value.size != 1) {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("throw new AssertionError(\"value.size != 1\")");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("if (value.head.name != \'java.lang.String\') {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("throw new AssertionError(\"value.head.name != \'java.lang.String\'\")");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("annotations.head.getClassValue(\"value\")");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("throw new AssertionError(\'annotations.head.getClassValue(\"value\")\')");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("} catch (Exception e) {");
_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\t");
_builder.append("case \'myusercode.UserCode3\',");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("case \'myusercode.UserCode5\': {");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("val value = annotations.head.getClassArrayValue(\"value\")");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("if (value.size != 2) {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("throw new AssertionError(\"value.size != 2\")");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("if (value.head.name != \'java.lang.String\') {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("throw new AssertionError(\"value.head.name != \'java.lang.String\'\")");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("if (value.last.name != \'java.lang.Integer\') {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("throw new AssertionError(\"value.last.name != \'java.lang.Integer\'\")");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("try {");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("annotations.head.getClassValue(\"value\")");
_builder.newLine();
_builder.append("\t\t\t\t\t");
_builder.append("throw new AssertionError(\'annotations.head.getClassValue(\"value\")\')");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("} catch (Exception e) {");
_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\t");
_builder.append("default:");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("throw new AssertionError(\'Unexpected type: \' + qualifiedName)");
_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(String)");
_builder_1.newLine();
_builder_1.append("class UserCode {}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation(#[String])");
_builder_1.newLine();
_builder_1.append("class UserCode2 {}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation(String, Integer)");
_builder_1.newLine();
_builder_1.append("class UserCode3 {}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation(value=String)");
_builder_1.newLine();
_builder_1.append("class UserCode4 {");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@MyAnnotation(value=#[String, Integer])");
_builder_1.newLine();
_builder_1.append("class UserCode5 {");
_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) -> {
};
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 testSwapExpressions.
@Test
public void testSwapExpressions() {
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(SwapProcessor))");
_builder.newLine();
_builder.append("annotation Swap{ }");
_builder.newLine();
_builder.append("class SwapProcessor implements TransformationParticipant<MutableMethodDeclaration> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableMethodDeclaration> methods, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val b1 = methods.get(0).body");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val b2 = methods.get(1).body");
_builder.newLine();
_builder.append("\t\t");
_builder.append("methods.get(0).body = b2");
_builder.newLine();
_builder.append("\t\t");
_builder.append("methods.get(1).body = b1");
_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/SwapAnnotation.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.Swap def foo(String a) {");
_builder_1.newLine();
_builder_1.append("\t\t");
_builder_1.append("return b");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@myannotation.Swap def bar(String b) {");
_builder_1.newLine();
_builder_1.append("\t\t");
_builder_1.append("return a");
_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) -> {
};
this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
use of org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl in project xtext-xtend by eclipse.
the class ActiveAnnotationsProcessingInIDETest method assertProcessing.
@Override
public void assertProcessing(final Pair<String, String> macroContent, final Pair<String, String> clientContent, final Procedure1<? super CompilationUnitImpl> expectations) {
try {
this.macroFile = this.newSource(ActiveAnnotationsProcessingInIDETest.macroProject, macroContent.getKey(), macroContent.getValue().toString());
final int lidx = macroContent.getKey().lastIndexOf("/");
if ((lidx != (-1))) {
this.exportedPackage = macroContent.getKey().substring(0, lidx).replace("/", ".");
boolean _addExportedPackages = WorkbenchTestHelper.addExportedPackages(ActiveAnnotationsProcessingInIDETest.macroProject.getProject(), this.exportedPackage);
if (_addExportedPackages) {
IResourcesSetupUtil.reallyWaitForAutoBuild();
}
}
this.clientFile = this.newSource(ActiveAnnotationsProcessingInIDETest.userProject, clientContent.getKey(), clientContent.getValue().toString());
IResourcesSetupUtil.waitForBuild();
final ResourceSet resourceSet = this.resourceSetProvider.get(ActiveAnnotationsProcessingInIDETest.userProject.getProject());
final Resource resource = resourceSet.getResource(URI.createPlatformResourceURI(this.clientFile.getFullPath().toString(), true), true);
EcoreUtil2.resolveLazyCrossReferences(resource, CancelIndicator.NullImpl);
this.validator.validate(resource, CheckMode.FAST_ONLY, CancelIndicator.NullImpl);
final CompilationUnitImpl unit = this.compilationUnitProvider.get();
unit.setXtendFile(IterableExtensions.<XtendFile>head(Iterables.<XtendFile>filter(resource.getContents(), XtendFile.class)));
expectations.apply(unit);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
Aggregations