Search in sources :

Example 6 with AnnotationReference

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

the class AbstractReusableActiveAnnotationTests method testAnnotationValueSetting_1.

@Test
public void testAnnotationValueSetting_1() {
    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.*");
    _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("BlackOrWhite color");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("BlackOrWhite[] colors");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Class<?> type");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Class<?>[] types");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("SomeAnnotation annotation");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("SomeAnnotation[] annotations");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("annotation SomeAnnotation {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("boolean value");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("enum BlackOrWhite {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("BLACK, WHITE");
    _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.append("val someAnnotationType = findTypeGlobally(SomeAnnotation)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val enumType = findTypeGlobally(\'myannotation.BlackOrWhite\') as EnumerationTypeDeclaration");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val white = enumType.findDeclaredValue(\'WHITE\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val black = enumType.findDeclaredValue(\'BLACK\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingValue = anno.getValue(\'color\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingValue != white)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"color\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithColor = annotatedClass.addAnnotation(newAnnotationReference(anno) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setEnumValue(\'color\', black)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(anno)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingColorsValue = annoWithColor.getValue(\'colors\') as Object[]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingColorsValue.get(0) != white && existingColorsValue.get(1) != black && existingColorsValue.length != 2)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"colors\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithColors = annotatedClass.addAnnotation(newAnnotationReference(annoWithColor) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setEnumValue(\'colors\', black, white)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithColor)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingType = annoWithColors.getValue(\'type\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingType != string)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"type\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithType = annotatedClass.addAnnotation(newAnnotationReference(annoWithColors) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setClassValue(\'type\', annotatedClass.newTypeReference)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithColors)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val existingTypes = annoWithType.getValue(\'types\') as Object[]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (existingTypes.get(0) != primitiveInt && existingTypes.get(1) != annotatedClass.newTypeReference && existingTypes.length != 2)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"types\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithTypes = annotatedClass.addAnnotation(newAnnotationReference(annoWithType) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setClassValue(\'types\', primitiveBoolean)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithType)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annotationReference = annoWithTypes.getAnnotationValue(\'annotation\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (someAnnotationType != annotationReference.annotationTypeDeclaration)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"someAnnotationType != annotationReference.annotationTypeDeclaration\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annoWithAnnotation = annotatedClass.addAnnotation(newAnnotationReference(annoWithTypes) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setAnnotationValue(\'annotation\', ");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("newAnnotationReference(someAnnotationType) [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("setBooleanValue(\'value\', false)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append(")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithTypes)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val annotationReferences = annoWithAnnotation.getAnnotationArrayValue(\'annotations\')");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (annotationReferences.size != 2)");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw new AssertionError(\"annotationReferences.size != 2\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotationReferences.forEach [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("if (someAnnotationType != annotationTypeDeclaration)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("throw new AssertionError(\"someAnnotationType != annotationTypeDeclaration\")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.addAnnotation(newAnnotationReference(annoWithAnnotation) [");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("setAnnotationValue(\'annotations\', ");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("newAnnotationReference(someAnnotationType) [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("setBooleanValue(\'value\', false)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("],");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("newAnnotationReference(someAnnotationType) [");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("setBooleanValue(\'value\', false)");
    _builder.newLine();
    _builder.append("\t\t\t\t");
    _builder.append("]");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append(")");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("])");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("annotatedClass.removeAnnotation(annoWithAnnotation)");
    _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(");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("color=BlackOrWhite.WHITE, ");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("colors=#[BlackOrWhite.WHITE, BlackOrWhite.BLACK], ");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("type = String, ");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("types=#[Integer, MyClass],");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("annotation=@SomeAnnotation(true),");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("annotations=#[@SomeAnnotation(true), @SomeAnnotation(true)]");
    _builder_1.newLine();
    _builder_1.append(")");
    _builder_1.newLine();
    _builder_1.append("class MyClass {");
    _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");
        Type _findTypeGlobally = it.getTypeLookup().findTypeGlobally("myannotation.BlackOrWhite");
        final EnumerationTypeDeclaration colorEnum = ((EnumerationTypeDeclaration) _findTypeGlobally);
        final Type annotationType = it.getTypeLookup().findTypeGlobally("myannotation.ConfigurableAnnotation");
        final AnnotationReference annotation = clazz.findAnnotation(annotationType);
        Assert.assertEquals(colorEnum.findDeclaredValue("BLACK"), annotation.getValue("color"));
        Object _value = annotation.getValue("colors");
        final Object[] colors = ((Object[]) _value);
        Assert.assertEquals(2, colors.length);
        Assert.assertEquals(colorEnum.findDeclaredValue("BLACK"), colors[0]);
        Assert.assertEquals(colorEnum.findDeclaredValue("WHITE"), colors[1]);
        Assert.assertEquals(it.getTypeReferenceProvider().newTypeReference(clazz), annotation.getValue("type"));
        final TypeReference[] types = annotation.getClassArrayValue("types");
        Assert.assertEquals(1, types.length);
        Assert.assertEquals(it.getTypeReferenceProvider().getPrimitiveBoolean(), types[0]);
        final Type someAnnotationType = it.getTypeLookup().findTypeGlobally("myannotation.SomeAnnotation");
        final AnnotationReference annotationValue = annotation.getAnnotationValue("annotation");
        Assert.assertNotNull(annotationValue);
        Assert.assertEquals(someAnnotationType, annotationValue.getAnnotationTypeDeclaration());
        Assert.assertFalse(annotationValue.getBooleanValue("value"));
        final AnnotationReference[] annotationsValue = annotation.getAnnotationArrayValue("annotations");
        Assert.assertNotNull(annotationsValue);
        Assert.assertEquals(2, ((List<AnnotationReference>) Conversions.doWrapArray(annotationsValue)).size());
        final Consumer<AnnotationReference> _function_1 = (AnnotationReference it_1) -> {
            Assert.assertEquals(someAnnotationType, annotationValue.getAnnotationTypeDeclaration());
            Assert.assertFalse(annotationValue.getBooleanValue("value"));
        };
        ((List<AnnotationReference>) Conversions.doWrapArray(annotationsValue)).forEach(_function_1);
    };
    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) Consumer(java.util.function.Consumer) 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) List(java.util.List) MutableClassDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration) EnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.EnumerationTypeDeclaration) MutableEnumerationTypeDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableEnumerationTypeDeclaration) Test(org.junit.Test)

