Search in sources :

Example 1 with BatchLinkableResource

use of org.eclipse.xtext.xbase.resource.BatchLinkableResource 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 BatchLinkableResource

use of org.eclipse.xtext.xbase.resource.BatchLinkableResource in project xtext-xtend by eclipse.

the class ResourceStorageTest method testFailedWrite.

@Test(expected = IOException.class)
public void testFailedWrite() throws Exception {
    final XtendFile file = this.file("class C{}");
    ByteArrayOutputStream _byteArrayOutputStream = new ByteArrayOutputStream();
    Resource _eResource = file.eResource();
    new BatchLinkableResourceStorageWritable(_byteArrayOutputStream, false) {

        @Override
        protected void writeAssociationsAdapter(final BatchLinkableResource resource, final OutputStream zipOut) throws IOException {
            final Function1<Adapter, Boolean> _function = (Adapter it) -> {
                return Boolean.valueOf((it instanceof JvmModelAssociator.Adapter));
            };
            final Adapter removeMe = IterableExtensions.<Adapter>findFirst(resource.eAdapters(), _function);
            Assert.assertTrue(resource.eAdapters().remove(removeMe));
            super.writeAssociationsAdapter(resource, zipOut);
        }
    }.writeResource(((StorageAwareResource) _eResource));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Resource(org.eclipse.emf.ecore.resource.Resource) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) BatchLinkableResourceStorageWritable(org.eclipse.xtext.xbase.resource.BatchLinkableResourceStorageWritable) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) DocumentationAdapter(org.eclipse.xtext.xbase.compiler.DocumentationAdapter) Adapter(org.eclipse.emf.common.notify.Adapter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) Test(org.junit.Test)

Example 3 with BatchLinkableResource

use of org.eclipse.xtext.xbase.resource.BatchLinkableResource in project xtext-xtend by eclipse.

the class ConstantExpressionsInterpreterTest method testNonConstant.

@Test
public void testNonConstant() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("class C { ");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public static final Class<?> REF = D.testFoo;");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        _builder.newLine();
        _builder.append("class D {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public static final Class<?> testFoo = Object");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final XtendFile file = this.file(_builder.toString());
        Resource _eResource = file.eResource();
        ((BatchLinkableResource) _eResource).resolveLazyCrossReferences(null);
        final XtendField field = IterableExtensions.<XtendField>head(Iterables.<XtendField>filter(IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes()).getMembers(), XtendField.class));
        try {
            this.interpreter.evaluate(field.getInitialValue(), field.getType());
            Assert.fail("exception expected");
        } catch (final Throwable _t) {
            if (_t instanceof ConstantExpressionEvaluationException) {
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) ConstantExpressionEvaluationException(org.eclipse.xtext.xbase.interpreter.ConstantExpressionEvaluationException) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Resource(org.eclipse.emf.ecore.resource.Resource) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Aggregations

XtendFile (org.eclipse.xtend.core.xtend.XtendFile)3 BatchLinkableResource (org.eclipse.xtext.xbase.resource.BatchLinkableResource)3 Resource (org.eclipse.emf.ecore.resource.Resource)2 Test (org.junit.Test)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 LinkedHashSet (java.util.LinkedHashSet)1 Adapter (org.eclipse.emf.common.notify.Adapter)1 ActiveAnnotationContext (org.eclipse.xtend.core.macro.ActiveAnnotationContext)1 ActiveAnnotationContexts (org.eclipse.xtend.core.macro.ActiveAnnotationContexts)1 XtendField (org.eclipse.xtend.core.xtend.XtendField)1 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)1 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)1 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)1 StorageAwareResource (org.eclipse.xtext.resource.persistence.StorageAwareResource)1 DocumentationAdapter (org.eclipse.xtext.xbase.compiler.DocumentationAdapter)1 ConstantExpressionEvaluationException (org.eclipse.xtext.xbase.interpreter.ConstantExpressionEvaluationException)1 IJvmDeclaredTypeAcceptor (org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor)1 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)1