Search in sources :

Example 21 with AnnotationReference

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

the class AbstractReusableActiveAnnotationTests method testAnnotationValueSetting_3.

@Test
public void testAnnotationValueSetting_3() {
    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(MoveAnnotationValuesProcessor)");
    _builder.newLine();
    _builder.append("annotation MoveValues {}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class MoveAnnotationValuesProcessor 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 classAnnotation = annotatedClass.annotations.head");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val field = annotatedClass.declaredFields.head");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val fieldAnnotation = field.annotations.head");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("field.removeAnnotation(fieldAnnotation)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("field.addAnnotation(fieldAnnotation.annotationTypeDeclaration.qualifiedName.newAnnotationReference [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'booleanValue\', classAnnotation.getValue(\'booleanValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'intValue\', classAnnotation.getValue(\'intValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'longValue\', classAnnotation.getValue(\'longValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'stringValue\', classAnnotation.getValue(\'stringValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'booleanArrayValue\', classAnnotation.getValue(\'booleanArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'intArrayValue\', classAnnotation.getValue(\'intArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'longArrayValue\', classAnnotation.getValue(\'longArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'stringArrayValue\', classAnnotation.getValue(\'stringArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'typeValue\', classAnnotation.getValue(\'typeValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'typeArrayValue\', classAnnotation.getValue(\'typeArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'annotation2Value\', classAnnotation.getValue(\'annotation2Value\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'annotation2ArrayValue\', classAnnotation.getValue(\'annotation2ArrayValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'enumValue\', classAnnotation.getValue(\'enumValue\'))");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("set(\'enumArrayValue\', classAnnotation.getValue(\'enumArrayValue\'))");
    _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/MoveValues.xtend", _builder.toString());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package myusercode");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import test.Annotation");
    _builder_1.newLine();
    _builder_1.append("import test.Annotation2");
    _builder_1.newLine();
    _builder_1.append("import myannotation.MoveValues");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("@Annotation(");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("intValue = 2 / 2 + 2 * 3 - 4 % 1,");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("longValue = 42 + 4 + 6 * 42 - 4 / 45,");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("stringValue = \'foo\' + \'baz\',");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("booleanArrayValue = #[true, false],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("intArrayValue = #[ -1, 34 + 45, 2 - 6 ],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("longArrayValue = #[42, 5 * -3],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("stringArrayValue = #[\'foo\', \'bla\' + \'buzz\'],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("typeValue = String,");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("typeArrayValue = #[String, Integer],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("annotation2Value = @Annotation2(\'foo\' + \'wuppa\'),");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("annotation2ArrayValue = #[@Annotation2, @Annotation2(\'foo\'+\'wuppa\')],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("enumValue = test.Enum1.YELLOW,");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("enumArrayValue = #[test.Enum1.YELLOW, test.Enum1.RED]");
    _builder_1.newLine();
    _builder_1.append(")");
    _builder_1.newLine();
    _builder_1.append("@MoveValues ");
    _builder_1.newLine();
    _builder_1.append("class UserCode {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@Annotation() String 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 clazz = it.getTypeLookup().findClass("myusercode.UserCode");
        final AnnotationReference annoRef = IterableExtensions.head(IterableExtensions.head(clazz.getDeclaredFields()).getAnnotations());
        Assert.assertEquals(Integer.valueOf((((2 / 2) + (2 * 3)) - (4 % 1))), annoRef.getValue("intValue"));
        Assert.assertEquals(Long.valueOf(((long) (((42 + 4) + (6 * 42)) - (4 / 45)))), annoRef.getValue("longValue"));
        Assert.assertEquals("foobaz", annoRef.getValue("stringValue"));
        Object _value = annoRef.getValue("booleanArrayValue");
        final boolean[] bools = ((boolean[]) _value);
        Assert.assertTrue(bools[0]);
        Assert.assertFalse(bools[1]);
        Object _value_1 = annoRef.getValue("intArrayValue");
        Assert.assertArrayEquals(new int[] { (-1), (34 + 45), (2 - 6) }, ((int[]) _value_1));
        Object _value_2 = annoRef.getValue("typeArrayValue");
        final TypeReference[] type = ((TypeReference[]) _value_2);
        Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(Integer.class), type[1]);
        Object _value_3 = annoRef.getValue("annotation2Value");
        final AnnotationReference anno = ((AnnotationReference) _value_3);
        Assert.assertEquals("foowuppa", anno.getValue("value"));
        Object _value_4 = annoRef.getValue("annotation2ArrayValue");
        final AnnotationReference[] annoArray = ((AnnotationReference[]) _value_4);
        Assert.assertEquals("HUBBA BUBBA!", (annoArray[0]).getValue("value"));
        Object _value_5 = annoRef.getValue("enumValue");
        final EnumerationValueDeclaration enum1 = ((EnumerationValueDeclaration) _value_5);
        Assert.assertEquals("YELLOW", enum1.getSimpleName());
        Object _value_6 = annoRef.getValue("enumArrayValue");
        final EnumerationValueDeclaration[] enumArray = ((EnumerationValueDeclaration[]) _value_6);
        Assert.assertEquals("YELLOW", IterableExtensions.<EnumerationValueDeclaration>head(((Iterable<EnumerationValueDeclaration>) Conversions.doWrapArray(enumArray))).getSimpleName());
    };
    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) EnumerationValueDeclaration(org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration) MutableEnumerationValueDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationValueDeclaration) Test(org.junit.Test)

