Search in sources :

Example 1 with IJvmDeclaredTypeAcceptor

use of org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor 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 IJvmDeclaredTypeAcceptor

use of org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor in project xtext-xtend by eclipse.

the class Case_14 method testInference.

@Test
public void testInference() {
    final Provider<IJvmModelInferrer> _function = () -> {
        final IJvmModelInferrer _function_1 = (EObject obj, IJvmDeclaredTypeAcceptor acceptor, boolean preIndexing) -> {
            final JvmGenericType firstType = this._jvmTypesBuilder.toClass(obj, "foo.Bar");
            final JvmGenericType secondType = this._jvmTypesBuilder.toClass(obj, "foo.Baz");
            Assert.assertNull(secondType.eResource());
            final Procedure1<JvmGenericType> _function_2 = (JvmGenericType it) -> {
                it.setAbstract(true);
                Assert.assertNotNull(firstType.eResource());
                Assert.assertNotNull(secondType.eResource());
            };
            acceptor.<JvmGenericType>accept(firstType, _function_2);
            final Procedure1<JvmGenericType> _function_3 = (JvmGenericType it) -> {
                it.setAbstract(true);
                Assert.assertNotNull(firstType.eResource());
            };
            acceptor.<JvmGenericType>accept(secondType, _function_3);
        };
        return _function_1;
    };
    this.assoc.setInferrerProvider(_function);
    this.resource.setDerivedStateComputer(null);
    this.resource.setURI(URI.createURI("foo.txt"));
    this.resourceSet.setClasspathURIContext(this.getClass());
    EList<Resource> _resources = this.resourceSet.getResources();
    this._jvmTypesBuilder.<DerivedStateAwareResource>operator_add(_resources, this.resource);
    EList<EObject> _contents = this.resource.getContents();
    EClass _createEClass = EcoreFactory.eINSTANCE.createEClass();
    this._jvmTypesBuilder.<EClass>operator_add(_contents, _createEClass);
    this.assoc.installDerivedState(this.resource, true);
    EObject _get = this.resource.getContents().get(1);
    Assert.assertFalse(((JvmDeclaredType) _get).isAbstract());
    this.resource.getContents().clear();
    EList<EObject> _contents_1 = this.resource.getContents();
    EClass _createEClass_1 = EcoreFactory.eINSTANCE.createEClass();
    this._jvmTypesBuilder.<EClass>operator_add(_contents_1, _createEClass_1);
    this.assoc.installDerivedState(this.resource, false);
    EObject _get_1 = this.resource.getContents().get(1);
    final JvmGenericType type = ((JvmGenericType) _get_1);
    Assert.assertTrue(type.isAbstract());
    Assert.assertEquals(1, IterableExtensions.size(Iterables.<JvmConstructor>filter(type.getMembers(), JvmConstructor.class)));
    JvmTypeReference _head = IterableExtensions.<JvmTypeReference>head(type.getSuperTypes());
    String _qualifiedName = null;
    if (_head != null) {
        _qualifiedName = _head.getQualifiedName();
    }
    Assert.assertEquals("java.lang.Object", _qualifiedName);
}
Also used : IJvmModelInferrer(org.eclipse.xtext.xbase.jvmmodel.IJvmModelInferrer) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) EClass(org.eclipse.emf.ecore.EClass) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) IJvmDeclaredTypeAcceptor(org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor) Test(org.junit.Test)

Aggregations

IJvmDeclaredTypeAcceptor (org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor)2 LinkedHashSet (java.util.LinkedHashSet)1 EClass (org.eclipse.emf.ecore.EClass)1 EObject (org.eclipse.emf.ecore.EObject)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 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)1 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)1 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)1 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)1 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)1 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)1 DerivedStateAwareResource (org.eclipse.xtext.resource.DerivedStateAwareResource)1 IJvmModelInferrer (org.eclipse.xtext.xbase.jvmmodel.IJvmModelInferrer)1 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)1 BatchLinkableResource (org.eclipse.xtext.xbase.resource.BatchLinkableResource)1 Test (org.junit.Test)1