Search in sources :

Example 16 with JvmAnnotationType

use of org.eclipse.xtext.common.types.JvmAnnotationType in project xtext-xtend by eclipse.

the class XtendValidator method checkAnnotationTarget.

@Check
public void checkAnnotationTarget(XAnnotation annotation) {
    JvmType annotationType = annotation.getAnnotationType();
    if (annotationType == null || annotationType.eIsProxy() || !(annotationType instanceof JvmAnnotationType)) {
        return;
    }
    Set<ElementType> targets = annotationUtil.getAnnotationTargets((JvmAnnotationType) annotationType);
    if (targets.isEmpty())
        return;
    final EObject eContainer = getContainingAnnotationTarget(annotation);
    Class<? extends EObject> clazz = eContainer.getClass();
    if (eContainer instanceof XtendField && eContainer.eContainer() instanceof XtendAnnotationType) {
        clazz = XtendFunction.class;
    }
    for (Entry<Class<?>, Collection<ElementType>> mapping : targetInfos.asMap().entrySet()) {
        if (mapping.getKey().isAssignableFrom(clazz)) {
            targets.retainAll(mapping.getValue());
            if (targets.isEmpty()) {
                error("The annotation @" + annotation.getAnnotationType().getSimpleName() + " is disallowed for this location.", annotation, null, INSIGNIFICANT_INDEX, ANNOTATION_WRONG_TARGET);
            }
        }
    }
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) ElementType(java.lang.annotation.ElementType) XtendAnnotationType(org.eclipse.xtend.core.xtend.XtendAnnotationType) EObject(org.eclipse.emf.ecore.EObject) Collection(java.util.Collection) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) JvmType(org.eclipse.xtext.common.types.JvmType) XtendField(org.eclipse.xtend.core.xtend.XtendField) Check(org.eclipse.xtext.validation.Check)

Example 17 with JvmAnnotationType

use of org.eclipse.xtext.common.types.JvmAnnotationType in project xtext-xtend by eclipse.

the class ActiveAnnotationContextProvider method computeContext.

