Search in sources :

Example 1 with AnnotationReferenceBuildContext

use of org.eclipse.xtend.lib.macro.services.AnnotationReferenceBuildContext 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 2 with AnnotationReferenceBuildContext

use of org.eclipse.xtend.lib.macro.services.AnnotationReferenceBuildContext in project xtext-xtend by eclipse.

the class AnnotationWithNestedEnumProcessor method doTransform.

@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
    final MutableInterfaceDeclaration interfaceType = context.findInterface(this.getInterfaceName(annotatedClass));
    context.setPrimarySourceElement(interfaceType, annotatedClass);
    try {
        final EnumerationValueDeclaration value = annotatedClass.findAnnotation(context.findTypeGlobally(AnnotationWithNestedEnum.class)).getEnumValue("value");
        final Procedure1<AnnotationReferenceBuildContext> _function = (AnnotationReferenceBuildContext it) -> {
            it.setEnumValue("value", value);
        };
        final AnnotationReference ref = context.newAnnotationReference(AnnotationWithNestedEnum.class, _function);
        interfaceType.addAnnotation(ref);
    } catch (final Throwable _t) {
        if (_t instanceof Exception) {
            final Exception exc = (Exception) _t;
            String _message = exc.getMessage();
            String _plus = ("failed: " + _message);
            context.addError(annotatedClass, _plus);
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}
Also used : MutableInterfaceDeclaration(org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration) AnnotationReferenceBuildContext(org.eclipse.xtend.lib.macro.services.AnnotationReferenceBuildContext) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) EnumerationValueDeclaration(org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration)

Example 3 with AnnotationReferenceBuildContext

use of org.eclipse.xtend.lib.macro.services.AnnotationReferenceBuildContext in project xtext-xtend by eclipse.

the class DeclarationsTest method testAnnotation3.

@Test
public void testAnnotation3() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("@test.Annotation");
    _builder.newLine();
    _builder.append("class MyClass {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("@test.Annotation2 String foo");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
        final AnnotationReference anno = IterableExtensions.head(it.getTypeLookup().findClass("MyClass").getAnnotations());
        final AnnotationReference anno2 = IterableExtensions.head(IterableExtensions.head(it.getTypeLookup().findClass("MyClass").getDeclaredFields()).getAnnotations());
        final Procedure1<AnnotationReferenceBuildContext> _function_1 = (AnnotationReferenceBuildContext it_1) -> {
            it_1.set("annotation2Value", anno2);
        };
        it.getTypeLookup().findClass("MyClass").addAnnotation(it.getAnnotationReferenceProvider().newAnnotationReference(anno, _function_1));
        it.getTypeLookup().findClass("MyClass").removeAnnotation(anno);
        Object _value = anno.getValue("annotation2Value");
        Assert.assertEquals(IterableExtensions.head(IterableExtensions.head(it.getTypeLookup().findClass("MyClass").getDeclaredFields()).getAnnotations()).getAnnotationTypeDeclaration(), ((AnnotationReference) _value).getAnnotationTypeDeclaration());
    };
    this.asCompilationUnit(this.validFile(_builder), _function);
}
Also used : CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AnnotationReference(org.eclipse.xtend.lib.macro.declaration.AnnotationReference) AnnotationReferenceBuildContext(org.eclipse.xtend.lib.macro.services.AnnotationReferenceBuildContext) AccessibleObject(java.lang.reflect.AccessibleObject) Test(org.junit.Test)

Aggregations

AnnotationReference (org.eclipse.xtend.lib.macro.declaration.AnnotationReference)3 AnnotationReferenceBuildContext (org.eclipse.xtend.lib.macro.services.AnnotationReferenceBuildContext)3 MutableInterfaceDeclaration (org.eclipse.xtend.lib.macro.declaration.MutableInterfaceDeclaration)2 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)2 AccessibleObject (java.lang.reflect.AccessibleObject)1 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)1 EnumerationValueDeclaration (org.eclipse.xtend.lib.macro.declaration.EnumerationValueDeclaration)1 TypeReference (org.eclipse.xtend.lib.macro.declaration.TypeReference)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 Procedure0 (org.eclipse.xtext.xbase.lib.Procedures.Procedure0)1 Test (org.junit.Test)1