Example 7 with AnnotationReference

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

the class CheckMutableInterfaceDeclarationProcessor method doTransform.

@Override
public void doTransform(final List<? extends MutableInterfaceDeclaration> annotatedTargetElements, @Extension final TransformationContext context) {
    for (final MutableInterfaceDeclaration annotatedTargetElement : annotatedTargetElements) {
        {
            final Procedure1<String> _function = (String identifier) -> {
                annotatedTargetElement.addTypeParameter(identifier).remove();
            };
            MutableAssert.assertValidJavaIdentifier("name", _function);
            final Procedure0 _function_1 = () -> {
                annotatedTargetElement.addTypeParameter("T", ((TypeReference[]) null));
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "upperBounds cannot be null", _function_1);
            final Procedure0 _function_2 = () -> {
                annotatedTargetElement.addTypeParameter("T", new TypeReference[] { null });
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "upperBounds cannot contain null", _function_2);
            annotatedTargetElement.addTypeParameter("T", ((TypeReference[]) Conversions.unwrapArray(CollectionLiterals.<TypeReference>emptyList(), TypeReference.class))).remove();
            final Procedure0 _function_3 = () -> {
                annotatedTargetElement.setExtendedInterfaces(null);
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "superinterfaces cannot be null", _function_3);
            final Procedure0 _function_4 = () -> {
                annotatedTargetElement.setExtendedInterfaces(Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList((TypeReference) null)));
            };
            MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "superinterfaces cannot contain null", _function_4);
            annotatedTargetElement.setExtendedInterfaces(CollectionLiterals.<TypeReference>emptyList());
            final Procedure1<AnnotationReferenceBuildContext> _function_5 = (AnnotationReferenceBuildContext it) -> {
                final Procedure0 _function_6 = () -> {
                    it.set(null, null);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_6);
                final Procedure0 _function_7 = () -> {
                    it.set(null, "foo");
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_7);
                final Procedure0 _function_8 = () -> {
                    String[] _xblockexpression = null;
                    {
                        final String[] array = { "foo" };
                        _xblockexpression = array;
                    }
                    it.set(null, _xblockexpression);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_8);
                final Procedure0 _function_9 = () -> {
                    it.set(null, Boolean.valueOf(true));
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_9);
                final Procedure0 _function_10 = () -> {
                    it.set(null, ((boolean[]) ((boolean[]) Conversions.unwrapArray(Collections.<Boolean>unmodifiableList(CollectionLiterals.<Boolean>newArrayList(Boolean.valueOf(true))), boolean.class))));
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_10);
                final Procedure0 _function_11 = () -> {
                    it.set(null, Integer.valueOf(0));
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_11);
                final Procedure0 _function_12 = () -> {
                    it.set(null, ((int[]) ((int[]) Conversions.unwrapArray(Collections.<Integer>unmodifiableList(CollectionLiterals.<Integer>newArrayList(Integer.valueOf(0))), int.class))));
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "name has to be a valid java identifier", _function_12);
                final Procedure0 _function_13 = () -> {
                    String[] _xblockexpression = null;
                    {
                        final String[] array = { "foo" };
                        _xblockexpression = array;
                    }
                    it.set("doesNotExist", _xblockexpression);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "The annotation property \'doesNotExist\' is not declared on the annotation type \'java.lang.Deprecated\'.", _function_13);
            };
            final AnnotationReference annotationReference = annotatedTargetElement.addAnnotation(context.newAnnotationReference(Deprecated.class, _function_5));
            annotatedTargetElement.removeAnnotation(annotationReference);
            final Procedure1<AnnotationReferenceBuildContext> _function_6 = (AnnotationReferenceBuildContext it) -> {
                String[] _xblockexpression = null;
                {
                    final String[] array = { "foo" };
                    _xblockexpression = array;
                }
                it.set("value", _xblockexpression);
                final Procedure0 _function_7 = () -> {
                    int[] _xblockexpression_1 = null;
                    {
                        final int[] array = { 1 };
                        _xblockexpression_1 = array;
                    }
                    it.set("value", _xblockexpression_1);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "int[] is not applicable at this location. Expected java.lang.String[]", _function_7);
            };
            final AnnotationReference otherAnnotationReference = annotatedTargetElement.addAnnotation(context.newAnnotationReference(SuppressWarnings.class, _function_6));
            annotatedTargetElement.removeAnnotation(otherAnnotationReference);
            final Procedure1<AnnotationReferenceBuildContext> _function_7 = (AnnotationReferenceBuildContext it) -> {
                it.set("validators", new TypeReference[0]);
                it.set("validators", context.newTypeReference(String.class));
                final Procedure0 _function_8 = () -> {
                    it.set("validators", new String[0]);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "java.lang.String[] is not applicable at this location. Expected org.eclipse.xtend.lib.macro.declaration.TypeReference[]", _function_8);
                final Procedure0 _function_9 = () -> {
                    it.set("validators", new Object[0]);
                };
                MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "Cannot set annotation values of type java.lang.Object[]", _function_9);
            };
            final AnnotationReference composedChecksReference = annotatedTargetElement.addAnnotation(context.newAnnotationReference(ComposedChecks.class, _function_7));
            annotatedTargetElement.removeAnnotation(composedChecksReference);
        }
    }
}
Also used : MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) Procedure0(org.eclipse.xtext.xbase.lib.Procedures.Procedure0) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) AnnotationReferenceBuildContext(org.eclipse.xtend.lib.macro.services.AnnotationReferenceBuildContext) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference)

