Search in sources :

Example 31 with MutableClassDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.

the class AccessorsAnnotationTest method testProperty2.

@Test
public void testProperty2() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import org.eclipse.xtend.core.tests.macro.Accessors");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class A {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@Accessors String field");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("private def String getField() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("return null");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("} ");
    _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.assertEquals(Visibility.PRIVATE, classA.findDeclaredMethod("getField").getVisibility());
        Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("setField", ctx.getString()).getVisibility());
        Assert.assertEquals(2, IterableExtensions.size(classA.getDeclaredMethods()));
    };
    this._xtendCompilerTester.compile(_builder, _function);
}
Also used : XtendCompilerTester(org.eclipse.xtend.core.compiler.batch.XtendCompilerTester) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TransformationContext(org.eclipse.xtend.lib.macro.TransformationContext) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) AbstractActiveAnnotationTest(org.eclipse.xtend.core.tests.macro.AbstractActiveAnnotationTest) Test(org.junit.Test)

Example 32 with MutableClassDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.

the class _TESTDATA_InternalClassProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    String _qualifiedName = annotatedClass.getQualifiedName();
    String _plus = (_qualifiedName + ".InternalClass");
    MutableClassDeclaration _findClass = context.findClass(_plus);
    final Procedure1<MutableClassDeclaration> _function = (MutableClassDeclaration it) -> {
        final Procedure1<MutableFieldDeclaration> _function_1 = (MutableFieldDeclaration it_1) -> {
            it_1.setType(context.getString());
        };
        it.addField("myField", _function_1);
    };
    ObjectExtensions.<MutableClassDeclaration>operator_doubleArrow(_findClass, _function);
}
Also used : MutableFieldDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)

Example 33 with MutableClassDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testThrowsAndTypeParam.

@Test
public void testThrowsAndTypeParam() {
    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(ThrowsAndTypeParamProcessor))");
    _builder.newLine();
    _builder.append("annotation ThrowsAndTypeParam { }");
    _builder.newLine();
    _builder.append("class ThrowsAndTypeParamProcessor implements TransformationParticipant<MutableMethodDeclaration> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableMethodDeclaration> annotatedMethods, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedMethods.forEach [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("val type = addTypeParameter(\'A\')");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("addParameter(\'myParam\', newTypeReference(type))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setExceptions(newTypeReference(\'java.lang.Exception\'))");
    _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/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.ThrowsAndTypeParam");
    _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("}");
    _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());
        Assert.assertEquals("A", IterableExtensions.head(method.getTypeParameters()).getSimpleName());
        Assert.assertEquals("myParam", IterableExtensions.head(method.getParameters()).getSimpleName());
        Assert.assertSame(IterableExtensions.head(method.getTypeParameters()), IterableExtensions.head(method.getParameters()).getType().getType());
        Assert.assertEquals(1, IterableExtensions.size(method.getExceptions()));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) MutableMethodDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 34 with MutableClassDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testAnnotationDefaultValues_01.

@Test
public void testAnnotationDefaultValues_01() {
    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("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);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 35 with MutableClassDeclaration

use of org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration in project xtext-xtend by eclipse.

the class AbstractReusableActiveAnnotationTests method testAnnotationValueSetting_2.

@Test
public void testAnnotationValueSetting_2() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package myannotation");
    _builder.newLine();
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.*");
    _builder.newLine();
    _builder.append("import org.eclipse.xtend.lib.macro.declaration.*");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(ConfigurableAnnotationProcessor)");
    _builder.newLine();
    _builder.append("annotation ConfigurableAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("int someValue");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Constants {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public static val int MYCONSTANT = Integer.MAX_VALUE - 42");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class ConfigurableAnnotationProcessor extends AbstractClassProcessor {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(MutableClassDeclaration annotatedClass, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val anno = annotatedClass.annotations.head");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingValue = anno.getValue(\'someValue\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.docComment = \'\'+existingValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/ConfigurableAnnotation.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("@ConfigurableAnnotation(someValue=MoreConstants.MY_CONSTANT * 1)");
    _builder_1.newLine();
    _builder_1.append("class MyClass {");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("class MoreConstants {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("public static val int MY_CONSTANT = myannotation.Constants.MYCONSTANT - Integer.MAX_VALUE + 42 * 2");
    _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("42", clazz.getDocComment());
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Aggregations

MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)97 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)94 Test (org.junit.Test)91 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)87 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)24 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)17 AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)14 EObject (org.eclipse.emf.ecore.EObject)10 Type (org.eclipse.xtend.lib.macro.declaration.Type)10 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)10 List (java.util.List)9 Element (org.eclipse.xtend.lib.macro.declaration.Element)8 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)8 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)7 AnnotationTypeDeclaration (org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)6 ClassDeclaration (org.eclipse.xtend.lib.macro.declaration.ClassDeclaration)6 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)6 AccessibleObject (java.lang.reflect.AccessibleObject)5 Consumer (java.util.function.Consumer)4 XtendCompilerTester (org.eclipse.xtend.core.compiler.batch.XtendCompilerTester)4