Search in sources :

Example 11 with AnnotationReference

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

the class AnnotationReferenceBuildContextImpl method _internalSet.

protected void _internalSet(final String name, final AnnotationReference[] values, final boolean mustBeArray) {
    ConditionUtils.checkIterable(((Iterable<?>) Conversions.doWrapArray(values)), "values");
    AnnotationReference _head = IterableExtensions.<AnnotationReference>head(((Iterable<AnnotationReference>) Conversions.doWrapArray(values)));
    AnnotationTypeDeclaration _annotationTypeDeclaration = null;
    if (_head != null) {
        _annotationTypeDeclaration = _head.getAnnotationTypeDeclaration();
    }
    String _qualifiedName = null;
    if (_annotationTypeDeclaration != null) {
        _qualifiedName = _annotationTypeDeclaration.getQualifiedName();
    }
    this.setValues(name, values, _qualifiedName, mustBeArray);
}
Also used : JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) AnnotationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.AnnotationTypeDeclaration)

Example 12 with AnnotationReference

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

the class AbstractReusableActiveAnnotationTests method testAddAnnotationValue.

@Test
public void testAddAnnotationValue() {
    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.MutableAnnotationTarget");
    _builder.newLine();
    _builder.newLine();
    _builder.append("@Active(AddAnnotationValueProcessor)");
    _builder.newLine();
    _builder.append("annotation AddAnnotationValue { }");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class AddAnnotationValueProcessor implements TransformationParticipant<MutableAnnotationTarget> {");
    _builder.newLine();
    _builder.append("\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("override doTransform(List<? extends MutableAnnotationTarget> annotationTargets, extension TransformationContext context) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotationTargets.forEach [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("addAnnotation(");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("MyAnnotation.findTypeGlobally.newAnnotationReference [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'value\', #[\'foo\', \'bar\', \'baz\'] as String[])");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'singleValue\', \'foo\')");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'booleans\', #[true, false, true] as boolean[])");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'singleBoolean\', true)");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'numbers\', #[1, 2, 3] as int[])");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("set(\'singleNumber\', 1)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("])");
    _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("String singleValue");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("boolean[] booleans");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("boolean singleBoolean");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("int[] numbers");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("int singleNumber");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    Pair<String, String> _mappedTo = Pair.<String, String>of("myannotation/AddAnnotationValue.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@myannotation.AddAnnotationValue");
    _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");
        final AnnotationReference annotation = clazz.findAnnotation(it.getTypeReferenceProvider().newTypeReference("myannotation.MyAnnotation").getType());
        Object _value = annotation.getValue("value");
        Assert.assertArrayEquals(((String[]) ((String[]) Conversions.unwrapArray(Collections.<String>unmodifiableList(CollectionLiterals.<String>newArrayList("foo", "bar", "baz")), String.class))), ((String[]) _value));
        Assert.assertEquals("foo", annotation.getValue("singleValue"));
        Object _value_1 = annotation.getValue("booleans");
        final boolean[] booleans = ((boolean[]) _value_1);
        Assert.assertTrue(booleans[2]);
        Assert.assertEquals(Boolean.valueOf(true), annotation.getValue("singleBoolean"));
        Object _value_2 = annotation.getValue("numbers");
        Assert.assertArrayEquals(new int[] { 1, 2, 3 }, ((int[]) _value_2));
        Assert.assertEquals(Integer.valueOf(1), annotation.getValue("singleNumber"));
    };
    this.assertProcessing(_mappedTo, _mappedTo_1, _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) EObject(org.eclipse.emf.ecore.EObject) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 13 with AnnotationReference

use of org.eclipse.xtend.lib.macro.declaration.AnnotationReference 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);
}
Also used : JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) Type(org.eclipse.xtend.lib.macro.declaration.Type) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) List(java.util.List) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Example 14 with AnnotationReference

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

the class AnnotationReferenceProviderImpl method newAnnotationReference.