Example 22 with AnnotationReference

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

the class AnnotationReferenceBuildContextImpl method _setValue.

protected void _setValue(final JvmAnnotationAnnotationValue it, final AnnotationReference[] value, final String componentType, final boolean mustBeArray) {
    this.checkType(it, componentType, mustBeArray);
    for (final AnnotationReference annotationValue : value) {
        boolean _matched = false;
        if (annotationValue instanceof JvmAnnotationReferenceImpl) {
            _matched = true;
            it.getValues().add(EcoreUtil2.<JvmAnnotationReference>cloneWithProxies(((JvmAnnotationReferenceImpl) annotationValue).getDelegate()));
        }
        if (!_matched) {
            if (annotationValue instanceof XtendAnnotationReferenceImpl) {
                _matched = true;
                throw new IllegalArgumentException("Multiple source annotations cannot be set as values. Please the the expression not the value.");
            }
        }
    }
}
Also used : JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference)

Example 23 with AnnotationReference

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

the class CompilationUnitImpl method translate.

protected Object translate(final Object object) {
    if ((object instanceof XAnnotation[])) {
        final AnnotationReference[] result = new AnnotationReference[((Object[]) object).length];
        int _length = ((Object[]) object).length;
        ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, _length, true);
        for (final Integer i : _doubleDotLessThan) {
            result[(i).intValue()] = this.translateAnnotation(((XAnnotation[]) object)[(i).intValue()]);
        }
        return result;
    }
    if ((object instanceof XAnnotation)) {
        return this.translateAnnotation(((XAnnotation) object));
    }
    if ((object instanceof JvmTypeReference[])) {
        final TypeReference[] result_1 = new TypeReference[((Object[]) object).length];
        int _length_1 = ((Object[]) object).length;
        ExclusiveRange _doubleDotLessThan_1 = new ExclusiveRange(0, _length_1, true);
        for (final Integer i_1 : _doubleDotLessThan_1) {
            Object _translate = this.translate(((Object[]) object)[(i_1).intValue()]);
            result_1[(i_1).intValue()] = ((TypeReference) _translate);
        }
        return result_1;
    }
    if ((object instanceof JvmTypeReference)) {
        return this.toTypeReference(((JvmTypeReference) object));
    }
    if ((object instanceof JvmEnumerationLiteral[])) {
        final EnumerationValueDeclaration[] result_2 = new EnumerationValueDeclaration[((Object[]) object).length];
        int _length_2 = ((Object[]) object).length;
        ExclusiveRange _doubleDotLessThan_2 = new ExclusiveRange(0, _length_2, true);
        for (final Integer i_2 : _doubleDotLessThan_2) {
            Object _translate_1 = this.translate(((Object[]) object)[(i_2).intValue()]);
            result_2[(i_2).intValue()] = ((EnumerationValueDeclaration) _translate_1);
        }
        return result_2;
    }
    if ((object instanceof JvmEnumerationLiteral)) {
        return this.toMemberDeclaration(((JvmMember) object));
    }
    return object;
}
Also used : ExclusiveRange(org.eclipse.xtext.xbase.lib.ExclusiveRange) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) JvmEnumerationLiteral(org.eclipse.xtext.common.types.JvmEnumerationLiteral) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) EObject(org.eclipse.emf.ecore.EObject) JvmMember(org.eclipse.xtext.common.types.JvmMember) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) EnumerationValueDeclaration(org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration)