public ActiveAnnotationContexts computeContext(final XtendFile file) {
    final Stopwatches.StoppedTask task = Stopwatches.forTask("[macros] findActiveAnnotations (ActiveAnnotationContextProvider.computeContext)");
    task.start();
    try {
        final ActiveAnnotationContexts result = ActiveAnnotationContexts.installNew(file.eResource());
        final CompilationUnitImpl compilationUnit = this.compilationUnitProvider.get();
        compilationUnit.setXtendFile(file);
        result.compilationUnit = compilationUnit;
        final IAcceptor<Pair<JvmAnnotationType, XAnnotation>> _function = (Pair<JvmAnnotationType, XAnnotation> it) -> {
            boolean _containsKey = result.getContexts().containsKey(it.getKey());
            boolean _not = (!_containsKey);
            if (_not) {
                final ActiveAnnotationContext fa = new ActiveAnnotationContext();
                fa.setCompilationUnit(compilationUnit);
                final JvmType processorType = this._xAnnotationExtensions.getProcessorType(it.getKey());
                try {
                    final Object processorInstance = this._processorInstanceForJvmTypeProvider.getProcessorInstance(processorType);
                    if ((processorInstance == null)) {
                        String _identifier = processorType.getIdentifier();
                        String _plus = ("Couldn\'t instantiate the annotation processor of type \'" + _identifier);
                        String _plus_1 = (_plus + "\'. This is usually the case when the processor resides in the same project as the annotated element.");
                        throw new IllegalStateException(_plus_1);
                    }
                    fa.setProcessorInstance(processorInstance);
                } catch (final Throwable _t) {
                    if (_t instanceof VirtualMachineError) {
                        final VirtualMachineError e = (VirtualMachineError) _t;
                        throw e;
                    } else if (_t instanceof Throwable) {
                        final Throwable e_1 = (Throwable) _t;
                        this.operationCanceledManager.propagateAsErrorIfCancelException(e_1);
                        String _switchResult = null;
                        boolean _matched = false;
                        if (e_1 instanceof ExceptionInInitializerError) {
                            _matched = true;
                            _switchResult = ((ExceptionInInitializerError) e_1).getException().getMessage();
                        }
                        if (!_matched) {
                            _switchResult = e_1.getMessage();
                        }
                        final String msg = _switchResult;
                        EList<Resource.Diagnostic> _errors = file.eResource().getErrors();
                        StringConcatenation _builder = new StringConcatenation();
                        _builder.append("Problem while loading annotation processor: ");
                        _builder.append(msg);
                        XAnnotation _value = it.getValue();
                        EObjectDiagnosticImpl _eObjectDiagnosticImpl = new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.PROCESSING_ERROR, _builder.toString(), _value, XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, (-1), null);
                        _errors.add(_eObjectDiagnosticImpl);
                    } else {
                        throw Exceptions.sneakyThrow(_t);
                    }
                }
                result.getContexts().put(it.getKey(), fa);
            }
            List<XtendAnnotationTarget> _annotatedSourceElements = result.getContexts().get(it.getKey()).getAnnotatedSourceElements();
            XtendAnnotationTarget _annotatedTarget = this._xAnnotationExtensions.getAnnotatedTarget(it.getValue());
            _annotatedSourceElements.add(_annotatedTarget);
        };
        this.searchAnnotatedElements(file, _function);
        return result;
    } catch (final Throwable _t) {
        if (_t instanceof Throwable) {
            final Throwable e = (Throwable) _t;
            this.operationCanceledManager.propagateAsErrorIfCancelException(e);
            boolean _matched = false;
            if (e instanceof VirtualMachineError) {
                _matched = true;
                throw ((VirtualMachineError) e);
            }
            if (!_matched) {
                if (e instanceof LinkageError) {
                    _matched = true;
                    throw ((LinkageError) e);
                }
            }
            ActiveAnnotationContextProvider.logger.warn("Error finding the elements to be processed by active annotations", e);
            return ActiveAnnotationContexts.installNew(file.eResource());
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    } finally {
        task.stop();
    }
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) Stopwatches(org.eclipse.xtext.util.internal.Stopwatches) CompilationUnitImpl(org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl) Resource(org.eclipse.emf.ecore.resource.Resource) JvmType(org.eclipse.xtext.common.types.JvmType) EList(org.eclipse.emf.common.util.EList) EObjectDiagnosticImpl(org.eclipse.xtext.validation.EObjectDiagnosticImpl) XtendAnnotationTarget(org.eclipse.xtend.core.xtend.XtendAnnotationTarget) ActiveAnnotationContexts(org.eclipse.xtend.core.macro.ActiveAnnotationContexts) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ActiveAnnotationContext(org.eclipse.xtend.core.macro.ActiveAnnotationContext) EObject(org.eclipse.emf.ecore.EObject) EList(org.eclipse.emf.common.util.EList) List(java.util.List) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 18 with JvmAnnotationType

use of org.eclipse.xtext.common.types.JvmAnnotationType in project xtext-xtend by eclipse.

the class ActiveAnnotationContextProvider method registerMacroAnnotations.

