Search in sources :

Example 1 with ActiveAnnotationContexts

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

the class XtendJvmModelInferrer method infer.

@Override
public void infer(/* @Nullable */
EObject object, final /* @NonNull */
IJvmDeclaredTypeAcceptor acceptor, boolean preIndexingPhase) {
    if (!(object instanceof XtendFile))
        return;
    final Set<JvmDeclaredType> types = new LinkedHashSet<JvmDeclaredType>();
    final IJvmDeclaredTypeAcceptor wrapper = new IJvmDeclaredTypeAcceptor() {

        @SuppressWarnings("deprecation")
        @Override
        public <T extends JvmDeclaredType> IPostIndexingInitializing<T> accept(T type) {
            types.add(type);
            return acceptor.accept(type);
        }

        @Override
        public <T extends JvmDeclaredType> void accept(T type, Procedure1<? super T> lateInitialization) {
            types.add(type);
            acceptor.accept(type, lateInitialization);
        }
    };
    final XtendFile xtendFile = (XtendFile) object;
    generatorConfig = generatorConfigProvider.get(xtendFile);
    final List<Runnable> doLater = newArrayList();
    for (final XtendTypeDeclaration declaration : xtendFile.getXtendTypes()) {
        inferTypeSceleton(declaration, wrapper, preIndexingPhase, xtendFile, doLater, null);
    }
    ActiveAnnotationContexts contexts = null;
    BatchLinkableResource resource = (BatchLinkableResource) xtendFile.eResource();
    try {
        compilerPhases.setIndexing(xtendFile, true);
        try {
            contexts = contextProvider.computeContext(xtendFile);
        } catch (Throwable t) {
            operationCanceledManager.propagateAsErrorIfCancelException(t);
            logger.error("Couldn't create annotation contexts", t);
            return;
        }
        try {
            contexts.before(ActiveAnnotationContexts.AnnotationCallback.INDEXING);
            for (ActiveAnnotationContext ctx : contexts.getContexts().values()) {
                try {
                    annotationProcessor.indexingPhase(ctx, wrapper, CancelIndicator.NullImpl);
                } catch (Throwable t) {
                    operationCanceledManager.propagateAsErrorIfCancelException(t);
                    ctx.handleProcessingError(xtendFile.eResource(), t);
                }
            }
        } finally {
            contexts.after(ActiveAnnotationContexts.AnnotationCallback.INDEXING);
        }
    } finally {
        compilerPhases.setIndexing(xtendFile, false);
        resource.getCache().clear(resource);
    }
    if (!preIndexingPhase) {
        final ActiveAnnotationContexts finalContexts = contexts;
        Runnable lateInit = new Runnable() {

            @Override
            public void run() {
                for (Runnable runnable : doLater) {
                    runnable.run();
                }
                try {
                    finalContexts.before(ActiveAnnotationContexts.AnnotationCallback.INFERENCE);
                    for (ActiveAnnotationContext ctx : finalContexts.getContexts().values()) {
                        try {
                            annotationProcessor.inferencePhase(ctx, CancelIndicator.NullImpl);
                        } catch (Throwable t) {
                            operationCanceledManager.propagateAsErrorIfCancelException(t);
                            ctx.handleProcessingError(xtendFile.eResource(), t);
                        }
                    }
                } finally {
                    finalContexts.after(ActiveAnnotationContexts.AnnotationCallback.INFERENCE);
                }
            }
        };
        resource.addJvmMemberInitializer(lateInit);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) LinkedHashSet(java.util.LinkedHashSet) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ActiveAnnotationContexts(org.eclipse.xtend.core.macro.ActiveAnnotationContexts) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) ActiveAnnotationContext(org.eclipse.xtend.core.macro.ActiveAnnotationContext) IJvmDeclaredTypeAcceptor(org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor)

Example 2 with ActiveAnnotationContexts

use of org.eclipse.xtend.core.macro.ActiveAnnotationContexts 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)

Example 3 with ActiveAnnotationContexts

use of org.eclipse.xtend.core.macro.ActiveAnnotationContexts 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 4 with ActiveAnnotationContexts

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

the class CachingResourceValidatorImpl method runActiveAnnotationValidation.

private void runActiveAnnotationValidation(final Resource resource, final CancelIndicator monitor) {
    final ActiveAnnotationContexts contexts = ActiveAnnotationContexts.find(resource);
    if ((contexts == null)) {
        return;
    }
    try {
        contexts.before(ActiveAnnotationContexts.AnnotationCallback.VALIDATION);
        Collection<ActiveAnnotationContext> _values = contexts.getContexts().values();
        for (final ActiveAnnotationContext ctx : _values) {
            {
                this.operationCanceledManager.checkCanceled(monitor);
                try {
                    this.annotationProcessor.validationPhase(ctx, monitor);
                } catch (final Throwable _t) {
                    if (_t instanceof Throwable) {
                        final Throwable t = (Throwable) _t;
                        this.operationCanceledManager.propagateAsErrorIfCancelException(t);
                        ctx.handleProcessingError(resource, t);
                    } else {
                        throw Exceptions.sneakyThrow(_t);
                    }
                }
            }
        }
    } finally {
        contexts.after(ActiveAnnotationContexts.AnnotationCallback.VALIDATION);
    }
}
Also used : ActiveAnnotationContexts(org.eclipse.xtend.core.macro.ActiveAnnotationContexts) ActiveAnnotationContext(org.eclipse.xtend.core.macro.ActiveAnnotationContext)

Aggregations

ActiveAnnotationContext (org.eclipse.xtend.core.macro.ActiveAnnotationContext)4 ActiveAnnotationContexts (org.eclipse.xtend.core.macro.ActiveAnnotationContexts)4 EObject (org.eclipse.emf.ecore.EObject)2 XtendAnnotationTarget (org.eclipse.xtend.core.xtend.XtendAnnotationTarget)2 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 EList (org.eclipse.emf.common.util.EList)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 CodeGenerationContextImpl (org.eclipse.xtend.core.macro.CodeGenerationContextImpl)1 CompilationUnitImpl (org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl)1 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)1 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)1 CodeGenerationParticipant (org.eclipse.xtend.lib.macro.CodeGenerationParticipant)1 MemberDeclaration (org.eclipse.xtend.lib.macro.declaration.MemberDeclaration)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)1 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)1 JvmType (org.eclipse.xtext.common.types.JvmType)1 Stopwatches (org.eclipse.xtext.util.internal.Stopwatches)1 EObjectDiagnosticImpl (org.eclipse.xtext.validation.EObjectDiagnosticImpl)1