use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.
the class AbstractReusableActiveAnnotationTests method testSetEmptyListAsAnnotationValue.
@Test
public void testSetEmptyListAsAnnotationValue() {
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.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("annotation Values {");
_builder.newLine();
_builder.append("\t");
_builder.append("int[] intValue");
_builder.newLine();
_builder.append("\t");
_builder.append("long[] longValue");
_builder.newLine();
_builder.append("\t");
_builder.append("short[] shortValue");
_builder.newLine();
_builder.append("\t");
_builder.append("double[] doubleValue");
_builder.newLine();
_builder.append("\t");
_builder.append("float[] floatValue");
_builder.newLine();
_builder.append("\t");
_builder.append("byte[] byteValue");
_builder.newLine();
_builder.append("\t");
_builder.append("boolean[] booleanValue");
_builder.newLine();
_builder.append("\t");
_builder.append("char[] charValue");
_builder.newLine();
_builder.append("\t");
_builder.append("String[] stringValue");
_builder.newLine();
_builder.append("\t");
_builder.append("Class<?>[] classValue");
_builder.newLine();
_builder.append("\t");
_builder.append("Enum1[] enumValue");
_builder.newLine();
_builder.append("\t");
_builder.append("Annotation2[] annotationValue");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("enum Enum1 {}");
_builder.newLine();
_builder.append("annotation Annotation2 {}");
_builder.newLine();
_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("removeAnnotation(annotations.head)");
_builder.newLine();
_builder.append("\t\t");
_builder.append("addAnnotation(Values.newAnnotationReference [");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setIntValue(\'intValue\', emptyList)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setLongValue(\'longValue\', emptyList)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setShortValue(\'shortValue\', emptyList)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setDoubleValue(\'doubleValue\', emptyList)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setFloatValue(\'floatValue\', emptyList)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setByteValue(\'byteValue\', emptyList)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setBooleanValue(\'booleanValue\', emptyList)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setCharValue(\'charValue\', emptyList)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setStringValue(\'stringValue\', emptyList)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setClassValue(\'classValue\', emptyList)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setEnumValue(\'enumValue\', emptyList)");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("setAnnotationValue(\'annotationValue\', emptyList)");
_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");
_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 MutableClassDeclaration foo = it.getTypeLookup().findClass("myusercode.Foo");
final Type annotationType = it.getTypeLookup().findTypeGlobally("myannotation.Values");
final AnnotationReference values = foo.findAnnotation(annotationType);
Assert.assertEquals(0, ((List<Integer>) Conversions.doWrapArray(values.getIntArrayValue("intValue"))).size());
Assert.assertEquals(0, ((List<Long>) Conversions.doWrapArray(values.getLongArrayValue("longValue"))).size());
Assert.assertEquals(0, ((List<Short>) Conversions.doWrapArray(values.getShortArrayValue("shortValue"))).size());
Assert.assertEquals(0, ((List<Double>) Conversions.doWrapArray(values.getDoubleArrayValue("doubleValue"))).size());
Assert.assertEquals(0, ((List<Float>) Conversions.doWrapArray(values.getFloatArrayValue("floatValue"))).size());
Assert.assertEquals(0, ((List<Byte>) Conversions.doWrapArray(values.getByteArrayValue("byteValue"))).size());
Assert.assertEquals(0, ((List<Boolean>) Conversions.doWrapArray(values.getBooleanArrayValue("booleanValue"))).size());
Assert.assertEquals(0, ((List<Character>) Conversions.doWrapArray(values.getCharArrayValue("charValue"))).size());
Assert.assertEquals(0, ((List<String>) Conversions.doWrapArray(values.getStringArrayValue("stringValue"))).size());
Assert.assertEquals(0, ((List<TypeReference>) Conversions.doWrapArray(values.getClassArrayValue("classValue"))).size());
Assert.assertEquals(0, ((List<EnumerationValueDeclaration>) Conversions.doWrapArray(values.getEnumArrayValue("enumValue"))).size());
Assert.assertEquals(0, ((List<AnnotationReference>) Conversions.doWrapArray(values.getAnnotationArrayValue("annotationValue"))).size());
};
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 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 testAnnotationDefaultValues_02.
@Test
public void testAnnotationDefaultValues_02() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package myannotation");
_builder.newLine();
_builder.newLine();
_builder.append("import java.util.List");
_builder.newLine();
_builder.append("import java.lang.annotation.RetentionPolicy");
_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.MutableClassDeclaration");
_builder.newLine();
_builder.newLine();
_builder.append("@Active(AnnotationDefaultValuesProcessor)");
_builder.newLine();
_builder.append("annotation AnnotationDefaultValues { }");
_builder.newLine();
_builder.newLine();
_builder.append("class AnnotationDefaultValuesProcessor implements TransformationParticipant<MutableClassDeclaration> {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("override doTransform(List<? extends MutableClassDeclaration> annotationTargets, extension TransformationContext context) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("annotationTargets.forEach[ annotationTarget |");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val annotation = annotationTarget.findAnnotation(findTypeGlobally(MyAnnotation))");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val strings = annotation.getStringArrayValue(\'value\').map[ toString ]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("strings.forEach [ annotationTarget.addField(it) [ type = string ] ]");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val booleans = annotation.getBooleanArrayValue(\'booleans\').map[ toString ]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("booleans.forEach [ annotationTarget.addField(it) [ type = string ] ]");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val types = annotation.getClassArrayValue(\'types\').map[ toString ]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("types.forEach [ annotationTarget.addField(it) [ type = string ] ]");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val policies = annotation.getEnumArrayValue(\'policies\').map[ toString ]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("policies.forEach [ annotationTarget.addField(it) [ type = string ] ]");
_builder.newLine();
_builder.append("\t\t");
_builder.newLine();
_builder.append("\t\t");
_builder.append("val nested = annotation.getAnnotationArrayValue(\'nested\').map[ toString ]");
_builder.newLine();
_builder.append("\t\t");
_builder.append("nested.forEach [ annotationTarget.addField(it) [ type = 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();
_builder.append("annotation MyAnnotation {");
_builder.newLine();
_builder.append("\t");
_builder.append("String[] value = #[]");
_builder.newLine();
_builder.append("\t");
_builder.append("boolean[] booleans = #[]");
_builder.newLine();
_builder.append("\t");
_builder.append("Class<?>[] types = #[]");
_builder.newLine();
_builder.append("\t");
_builder.append("RetentionPolicy[] policies = #[]");
_builder.newLine();
_builder.append("\t");
_builder.append("Active[] nested = #[]");
_builder.newLine();
_builder.append("}");
_builder.newLine();
Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AnnotationDefaultValues.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package myusercode");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@myannotation.AnnotationDefaultValues");
_builder_1.newLine();
_builder_1.append("@myannotation.MyAnnotation(");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("value = #[],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("booleans = #[],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("types = #[],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("policies = #[],");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("nested = #[]");
_builder_1.newLine();
_builder_1.append(")");
_builder_1.newLine();
_builder_1.append("class MyClass {}");
_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.assertTrue(IterableExtensions.isEmpty(clazz.getDeclaredFields()));
};
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 testFileSystemSupport_01.
@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.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.
the class AccessObjectProcessorTest method testWithoutPackage.
@Test
public void testWithoutPackage() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("import org.eclipse.xtend.core.tests.macro.Accessors");
_builder.newLine();
_builder.newLine();
_builder.append("@org.eclipse.xtend.core.tests.macro.AccessObjectAnn");
_builder.newLine();
_builder.append("class A {");
_builder.newLine();
_builder.append("\t");
_builder.append("String field");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
final TransformationContext ctx = it.getTransformationContext();
final MutableClassDeclaration classA = ctx.findClass("A");
Assert.assertNotNull(classA.findDeclaredMethod("getField"));
final MutableClassDeclaration classPA = ctx.findClass("PA");
final Function1<TypeReference, Boolean> _function_1 = (TypeReference it_1) -> {
Type _type = it_1.getType();
Type _type_1 = ctx.newTypeReference(Serializable.class).getType();
return Boolean.valueOf(Objects.equal(_type, _type_1));
};
Assert.assertNotNull(IterableExtensions.findFirst(classPA.getImplementedInterfaces(), _function_1));
final MutableClassDeclaration classGA = ctx.findClass("GA");
final Function1<TypeReference, Boolean> _function_2 = (TypeReference it_1) -> {
Type _type = it_1.getType();
Type _type_1 = ctx.newTypeReference(Serializable.class).getType();
return Boolean.valueOf(Objects.equal(_type, _type_1));
};
Assert.assertNotNull(IterableExtensions.findFirst(classGA.getImplementedInterfaces(), _function_2));
};
this._xtendCompilerTester.compile(_builder, _function);
}
Aggregations