private void registerMacroAnnotations(final XtendAnnotationTarget candidate, final IAcceptor<Pair<JvmAnnotationType, XAnnotation>> acceptor) {
    final Function1<XAnnotation, Boolean> _function = (XAnnotation it) -> {
        return Boolean.valueOf(this._xAnnotationExtensions.isProcessed(it));
    };
    Iterable<XAnnotation> _filter = IterableExtensions.<XAnnotation>filter(candidate.getAnnotations(), _function);
    for (final XAnnotation annotation : _filter) {
        {
            final JvmAnnotationType activeAnnotationDeclaration = this._xAnnotationExtensions.tryFindAnnotationType(annotation);
            if ((activeAnnotationDeclaration != null)) {
                boolean _isValid = this.isValid(annotation, activeAnnotationDeclaration);
                if (_isValid) {
                    Pair<JvmAnnotationType, XAnnotation> _mappedTo = Pair.<JvmAnnotationType, XAnnotation>of(activeAnnotationDeclaration, annotation);
                    acceptor.accept(_mappedTo);
                }
            }
        }
    }
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 19 with JvmAnnotationType

use of org.eclipse.xtext.common.types.JvmAnnotationType in project xtext-xtend by eclipse.

the class XtendJvmModelInferrer method doInferTypeSceleton.

protected JvmDeclaredType doInferTypeSceleton(final XtendTypeDeclaration declaration, final IJvmDeclaredTypeAcceptor acceptor, boolean preIndexingPhase, XtendFile xtendFile, List<Runnable> doLater) {
    if (Strings.isEmpty(declaration.getName()))
        return null;
    if (declaration instanceof XtendAnnotationType) {
        final JvmAnnotationType annotation = typesFactory.createJvmAnnotationType();
        if (!preIndexingPhase) {
            doLater.add(new Runnable() {

                @Override
                public void run() {
                    initialize((XtendAnnotationType) declaration, annotation);
                }
            });
        }
        return annotation;
    } else if (declaration instanceof XtendClass) {
        XtendClass xtendClass = (XtendClass) declaration;
        final JvmGenericType javaType = typesFactory.createJvmGenericType();
        copyTypeParameters(xtendClass.getTypeParameters(), javaType);
        if (!preIndexingPhase) {
            doLater.add(new Runnable() {

                @Override
                public void run() {
                    initialize((XtendClass) declaration, javaType);
                }
            });
        }
        return javaType;
    } else if (declaration instanceof XtendInterface) {
        XtendInterface xtendInterface = (XtendInterface) declaration;
        final JvmGenericType javaType = typesFactory.createJvmGenericType();
        javaType.setInterface(true);
        copyTypeParameters(xtendInterface.getTypeParameters(), javaType);
        if (!preIndexingPhase) {
            doLater.add(new Runnable() {

                @Override
                public void run() {
                    initialize((XtendInterface) declaration, javaType);
                }
            });
        }
        return javaType;
    } else if (declaration instanceof XtendEnum) {
        final JvmEnumerationType javaType = typesFactory.createJvmEnumerationType();
        if (!preIndexingPhase) {
            doLater.add(new Runnable() {

                @Override
                public void run() {
                    initialize((XtendEnum) declaration, javaType);
                }
            });
        }
        return javaType;
    } else {
        return null;
    }
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) XtendAnnotationType(org.eclipse.xtend.core.xtend.XtendAnnotationType) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendInterface(org.eclipse.xtend.core.xtend.XtendInterface) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) XtendEnum(org.eclipse.xtend.core.xtend.XtendEnum) JvmEnumerationType(org.eclipse.xtext.common.types.JvmEnumerationType)

Example 20 with JvmAnnotationType

use of org.eclipse.xtext.common.types.JvmAnnotationType in project xtext-xtend by eclipse.

the class JvmModelTests method testEnumInAnnotationIsStatic.

@Test
public void testEnumInAnnotationIsStatic() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("annotation Foo {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("enum E { L0, L1 }");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final JvmAnnotationType inferred = this._iXtendJvmAssociations.getInferredAnnotationType(this.annotationType(_builder.toString()));
        JvmMember _head = IterableExtensions.<JvmMember>head(inferred.getMembers());
        final JvmEnumerationType e = ((JvmEnumerationType) _head);
        Assert.assertTrue(e.isStatic());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmEnumerationType(org.eclipse.xtext.common.types.JvmEnumerationType) Test(org.junit.Test)

Aggregations

JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)26 JvmType (org.eclipse.xtext.common.types.JvmType)9 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)8 JvmEnumerationType (org.eclipse.xtext.common.types.JvmEnumerationType)6 EObject (org.eclipse.emf.ecore.EObject)5 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)5 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)5 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)5 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)5 JvmAnnotationReference (org.eclipse.xtext.common.types.JvmAnnotationReference)4 JvmMember (org.eclipse.xtext.common.types.JvmMember)4 Test (org.junit.Test)4 InternalEObject (org.eclipse.emf.ecore.InternalEObject)3 JvmAnnotationTypeDeclarationImpl (org.eclipse.xtend.core.macro.declaration.JvmAnnotationTypeDeclarationImpl)3 XAnnotation (org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation)3 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)3 URI (org.eclipse.emf.common.util.URI)2 XtendAnnotationType (org.eclipse.xtend.core.xtend.XtendAnnotationType)2 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)2 XtendConstructor (org.eclipse.xtend.core.xtend.XtendConstructor)2