Example 24 with AnnotationReference

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

the class CompilationUnitImpl method translateAnnotation.

protected AnnotationReference translateAnnotation(final XAnnotation annotation) {
    AnnotationReference _xblockexpression = null;
    {
        AnnotationReferenceBuildContextImpl _annotationReferenceBuildContextImpl = new AnnotationReferenceBuildContextImpl();
        final Procedure1<AnnotationReferenceBuildContextImpl> _function = (AnnotationReferenceBuildContextImpl it) -> {
            it.setCompilationUnit(this);
            JvmAnnotationReference _createJvmAnnotationReference = this.typesFactory.createJvmAnnotationReference();
            final Procedure1<JvmAnnotationReference> _function_1 = (JvmAnnotationReference reference) -> {
                JvmType _annotationType = annotation.getAnnotationType();
                reference.setAnnotation(((JvmAnnotationType) _annotationType));
            };
            JvmAnnotationReference _doubleArrow = ObjectExtensions.<JvmAnnotationReference>operator_doubleArrow(_createJvmAnnotationReference, _function_1);
            it.setDelegate(_doubleArrow);
        };
        final AnnotationReferenceBuildContextImpl buildContext = ObjectExtensions.<AnnotationReferenceBuildContextImpl>operator_doubleArrow(_annotationReferenceBuildContextImpl, _function);
        EList<XAnnotationElementValuePair> _elementValuePairs = annotation.getElementValuePairs();
        for (final XAnnotationElementValuePair valuePair : _elementValuePairs) {
            {
                final XExpression value = valuePair.getValue();
                if ((value != null)) {
                    final JvmOperation operation = valuePair.getElement();
                    final Object annotationValue = this.translateAnnotationValue(value, operation.getReturnType());
                    buildContext.set(operation.getSimpleName(), annotationValue);
                }
            }
        }
        XExpression _value = annotation.getValue();
        boolean _tripleNotEquals = (_value != null);
        if (_tripleNotEquals) {
            final Object annotationValue = this.translateAnnotationValue(annotation.getValue(), null);
            buildContext.set("value", annotationValue);
        }
        _xblockexpression = this.toAnnotationReference(buildContext.getDelegate());
    }
    return _xblockexpression;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XAnnotationElementValuePair(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair) EList(org.eclipse.emf.common.util.EList) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) XExpression(org.eclipse.xtext.xbase.XExpression) EObject(org.eclipse.emf.ecore.EObject) JvmType(org.eclipse.xtext.common.types.JvmType) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference)

Example 25 with AnnotationReference

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

the class DeclarationsTest method testAnnotationReferenceValues.

@Test
public void testAnnotationReferenceValues() {
    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(Integer.valueOf((((2 / 2) + (2 * 3)) - (4 % 1))), annoRef.getValue("intValue"));
        Assert.assertEquals(Integer.valueOf((((42 + 4) + (6 * 42)) - (4 / 45))), annoRef.getValue("longValue"));
        Assert.assertEquals("foobaz", annoRef.getValue("stringValue"));
        Object _value = annoRef.getValue("booleanArrayValue");
        final boolean[] bools = ((boolean[]) _value);
        Assert.assertTrue(bools[0]);
        Assert.assertFalse(bools[1]);
        Object _value_1 = annoRef.getValue("intArrayValue");
        Assert.assertArrayEquals(new int[] { (-1), (34 + 45), (2 - 6) }, ((int[]) _value_1));
        Object _value_2 = annoRef.getValue("typeArrayValue");
        final TypeReference[] type = ((TypeReference[]) _value_2);
        Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(Integer.class), type[1]);
        Object _value_3 = annoRef.getValue("annotation2Value");
        final AnnotationReference anno = ((AnnotationReference) _value_3);
        Assert.assertEquals("foowuppa", anno.getValue("value"));
        Object _value_4 = annoRef.getValue("annotation2ArrayValue");
        final AnnotationReference[] annoArray = ((AnnotationReference[]) _value_4);
        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) AccessibleObject(java.lang.reflect.AccessibleObject) 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