Search in sources :

Example 1 with DerivedStateAwareResource

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

the class UnloadingTest method testProperUnloading.

@Test
public void testProperUnloading() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("class B {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("def void foo() {");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("new A(this)");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final String fileB = _builder.toString();
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("class A {");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("new (B b) {");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("}");
        _builder_1.newLine();
        _builder_1.append("}");
        _builder_1.newLine();
        final List<XtendFile> parsedFiles = IterableExtensions.<XtendFile>toList(this.files(true, _builder_1.toString(), fileB));
        Resource _eResource = parsedFiles.get(1).eResource();
        final DerivedStateAwareResource resource = ((DerivedStateAwareResource) _eResource);
        final Resource resourceA = IterableExtensions.<XtendFile>head(parsedFiles).eResource();
        resource.reparse(fileB);
        EObject _head = IterableExtensions.<EObject>head(resourceA.getContents());
        final XtendFile file = ((XtendFile) _head);
        XtendTypeDeclaration _head_1 = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
        XtendMember _head_2 = IterableExtensions.<XtendMember>head(((XtendClass) _head_1).getMembers());
        Assert.assertNotNull(IterableExtensions.<XtendParameter>head(((XtendConstructor) _head_2).getParameters()).getParameterType().getType().eResource());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendParameter(org.eclipse.xtend.core.xtend.XtendParameter) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) Test(org.junit.Test)

Example 2 with DerivedStateAwareResource

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

the class XtextLinkingService method getUsedGrammar.

private List<EObject> getUsedGrammar(Grammar grammar, INode node) {
    try {
        String grammarName = (String) valueConverterService.toValue("", "GrammarID", node);
        if (grammarName != null) {
            final ResourceSet resourceSet = grammar.eResource().getResourceSet();
            List<Resource> resources = resourceSet.getResources();
            for (int i = 0; i < resources.size(); i++) {
                Resource resource = resources.get(i);
                EObject rootElement = null;
                if (resource instanceof XtextResource) {
                    IParseResult parseResult = ((XtextResource) resource).getParseResult();
                    if (parseResult != null)
                        rootElement = parseResult.getRootASTElement();
                } else if (!resource.getContents().isEmpty()) {
                    rootElement = resource.getContents().get(0);
                }
                if (rootElement instanceof Grammar) {
                    Grammar otherGrammar = (Grammar) rootElement;
                    if (grammarName.equals(otherGrammar.getName())) {
                        if (resource instanceof DerivedStateAwareResource)
                            resource.getContents();
                        return Collections.<EObject>singletonList(otherGrammar);
                    }
                }
            }
            URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + "." + fileExtension);
            URI normalizedURI = null;
            if (resourceSet instanceof XtextResourceSet) {
                XtextResourceSet set = (XtextResourceSet) resourceSet;
                normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
            } else {
                normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
            }
            final Resource resource = resourceSet.getResource(normalizedURI, true);
            if (!resource.getContents().isEmpty()) {
                final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
                if (grammarName.equals(usedGrammar.getName()))
                    return Collections.<EObject>singletonList(usedGrammar);
            }
        }
        return Collections.emptyList();
    } catch (ClasspathUriResolutionException e) {
        log.debug("Cannot load used grammar.", e);
        return Collections.emptyList();
    } catch (ValueConverterException e) {
        log.debug("Cannot load used grammar.", e);
        return Collections.emptyList();
    }
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Grammar(org.eclipse.xtext.Grammar) URI(org.eclipse.emf.common.util.URI) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) IParseResult(org.eclipse.xtext.parser.IParseResult) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 3 with DerivedStateAwareResource

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

the class EcoreUtil2Test method testClone_2.

