Search in sources :

Example 6 with DerivedStateAwareResource

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

the class XtendResourceSetBasedResourceDescriptionsTest method testUnloadedInstallDerivedStateThrowsException.

@Test
public void testUnloadedInstallDerivedStateThrowsException() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("package foo class ClassA extends bar.ClassB {}");
        Pair<String, String> _mappedTo = Pair.<String, String>of("foo/ClassA.xtend", _builder.toString());
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("package bar class ClassB { public foo.ClassA myField }");
        Pair<String, String> _mappedTo_1 = Pair.<String, String>of("bar/ClassB.xtend", _builder_1.toString());
        final ResourceSet resourceSet = this.compiler.unLoadedResourceSet(_mappedTo, _mappedTo_1);
        final List<? extends Resource> resources = resourceSet.getResources();
        ArrayList<Resource> _arrayList = new ArrayList<Resource>(resources);
        for (final Resource res : _arrayList) {
            {
                Assert.assertFalse(res.isLoaded());
                try {
                    ((DerivedStateAwareResource) res).installDerivedState(true);
                    Assert.fail("expected exception");
                } catch (final Throwable _t) {
                    if (_t instanceof IllegalStateException) {
                    } else {
                        throw Exceptions.sneakyThrow(_t);
                    }
                }
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) ArrayList(java.util.ArrayList) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Test(org.junit.Test)

Example 7 with DerivedStateAwareResource

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

the class IndexingTest method doTestResourceDescriptionWithoutGetContents.

private void doTestResourceDescriptionWithoutGetContents(final String input) throws IOException {
    XtextResourceSet resourceSet = getResourceSet();
    new ClasspathTypeProvider(classLoader, resourceSet, null, null);
    final Wrapper<Boolean> wrapper = Wrapper.wrap(Boolean.FALSE);
    for (int i = 0; i < 10; i++) {
        DerivedStateAwareResource resource = (DerivedStateAwareResource) resourceSet.createResource(URI.createURI("Dummy" + i + ".xtend"));
        resource.setDerivedStateComputer(new IDerivedStateComputer() {

            @Override
            public void installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) {
                if (!preLinkingPhase) {
                    wrapper.set(Boolean.TRUE);
                }
                derivedStateComputer.installDerivedState(resource, preLinkingPhase);
            }

            @Override
            public void discardDerivedState(DerivedStateAwareResource resource) {
                derivedStateComputer.discardDerivedState(resource);
            }
        });
        String actualInput = input;
        if (i != 0) {
            actualInput = "import C" + (i + 1) + " " + actualInput;
            actualInput = "import C" + (i - 1) + " " + actualInput;
        }
        actualInput = String.format(actualInput, i, i + 1);
        resource.load(new StringInputStream(actualInput), null);
    }
    for (int i = 0; i < 10; i++) {
        Resource resource = resourceSet.getResources().get(i);
        for (IEObjectDescription description : resourceDescriptionManager.getResourceDescription(resource).getExportedObjects()) {
            description.getEObjectOrProxy();
        }
    }
    assertFalse(wrapper.get());
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) XtextResource(org.eclipse.xtext.resource.XtextResource) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) IDerivedStateComputer(org.eclipse.xtext.resource.IDerivedStateComputer) ClasspathTypeProvider(org.eclipse.xtext.common.types.access.impl.ClasspathTypeProvider) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) StringInputStream(org.eclipse.xtext.util.StringInputStream) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet)

Example 8 with DerivedStateAwareResource

use of org.eclipse.xtext.resource.DerivedStateAwareResource 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)

Example 9 with DerivedStateAwareResource

use of org.eclipse.xtext.resource.DerivedStateAwareResource in project xtext-eclipse by eclipse.

the class DefaultJvmModelRenameStrategy method setInferredJvmElementName.

protected void setInferredJvmElementName(String name, EObject renamedElement) {
    // the inferred model.
    if (renamedElement.eResource() instanceof DerivedStateAwareResource) {
        DerivedStateAwareResource resource = (DerivedStateAwareResource) renamedElement.eResource();
        resource.discardDerivedState();
        resource.installDerivedState(false);
    }
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource)

Example 10 with DerivedStateAwareResource

use of org.eclipse.xtext.resource.DerivedStateAwareResource in project dsl-devkit by dsldevkit.

the class TransformUtil method lookupEPackage.

/**
 * Looks up an EPackage visible in the context of a grammar.
 *
 * @param grammar
 *          context grammar
 * @param packageName
 *          package name
 * @return EPackage or null
 */
public static EPackage lookupEPackage(final Grammar grammar, final String packageName) {
    try {
        for (AbstractMetamodelDeclaration decl : grammar.getMetamodelDeclarations()) {
            if (decl.getEPackage() != null && packageName.equals(decl.getEPackage().getName())) {
                return decl.getEPackage();
            }
        }
        ResourceSet resourceSet = grammar.eResource().getResourceSet();
        loadImplicitMetamodels(resourceSet);
        // With DerivedStateAwareResources, calling getContents() may cause linking, which may load additional resources into the resource set.
        // If that happens, we get a ConcurrentModificationException in this loop if we just do
        // 
        // for (Resource res : resourceSet.getResources()) { ... }
        // 
        // Try to guard against that by explicitly copying the list. Since there may be other resource types that may do equally crazy things
        // upon getContents(), we do *not* try to guard by only calling getContents() if (!(res instanceof DerivedStateAwareResource)).
        List<Resource> resources = new ArrayList<Resource>(resourceSet.getResources());
        for (Resource res : resources) {
            // language resources. GrammarResources invoke linking here!
            if (!(res instanceof DerivedStateAwareResource) && !res.getContents().isEmpty() && res.getContents().get(0) instanceof EPackage) {
                EPackage epkg = (EPackage) res.getContents().get(0);
                if (packageName.equals(epkg.getName())) {
                    return epkg;
                }
            }
        }
        Registry reg = resourceSet.getPackageRegistry();
        for (String uri : reg.keySet()) {
            EPackage epkg = reg.getEPackage(uri);
            if (packageName.equals(epkg.getName())) {
                return epkg;
            }
        }
    // CHECKSTYLE:OFF
    } catch (Exception e) {
        // CHECKSTYLE:ON
        LOG.error("error looking up package " + packageName, e);
    }
    LOG.info("no package found with name " + packageName);
    return null;
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) ArrayList(java.util.ArrayList) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Registry(org.eclipse.emf.ecore.EPackage.Registry) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) EPackage(org.eclipse.emf.ecore.EPackage)

Aggregations

DerivedStateAwareResource (org.eclipse.xtext.resource.DerivedStateAwareResource)10 Resource (org.eclipse.emf.ecore.resource.Resource)9 EObject (org.eclipse.emf.ecore.EObject)4 Test (org.junit.Test)4 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 XtextResource (org.eclipse.xtext.resource.XtextResource)3 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)3 ArrayList (java.util.ArrayList)2 URI (org.eclipse.emf.common.util.URI)2 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 Grammar (org.eclipse.xtext.Grammar)2 ValueConverterException (org.eclipse.xtext.conversion.ValueConverterException)2 IParseResult (org.eclipse.xtext.parser.IParseResult)2 ClasspathUriResolutionException (org.eclipse.xtext.resource.ClasspathUriResolutionException)2 EClass (org.eclipse.emf.ecore.EClass)1 EPackage (org.eclipse.emf.ecore.EPackage)1 Registry (org.eclipse.emf.ecore.EPackage.Registry)1 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)1 EcoreUtil (org.eclipse.emf.ecore.util.EcoreUtil)1 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)1