@Override
public AnnotationReference newAnnotationReference(final String annotationTypeName, final Procedure1<AnnotationReferenceBuildContext> initializer) {
    AnnotationReference _xblockexpression = null;
    {
        this.compilationUnit.checkCanceled();
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("annotationTypeName cannot be null");
        Preconditions.checkArgument((annotationTypeName != null), _builder);
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("initializer cannot be null");
        Preconditions.checkArgument((initializer != null), _builder_1);
        final JvmAnnotationReference jvmAnnotationReference = this.createJvmAnnotationReference(this.compilationUnit.getTypeReferences().findDeclaredType(annotationTypeName, this.compilationUnit.getXtendFile()));
        if ((jvmAnnotationReference == null)) {
            return null;
        }
        AnnotationReferenceBuildContextImpl _annotationReferenceBuildContextImpl = new AnnotationReferenceBuildContextImpl();
        final Procedure1<AnnotationReferenceBuildContextImpl> _function = (AnnotationReferenceBuildContextImpl it) -> {
            it.setDelegate(jvmAnnotationReference);
            it.setCompilationUnit(this.compilationUnit);
        };
        final AnnotationReferenceBuildContextImpl buildContext = ObjectExtensions.<AnnotationReferenceBuildContextImpl>operator_doubleArrow(_annotationReferenceBuildContextImpl, _function);
        initializer.apply(buildContext);
        _xblockexpression = this.compilationUnit.toAnnotationReference(jvmAnnotationReference);
    }
    return _xblockexpression;
}
Also used : Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference)

Example 15 with AnnotationReference

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

the class DeclarationsTest method testAnnotationReferenceValues_2.

@Test
public void testAnnotationReferenceValues_2() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package foo");
    _builder.newLine();
    _builder.append("@test.Annotation(");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("intValue = 2 / 2 + 2 * 3 - 4 % 1,");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("longValue = 42 + 4 + 6 * 42 - 4 / 45,");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("stringValue = \'foo\' + \'baz\',");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("booleanArrayValue = #[true, false],");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("intArrayValue = #[ -1, 34 + 45, 2 - 6 ],");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("longArrayValue = #[42, 5 * -3],");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("stringArrayValue = #[\'foo\', \'bla\' + \'buzz\'],");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("typeValue = String,");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("typeArrayValue = #[String, Integer],");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("annotation2Value = @test.Annotation2(\'foo\' + \'wuppa\'),");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("annotation2ArrayValue = #[@test.Annotation2, @test.Annotation2(\'foo\'+\'wuppa\')]");
    _builder.newLine();
    _builder.append("\t");
    _builder.append(") class Bar {");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final MutableClassDeclaration baseClass = it.getTypeLookup().findClass("foo.Bar");
        final AnnotationReference annoRef = IterableExtensions.head(baseClass.getAnnotations());
        Assert.assertEquals((((2 / 2) + (2 * 3)) - (4 % 1)), annoRef.getIntValue("intValue"));
        Assert.assertEquals((((42 + 4) + (6 * 42)) - (4 / 45)), annoRef.getLongValue("longValue"));
        Assert.assertEquals("foobaz", annoRef.getStringValue("stringValue"));
        final boolean[] bools = annoRef.getBooleanArrayValue("booleanArrayValue");
        Assert.assertTrue(bools[0]);
        Assert.assertFalse(bools[1]);
        Assert.assertArrayEquals(new int[] { (-1), (34 + 45), (2 - 6) }, annoRef.getIntArrayValue("intArrayValue"));
        final TypeReference[] type = annoRef.getClassArrayValue("typeArrayValue");
        Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(Integer.class), type[1]);
        final AnnotationReference anno = annoRef.getAnnotationValue("annotation2Value");
        Assert.assertEquals("foowuppa", anno.getStringValue("value"));
        final AnnotationReference[] annoArray = annoRef.getAnnotationArrayValue("annotation2ArrayValue");
        Assert.assertEquals("HUBBA BUBBA!", (annoArray[0]).getValue("value"));
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) Test(org.junit.Test)

Aggregations

AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)33 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)19 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)17 Test (org.junit.Test)17 MutableClassDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration)16 EObject (org.eclipse.emf.ecore.EObject)11 EnumerationValueDeclaration (org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration)7 JvmAnnotationReference (org.eclipse.xtext.common.types.JvmAnnotationReference)7 List (java.util.List)5 MutableFieldDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableFieldDeclaration)5 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)5 Data (org.eclipse.xtend.lib.annotations.Data)4 MutableEnumerationValueDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration)4 Extension (org.eclipse.xtext.xbase.lib.Extension)4 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)4 AccessibleObject (java.lang.reflect.AccessibleObject)3 MutableMethodDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration)3 Type (org.eclipse.xtend.lib.macro.declaration.Type)3 AnnotationReferenceBuildContext (org.eclipse.xtend.lib.macro.services.AnnotationReferenceBuildContext)3 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)3