use of org.eclipse.xtend.core.macro.ActiveAnnotationContext 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);
}
}
use of org.eclipse.xtend.core.macro.ActiveAnnotationContext 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);
}
}
use of org.eclipse.xtend.core.macro.ActiveAnnotationContext 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();
}
}
use of org.eclipse.xtend.core.macro.ActiveAnnotationContext 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);
}
}
Aggregations