Example 8 with AnnotationReference

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

the class CompilationUnitImpl method translateAnnotationValue.

public Object translateAnnotationValue(final JvmAnnotationValue value, final boolean isArray) {
    Pair<List<?>, Class<?>> _switchResult = null;
    boolean _matched = false;
    if (value instanceof JvmCustomAnnotationValue) {
        if ((((JvmCustomAnnotationValue) value).getValues().isEmpty() && isArray)) {
            _matched = true;
            List<Object> _emptyList = CollectionLiterals.<Object>emptyList();
            Class<?> _arrayComponentType = this.toArrayComponentType(this.findExpectedType(value));
            _switchResult = Pair.<List<?>, Class<?>>of(_emptyList, _arrayComponentType);
        }
    }
    if (!_matched) {
        if (value instanceof JvmCustomAnnotationValue) {
            _matched = true;
            final JvmTypeReference expectedType = this.findExpectedType(value);
            final Function1<XExpression, Object> _function = (XExpression it) -> {
                return this.evaluate(it, expectedType);
            };
            final Object result = IterableExtensions.<Object>head(IterableExtensions.<XExpression, Object>map(Iterables.<XExpression>filter(((JvmCustomAnnotationValue) value).getValues(), XExpression.class), _function));
            return this.translateAnnotationValue(result, expectedType, isArray);
        }
    }
    if (!_matched) {
        if (value instanceof JvmTypeAnnotationValue) {
            _matched = true;
            final Function1<JvmTypeReference, TypeReference> _function = (JvmTypeReference it) -> {
                return this.toTypeReference(it);
            };
            List<TypeReference> _map = ListExtensions.<JvmTypeReference, TypeReference>map(((JvmTypeAnnotationValue) value).getValues(), _function);
            _switchResult = Pair.<List<?>, Class<?>>of(_map, TypeReference.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmAnnotationAnnotationValue) {
            _matched = true;
            final Function1<JvmAnnotationReference, AnnotationReference> _function = (JvmAnnotationReference it) -> {
                return this.toAnnotationReference(it);
            };
            List<AnnotationReference> _map = ListExtensions.<JvmAnnotationReference, AnnotationReference>map(((JvmAnnotationAnnotationValue) value).getValues(), _function);
            _switchResult = Pair.<List<?>, Class<?>>of(_map, AnnotationReference.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmStringAnnotationValue) {
            _matched = true;
            EList<String> _values = ((JvmStringAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, String.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmBooleanAnnotationValue) {
            _matched = true;
            EList<Boolean> _values = ((JvmBooleanAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, boolean.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmIntAnnotationValue) {
            _matched = true;
            EList<Integer> _values = ((JvmIntAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, int.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmByteAnnotationValue) {
            _matched = true;
            EList<Byte> _values = ((JvmByteAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, byte.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmCharAnnotationValue) {
            _matched = true;
            EList<Character> _values = ((JvmCharAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, char.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmDoubleAnnotationValue) {
            _matched = true;
            EList<Double> _values = ((JvmDoubleAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, double.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmEnumAnnotationValue) {
            _matched = true;
            final Function1<JvmEnumerationLiteral, NamedElement> _function = (JvmEnumerationLiteral it) -> {
                return this.toNamedElement(it);
            };
            List<NamedElement> _map = ListExtensions.<JvmEnumerationLiteral, NamedElement>map(((JvmEnumAnnotationValue) value).getValues(), _function);
            _switchResult = Pair.<List<?>, Class<?>>of(_map, EnumerationValueDeclaration.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmFloatAnnotationValue) {
            _matched = true;
            EList<Float> _values = ((JvmFloatAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, float.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmLongAnnotationValue) {
            _matched = true;
            EList<Long> _values = ((JvmLongAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, long.class);
        }
    }
    if (!_matched) {
        if (value instanceof JvmShortAnnotationValue) {
            _matched = true;
            EList<Short> _values = ((JvmShortAnnotationValue) value).getValues();
            _switchResult = Pair.<List<?>, Class<?>>of(_values, short.class);
        }
    }
    if (!_matched) {
        List<Object> _emptyList = CollectionLiterals.<Object>emptyList();
        _switchResult = Pair.<List<?>, Class<?>>of(_emptyList, Object.class);
    }
    final Pair<List<?>, Class<?>> result = _switchResult;
    if (isArray) {
        return this.toArrayOfType(result.getKey(), result.getValue());
    } else {
        return IterableExtensions.head(result.getKey());
    }
}
Also used : JvmEnumAnnotationValue(org.eclipse.xtext.common.types.JvmEnumAnnotationValue) JvmByteAnnotationValue(org.eclipse.xtext.common.types.JvmByteAnnotationValue) JvmCustomAnnotationValue(org.eclipse.xtext.common.types.JvmCustomAnnotationValue) JvmLongAnnotationValue(org.eclipse.xtext.common.types.JvmLongAnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) EList(org.eclipse.emf.common.util.EList) JvmStringAnnotationValue(org.eclipse.xtext.common.types.JvmStringAnnotationValue) JvmFloatAnnotationValue(org.eclipse.xtext.common.types.JvmFloatAnnotationValue) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) EObject(org.eclipse.emf.ecore.EObject) NamedElement(org.eclipse.xtend.lib.macro.declaration.NamedElement) EnumerationValueDeclaration(org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration) JvmTypeAnnotationValue(org.eclipse.xtext.common.types.JvmTypeAnnotationValue) JvmBooleanAnnotationValue(org.eclipse.xtext.common.types.JvmBooleanAnnotationValue) JvmIntAnnotationValue(org.eclipse.xtext.common.types.JvmIntAnnotationValue) JvmCharAnnotationValue(org.eclipse.xtext.common.types.JvmCharAnnotationValue) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) TypeReference(org.eclipse.xtend.lib.macro.declaration.TypeReference) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmShortAnnotationValue(org.eclipse.xtext.common.types.JvmShortAnnotationValue) JvmDoubleAnnotationValue(org.eclipse.xtext.common.types.JvmDoubleAnnotationValue) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) JvmEnumerationLiteral(org.eclipse.xtext.common.types.JvmEnumerationLiteral) XExpression(org.eclipse.xtext.xbase.XExpression) JvmAnnotationAnnotationValue(org.eclipse.xtext.common.types.JvmAnnotationAnnotationValue)

Example 9 with AnnotationReference

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

the class JvmAnnotationTargetImpl method addAnnotation.

public AnnotationReference addAnnotation(final AnnotationReference annotationReference) {
    AnnotationReference _xblockexpression = null;
    {
        this.checkMutable();
        Preconditions.checkArgument((annotationReference != null), "annotationReference cannot be null");
        AnnotationReference _xifexpression = null;
        if ((annotationReference instanceof JvmAnnotationReferenceImpl)) {
            AnnotationReference _xblockexpression_1 = null;
            {
                final JvmAnnotationReference jvmAnnotationReference = EcoreUtil2.<JvmAnnotationReference>cloneWithProxies(((JvmAnnotationReferenceImpl) annotationReference).getDelegate());
                EList<JvmAnnotationReference> _annotations = this.getDelegate().getAnnotations();
                _annotations.add(jvmAnnotationReference);
                _xblockexpression_1 = this.getCompilationUnit().toAnnotationReference(jvmAnnotationReference);
            }
            _xifexpression = _xblockexpression_1;
        } else {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append(annotationReference);
            _builder.append(" is not annotation reference");
            throw new IllegalArgumentException(_builder.toString());
        }
        _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
}
Also used : 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 10 with AnnotationReference

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

the class JvmMemberDeclarationImpl method setDeprecated.

public void setDeprecated(final boolean deprecated) {
    this.checkMutable();
    if (deprecated) {
        T _delegate = this.getDelegate();
        _delegate.setDeprecated(true);
        this.addAnnotation(this.getCompilationUnit().getAnnotationReferenceProvider().newAnnotationReference(Deprecated.class));
    } else {
        T _delegate_1 = this.getDelegate();
        _delegate_1.setDeprecated(false);
        final AnnotationReference existingReference = this.findAnnotation(this.getCompilationUnit().getTypeLookup().findTypeGlobally(Deprecated.class));
        if ((existingReference != null)) {
            this.removeAnnotation(existingReference);
        }
    }
}
Also used : AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference)

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