Search in sources :

Example 6 with XtendAnnotationTarget

use of org.eclipse.xtend.core.xtend.XtendAnnotationTarget in project xtext-xtend by eclipse.

the class XAnnotationExtensions method getAnnotatedTarget.

public XtendAnnotationTarget getAnnotatedTarget(final XAnnotation annotation) {
    XtendAnnotationTarget _switchResult = null;
    EObject _eContainer = annotation.eContainer();
    final EObject container = _eContainer;
    boolean _matched = false;
    if (container instanceof XtendAnnotationType) {
        _matched = true;
    }
    if (!_matched) {
        if (container instanceof XtendClass) {
            _matched = true;
        }
    }
    if (!_matched) {
        if (container instanceof XtendInterface) {
            _matched = true;
        }
    }
    if (!_matched) {
        if (container instanceof XtendEnum) {
            _matched = true;
        }
    }
    if (!_matched) {
        if (container instanceof XtendField) {
            _matched = true;
        }
    }
    if (!_matched) {
        if (container instanceof XtendFunction) {
            _matched = true;
        }
    }
    if (!_matched) {
        if (container instanceof XtendConstructor) {
            _matched = true;
        }
    }
    if (!_matched) {
        if (container instanceof XtendEnumLiteral) {
            _matched = true;
        }
    }
    if (!_matched) {
        if (container instanceof XtendParameter) {
            _matched = true;
        }
    }
    if (_matched) {
        _switchResult = ((XtendAnnotationTarget) container);
    }
    if (!_matched) {
        if (container instanceof XtendAnnotationTarget) {
            _matched = true;
            XtendAnnotationTarget _xblockexpression = null;
            {
                final EObject containerContainer = ((XtendAnnotationTarget) container).eContainer();
                XtendAnnotationTarget _xifexpression = null;
                if ((containerContainer instanceof XtendAnnotationTarget)) {
                    _xifexpression = ((XtendAnnotationTarget) containerContainer);
                } else {
                    _xifexpression = ((XtendAnnotationTarget) container);
                }
                _xblockexpression = _xifexpression;
            }
            _switchResult = _xblockexpression;
        }
    }
    if (!_matched) {
        if (container instanceof XAnnotation) {
            _matched = true;
            _switchResult = this.getAnnotatedTarget(((XAnnotation) container));
        }
    }
    if (!_matched) {
        _switchResult = null;
    }
    return _switchResult;
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XtendConstructor(org.eclipse.xtend.core.xtend.XtendConstructor) XtendField(org.eclipse.xtend.core.xtend.XtendField) XtendParameter(org.eclipse.xtend.core.xtend.XtendParameter) XtendAnnotationTarget(org.eclipse.xtend.core.xtend.XtendAnnotationTarget) XtendAnnotationType(org.eclipse.xtend.core.xtend.XtendAnnotationType) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendInterface(org.eclipse.xtend.core.xtend.XtendInterface) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) XtendEnum(org.eclipse.xtend.core.xtend.XtendEnum) XtendEnumLiteral(org.eclipse.xtend.core.xtend.XtendEnumLiteral)

Example 7 with XtendAnnotationTarget

use of org.eclipse.xtend.core.xtend.XtendAnnotationTarget in project xtext-xtend by eclipse.

the class CompilationUnitImpl method handleProcessingError.

public void handleProcessingError(final Iterable<? extends EObject> sourceElements, final Resource resource, final Throwable t) {
    if ((t instanceof VirtualMachineError)) {
        throw ((VirtualMachineError) t);
    }
    boolean _equals = Objects.equal(this.lastPhase, ActiveAnnotationContexts.AnnotationCallback.GENERATION);
    if (_equals) {
        Throwables.throwIfUnchecked(t);
        String _messageWithoutStackTrace = this.getMessageWithoutStackTrace(t);
        throw new RuntimeException(_messageWithoutStackTrace, t);
    }
    final String msg = this.getMessageWithStackTrace(t);
    final EList<Resource.Diagnostic> errors = resource.getErrors();
    for (final EObject target : sourceElements) {
        boolean _matched = false;
        if (target instanceof XtendAnnotationTarget) {
            _matched = true;
            final EList<XAnnotation> annotations = ((XtendAnnotationTarget) target).getAnnotations();
            EObject _xifexpression = null;
            boolean _isEmpty = annotations.isEmpty();
            if (_isEmpty) {
                _xifexpression = target;
            } else {
                _xifexpression = IterableExtensions.<XAnnotation>head(annotations);
            }
            EObjectDiagnosticImpl _eObjectDiagnosticImpl = new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.PROCESSING_ERROR, msg, _xifexpression, null, (-1), null);
            errors.add(_eObjectDiagnosticImpl);
        }
        if (!_matched) {
            EObjectDiagnosticImpl _eObjectDiagnosticImpl = new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.PROCESSING_ERROR, msg, target, null, (-1), null);
            errors.add(_eObjectDiagnosticImpl);
        }
    }
}
Also used : XtendAnnotationTarget(org.eclipse.xtend.core.xtend.XtendAnnotationTarget) EObjectDiagnosticImpl(org.eclipse.xtext.validation.EObjectDiagnosticImpl) EObject(org.eclipse.emf.ecore.EObject) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation)

