use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration 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.lib.macro.declaration.MutableClassDeclaration in project xtext-core by eclipse.
the class EmfAdaptableProcessor method doTransform.
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
final MutableClassDeclaration adapterClass = context.findClass(this.getAdapterClassName(annotatedClass));
final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
it.addParameter("emfObject", context.newTypeReference(Notifier.class));
it.setReturnType(context.newTypeReference(annotatedClass));
it.setStatic(true);
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("for (");
_builder.append(Adapter.class);
_builder.append(" adapter : emfObject.eAdapters()) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("if (adapter instanceof ");
_builder.append(adapterClass, "\t");
_builder.append(") {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("return ((");
_builder.append(adapterClass, "\t\t");
_builder.append(") adapter).get();");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.append("return null;");
_builder.newLine();
}
};
it.setBody(_client);
};
annotatedClass.addMethod("findInEmfObject", _function);
final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it) -> {
it.addParameter("emfObject", context.newTypeReference(Notifier.class));
it.setReturnType(context.newTypeReference(annotatedClass));
it.setStatic(true);
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append(List.class);
_builder.append("<");
_builder.append(Adapter.class);
_builder.append("> adapters = emfObject.eAdapters();");
_builder.newLineIfNotEmpty();
_builder.append("for(int i = 0, max = adapters.size(); i < max; i++) {");
_builder.newLine();
_builder.append("\t");
_builder.append(Adapter.class, "\t");
_builder.append(" adapter = adapters.get(i);");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("if (adapter instanceof ");
_builder.append(adapterClass, "\t");
_builder.append(") {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("emfObject.eAdapters().remove(i);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return ((");
_builder.append(adapterClass, "\t\t");
_builder.append(") adapter).get();");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.append("return null;");
_builder.newLine();
}
};
it.setBody(_client);
};
annotatedClass.addMethod("removeFromEmfObject", _function_1);
final Procedure1<MutableMethodDeclaration> _function_2 = (MutableMethodDeclaration it) -> {
it.addParameter("emfObject", context.newTypeReference(Notifier.class));
it.setReturnType(context.getPrimitiveVoid());
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
String _simpleName = annotatedClass.getSimpleName();
_builder.append(_simpleName);
_builder.append(" result = findInEmfObject(emfObject);");
_builder.newLineIfNotEmpty();
_builder.append("if (result != null)");
_builder.newLine();
_builder.append("\t");
_builder.append("throw new IllegalStateException(\"The given EMF object already contains an adapter for ");
String _simpleName_1 = annotatedClass.getSimpleName();
_builder.append(_simpleName_1, "\t");
_builder.append("\");");
_builder.newLineIfNotEmpty();
_builder.append(adapterClass);
_builder.append(" adapter = new ");
_builder.append(adapterClass);
_builder.append("(this);");
_builder.newLineIfNotEmpty();
_builder.append("emfObject.eAdapters().add(adapter);");
_builder.newLine();
}
};
it.setBody(_client);
};
annotatedClass.addMethod("attachToEmfObject", _function_2);
adapterClass.setExtendedClass(context.newTypeReference(AdapterImpl.class));
final Procedure1<MutableFieldDeclaration> _function_3 = (MutableFieldDeclaration it) -> {
it.setType(context.newTypeReference(annotatedClass));
};
adapterClass.addField("element", _function_3);
final Procedure1<MutableConstructorDeclaration> _function_4 = (MutableConstructorDeclaration it) -> {
it.addParameter("element", context.newTypeReference(annotatedClass));
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("this.element = element;");
_builder.newLine();
}
};
it.setBody(_client);
};
adapterClass.addConstructor(_function_4);
final Procedure1<MutableMethodDeclaration> _function_5 = (MutableMethodDeclaration it) -> {
it.setReturnType(context.newTypeReference(annotatedClass));
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("return this.element;");
_builder.newLine();
}
};
it.setBody(_client);
};
adapterClass.addMethod("get", _function_5);
final Procedure1<MutableMethodDeclaration> _function_6 = (MutableMethodDeclaration it) -> {
it.addAnnotation(context.newAnnotationReference(Override.class));
it.addParameter("object", context.newTypeReference(Object.class));
it.setReturnType(context.getPrimitiveBoolean());
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("return object == ");
_builder.append(annotatedClass);
_builder.append(".class;");
_builder.newLineIfNotEmpty();
}
};
it.setBody(_client);
};
adapterClass.addMethod("isAdapterForType", _function_6);
}
use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testInferredMethodReturnType.
@Test
public void testInferredMethodReturnType() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.AbstractMethodProcessor");
_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.MutableMethodDeclaration");
_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 extends AbstractMethodProcessor {");
_builder.newLine();
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(MutableMethodDeclaration annotatedMethod, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedMethod.declaringType.addField(annotatedMethod.simpleName + \'_field\') [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("type = annotatedMethod.returnType");
_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("class Client {");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("def bar() {");
_builder_1.newLine();
_builder_1.append("\t\t");
_builder_1.append("1");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@MyAnnotation");
_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 foo = it.getTypeLookup().findClass("myusercode.Client");
final TypeReference barType = foo.findDeclaredField("bar_field").getType();
this.assertSameType(barType, it.getTypeReferenceProvider().getPrimitiveInt());
this.assertSameType(barType, foo.findDeclaredMethod("bar").getReturnType());
final TypeReference fooType = foo.findDeclaredField("foo_field").getType();
this.assertSameType(fooType, it.getTypeReferenceProvider().newTypeReference(Integer.class));
this.assertSameType(fooType, foo.findDeclaredMethod("foo").getReturnType());
};
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 testValidation.
@Test
public void testValidation() {
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.MutableNamedElement");
_builder.newLine();
_builder.append("import org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(typeof(ValidatedProcessor))");
_builder.newLine();
_builder.append("annotation Validated { }");
_builder.newLine();
_builder.append("class ValidatedProcessor implements TransformationParticipant<MutableNamedElement> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableNamedElement> annotatedMethods, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotatedMethods.forEach [ ele |");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("switch ele {");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("MutableFieldDeclaration : ele.addWarning(\'field-warning\')");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("default : ele.addWarning(\'warning\')");
_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/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.Validated");
_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("\t");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("@myannotation.Validated");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("String name");
_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());
final MutableFieldDeclaration field = IterableExtensions.head(type.getDeclaredFields());
Assert.assertEquals("field-warning", IterableExtensions.head(it.getProblemSupport().getProblems(field)).getMessage());
Assert.assertEquals("warning", IterableExtensions.head(it.getProblemSupport().getProblems(method)).getMessage());
};
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 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);
}
Aggregations