use of org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration 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.MutableMethodDeclaration 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.MutableMethodDeclaration 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.MutableMethodDeclaration 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.MutableMethodDeclaration 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);
}
Aggregations