Example 8 with XtendAnnotationTarget

use of org.eclipse.xtend.core.xtend.XtendAnnotationTarget 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);
                        StringConcatenation _builder = new StringConcatenation();
                        _builder.append("Problem while loading annotation processor: ");
                        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();
                        }
                        _builder.append(_switchResult);
                        final String msg = _builder.toString();
                        ActiveAnnotationContextProvider.logger.error(msg, e_1);
                        EList<Resource.Diagnostic> _errors = file.eResource().getErrors();
                        XAnnotation _value = it.getValue();
                        EObjectDiagnosticImpl _eObjectDiagnosticImpl = new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.PROCESSING_ERROR, msg, _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) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) 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 9 with XtendAnnotationTarget

use of org.eclipse.xtend.core.xtend.XtendAnnotationTarget in project xtext-xtend by eclipse.

the class XtendGenerator method callMacroProcessors.

public void callMacroProcessors(final Resource input) {
    final ActiveAnnotationContexts ctxs = ActiveAnnotationContexts.find(input);
    if ((ctxs == null)) {
        return;
    }
    try {
        ctxs.before(ActiveAnnotationContexts.AnnotationCallback.GENERATION);
        Collection<ActiveAnnotationContext> _values = ctxs.getContexts().values();
        for (final ActiveAnnotationContext context : _values) {
            try {
                Object _processorInstance = context.getProcessorInstance();
                final Object processor = _processorInstance;
                boolean _matched = false;
                if (processor instanceof CodeGenerationParticipant) {
                    _matched = true;
                    CodeGenerationContextImpl _codeGenerationContextImpl = new CodeGenerationContextImpl();
                    final Procedure1<CodeGenerationContextImpl> _function = (CodeGenerationContextImpl it) -> {
                        it.setUnit(context.getCompilationUnit());
                    };
                    final CodeGenerationContextImpl codeGenServices = ObjectExtensions.<CodeGenerationContextImpl>operator_doubleArrow(_codeGenerationContextImpl, _function);
                    final Function1<XtendAnnotationTarget, MemberDeclaration> _function_1 = (XtendAnnotationTarget it) -> {
                        return context.getCompilationUnit().toXtendMemberDeclaration(((XtendMember) it));
                    };
                    final List<MemberDeclaration> elements = ListExtensions.<XtendAnnotationTarget, MemberDeclaration>map(context.getAnnotatedSourceElements(), _function_1);
                    ((CodeGenerationParticipant<NamedElement>) processor).doGenerateCode(elements, codeGenServices);
                }
            } catch (final Throwable _t) {
                if (_t instanceof Throwable) {
                    final Throwable t = (Throwable) _t;
                    this.operationCanceledManager.propagateAsErrorIfCancelException(t);
                    context.handleProcessingError(input, t);
                } else {
                    throw Exceptions.sneakyThrow(_t);
                }
            }
        }
    } finally {
        ctxs.after(ActiveAnnotationContexts.AnnotationCallback.GENERATION);
    }
}
Also used : MemberDeclaration(org.eclipse.xtend.lib.macro.declaration.MemberDeclaration) XtendAnnotationTarget(org.eclipse.xtend.core.xtend.XtendAnnotationTarget) ActiveAnnotationContexts(org.eclipse.xtend.core.macro.ActiveAnnotationContexts) ActiveAnnotationContext(org.eclipse.xtend.core.macro.ActiveAnnotationContext) CodeGenerationParticipant(org.eclipse.xtend.lib.macro.CodeGenerationParticipant) CodeGenerationContextImpl(org.eclipse.xtend.core.macro.CodeGenerationContextImpl) EObject(org.eclipse.emf.ecore.EObject)

Aggregations

XtendAnnotationTarget (org.eclipse.xtend.core.xtend.XtendAnnotationTarget)9 EObject (org.eclipse.emf.ecore.EObject)4 XtendParameter (org.eclipse.xtend.core.xtend.XtendParameter)4 List (java.util.List)3 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)3 Declaration (org.eclipse.xtend.lib.macro.declaration.Declaration)3 MutableNamedElement (org.eclipse.xtend.lib.macro.declaration.MutableNamedElement)3 NamedElement (org.eclipse.xtend.lib.macro.declaration.NamedElement)3 XAnnotation (org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation)3 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)3 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)2 Element (org.eclipse.xtend.lib.macro.declaration.Element)2 EObjectDiagnosticImpl (org.eclipse.xtext.validation.EObjectDiagnosticImpl)2 EList (org.eclipse.emf.common.util.EList)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ActiveAnnotationContext (org.eclipse.xtend.core.macro.ActiveAnnotationContext)1 ActiveAnnotationContexts (org.eclipse.xtend.core.macro.ActiveAnnotationContexts)1 CodeGenerationContextImpl (org.eclipse.xtend.core.macro.CodeGenerationContextImpl)1 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)1