@Test
public void testClone_2() throws Exception {
    ResourceSetImpl sourceSet = new DerivedStateAwareResourceSet();
    DerivedStateAwareResource resource = (DerivedStateAwareResource) sourceSet.createResource(URI.createURI("http://derived.res"));
    boolean stateToCheck = !resource.isFullyInitialized();
    resource.setFullyInitialized(stateToCheck);
    Resource targetRes = EcoreUtil2.clone(new DerivedStateAwareResourceSet(), sourceSet).getResources().get(0);
    assertTrue(targetRes instanceof DerivedStateAwareResource);
    assertEquals("FullyInitialized flag not copied ", stateToCheck, ((DerivedStateAwareResource) targetRes).isFullyInitialized());
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) Test(org.junit.Test)

Example 4 with DerivedStateAwareResource

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

the class EcoreUtil2 method clone.

/**
 * copies contents of a resource set into a new one
 */
public static <T extends ResourceSet> T clone(T target, ResourceSet source) {
    EList<Resource> resources = source.getResources();
    EcoreUtil.Copier copier = new EcoreUtil.Copier();
    for (Resource resource : resources) {
        Resource targetResource = target.createResource(resource.getURI());
        targetResource.getContents().addAll(copier.copyAll(resource.getContents()));
        // mark all resources as fully initialized
        if (resource instanceof DerivedStateAwareResource && targetResource instanceof DerivedStateAwareResource) {
            ((DerivedStateAwareResource) targetResource).setFullyInitialized(((DerivedStateAwareResource) resource).isFullyInitialized());
        }
    }
    copier.copyReferences();
    return target;
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) LazyLinkingResource(org.eclipse.xtext.linking.lazy.LazyLinkingResource) Resource(org.eclipse.emf.ecore.resource.Resource) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil)

Example 5 with DerivedStateAwareResource

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

the class AbstractFastLinkingService method getUsedGrammar.

/**
 * Tries to find a grammar.
 *
 * @param resourceSet
 *          to use for loading
 * @param grammarName
 *          qualified grammar name
 * @return A singleton list containing the grammar, or an empty list if not found.
 */
protected List<EObject> getUsedGrammar(final ResourceSet resourceSet, final String grammarName) {
    // copied from XtextLinkingService#getUsedGrammar()
    try {
        if (grammarName != null) {
            List<Resource> resources = resourceSet.getResources();
            for (int i = 0; i < resources.size(); i++) {
                Resource resource = resources.get(i);
                EObject rootElement = null;
                if (resource instanceof XtextResource) {
                    IParseResult parseResult = ((XtextResource) resource).getParseResult();
                    if (parseResult != null) {
                        rootElement = parseResult.getRootASTElement();
                    }
                } else if (!resource.getContents().isEmpty()) {
                    rootElement = resource.getContents().get(0);
                }
                if (rootElement instanceof Grammar) {
                    Grammar otherGrammar = (Grammar) rootElement;
                    if (grammarName.equals(otherGrammar.getName())) {
                        if (resource instanceof DerivedStateAwareResource) {
                            resource.getContents();
                        }
                        return Collections.<EObject>singletonList(otherGrammar);
                    }
                }
            }
            // $NON-NLS-1$ //$NON-NLS-2$
            URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + ".xtext");
            URI normalizedURI = null;
            if (resourceSet instanceof XtextResourceSet) {
                XtextResourceSet set = (XtextResourceSet) resourceSet;
                normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
            } else {
                normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
            }
            final Resource resource = resourceSet.getResource(normalizedURI, true);
            if (!resource.getContents().isEmpty()) {
                final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
                if (grammarName.equals(usedGrammar.getName())) {
                    return Collections.<EObject>singletonList(usedGrammar);
                }
            }
        }
        return Collections.emptyList();
    } catch (ClasspathUriResolutionException e) {
        return Collections.emptyList();
    } catch (ValueConverterException e) {
        return Collections.emptyList();
    }
}
Also used : DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) XtextResource(org.eclipse.xtext.resource.XtextResource) DerivedStateAwareResource(org.eclipse.xtext.resource.DerivedStateAwareResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult) Grammar(org.eclipse.xtext.Grammar) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) URI(org.eclipse.emf.common.util.URI)

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