Search in sources :

Example 91 with Resource

use of org.eclipse.emf.ecore.resource.Resource in project xtext-xtend by eclipse.

the class ResourceStorageTest method testUpstreamResourcesAreLoadedFromStorage.

@Test
public void testUpstreamResourcesAreLoadedFromStorage() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("package mypack");
        _builder.newLine();
        _builder.newLine();
        _builder.append("class MyClass {");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        this.helper.createFile("mypack/MyClass.xtend", _builder.toString());
        final IProject downStreamProject = WorkbenchTestHelper.createPluginProject("downstream", this.helper.getProject().getName());
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("package downstream");
        _builder_1.newLine();
        _builder_1.newLine();
        _builder_1.append("class SomeClass {");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("def void foo(mypack.MyClass myClass) {");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("}");
        _builder_1.newLine();
        _builder_1.append("}");
        _builder_1.newLine();
        final IFile downstreamFile = this.helper.createFileImpl("/downstream/src/downstream/SomeClass.xtend", _builder_1.toString());
        IResourcesSetupUtil.waitForBuild();
        final URI downstreamUri = this.uriMapper.getUri(downstreamFile);
        final ResourceSet resourceSet = this.resourceSetProvider.get(downStreamProject);
        SourceLevelURIsAdapter.setSourceLevelUris(resourceSet, Collections.<URI>unmodifiableList(CollectionLiterals.<URI>newArrayList(downstreamUri)));
        Resource _resource = resourceSet.getResource(downstreamUri, true);
        final StorageAwareResource downstreamResource = ((StorageAwareResource) _resource);
        EObject _get = downstreamResource.getContents().get(1);
        final JvmGenericType type = ((JvmGenericType) _get);
        final JvmType parameterType = IterableExtensions.<JvmFormalParameter>head(IterableExtensions.<JvmOperation>head(Iterables.<JvmOperation>filter(type.getMembers(), JvmOperation.class)).getParameters()).getParameterType().getType();
        Assert.assertNotNull(parameterType);
        Resource _eResource = parameterType.eResource();
        Assert.assertTrue(((StorageAwareResource) _eResource).isLoadedFromStorage());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) Resource(org.eclipse.emf.ecore.resource.Resource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) JvmType(org.eclipse.xtext.common.types.JvmType) URI(org.eclipse.emf.common.util.URI) AbsoluteURI(org.eclipse.xtext.generator.trace.AbsoluteURI) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) IProject(org.eclipse.core.resources.IProject) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Test(org.junit.Test)

Example 92 with Resource

use of org.eclipse.emf.ecore.resource.Resource in project xtext-xtend by eclipse.

the class ResourceStorageTest method testLoadFromStorage.

@Test
public void testLoadFromStorage() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("package mypack");
        _builder.newLine();
        _builder.newLine();
        _builder.append("class MyClass {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("public def void foo() {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final IFile file = this.helper.createFile("src/mypack/MyClass.xtend", _builder.toString());
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("package mypack");
        _builder_1.newLine();
        _builder_1.newLine();
        _builder_1.append("class OtherClass extends MyClass {");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("override foo() {");
        _builder_1.newLine();
        _builder_1.append("\t");
        _builder_1.append("}");
        _builder_1.newLine();
        _builder_1.append("}");
        _builder_1.newLine();
        final IFile file2 = this.helper.createFile("src/mypack/OtherClass.xtend", _builder_1.toString());
        IResourcesSetupUtil.waitForBuild();
        final URI uri = this.uriMapper.getUri(file);
        final URI uri2 = this.uriMapper.getUri(file2);
        final ResourceSet resourceSet = this.resourceSetProvider.get(file.getProject());
        SourceLevelURIsAdapter.setSourceLevelUris(resourceSet, Collections.<URI>unmodifiableList(CollectionLiterals.<URI>newArrayList()));
        Resource _resource = resourceSet.getResource(uri2, true);
        final StorageAwareResource resource2 = ((StorageAwareResource) _resource);
        Assert.assertTrue(resource2.isLoadedFromStorage());
        EcoreUtil.resolveAll(resource2);
        final Function1<IEObjectDescription, String> _function = (IEObjectDescription it) -> {
            return it.getName().toString();
        };
        Assert.assertEquals("mypack.OtherClass", IterableExtensions.join(IterableExtensions.<IEObjectDescription, String>map(resource2.getResourceDescription().getExportedObjects(), _function), ","));
        Resource _resource_1 = resourceSet.getResource(uri, false);
        final StorageAwareResource resource = ((StorageAwareResource) _resource_1);
        Assert.assertTrue(resource.isLoadedFromStorage());
        final Function1<IEObjectDescription, String> _function_1 = (IEObjectDescription it) -> {
            return it.getName().toString();
        };
        Assert.assertEquals("mypack.MyClass", IterableExtensions.join(IterableExtensions.<IEObjectDescription, String>map(resource.getResourceDescription().getExportedObjects(), _function_1), ","));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Resource(org.eclipse.emf.ecore.resource.Resource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) URI(org.eclipse.emf.common.util.URI) AbsoluteURI(org.eclipse.xtext.generator.trace.AbsoluteURI) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Test(org.junit.Test)

Example 93 with Resource

use of org.eclipse.emf.ecore.resource.Resource in project xtext-core by eclipse.

the class FileAwareTestLanguageResourceRelocationStrategy method applyChange.

@Override
public void applyChange(final ResourceRelocationContext context) {
    final Function1<ResourceRelocationChange, Boolean> _function = (ResourceRelocationChange it) -> {
        return Boolean.valueOf(this.canHandle(it));
    };
    final Consumer<ResourceRelocationChange> _function_1 = (ResourceRelocationChange change) -> {
        final IChangeSerializer.IModification<Resource> _function_2 = (Resource resource) -> {
            final EObject rootElement = IterableExtensions.<EObject>head(resource.getContents());
            if ((rootElement instanceof PackageDeclaration)) {
                final String newPackage = IterableExtensions.join(IterableExtensions.<String>drop(change.getToURI().trimSegments(1).segmentsList(), 2), ".");
                ((PackageDeclaration) rootElement).setName(newPackage);
            }
        };
        context.addModification(change, _function_2);
    };
    IterableExtensions.<ResourceRelocationChange>filter(context.getChanges(), _function).forEach(_function_1);
}
Also used : ResourceRelocationChange(org.eclipse.xtext.ide.refactoring.ResourceRelocationChange) EObject(org.eclipse.emf.ecore.EObject) Resource(org.eclipse.emf.ecore.resource.Resource) PackageDeclaration(org.eclipse.xtext.testlanguages.fileAware.fileAware.PackageDeclaration)

Example 94 with Resource

use of org.eclipse.emf.ecore.resource.Resource in project xtext-core by eclipse.

the class ResourceRelocationContext method loadAndWatchResource.

/**
 * Loads and watches the respective resource and applies the relocation change.
 * Clients may usually rather call {@link #addModification} to register their
 * side-effects.
 *
 * @param change the change to execute
 */
protected Resource loadAndWatchResource(final ResourceRelocationChange change) {
    try {
        Resource _switchResult = null;
        final ResourceRelocationContext.ChangeType changeType = this.changeType;
        if (changeType != null) {
            switch(changeType) {
                case MOVE:
                case RENAME:
                    Resource _xblockexpression = null;
                    {
                        final Resource original = this.resourceSet.getResource(change.getFromURI(), true);
                        final IChangeSerializer.IModification<Resource> _function = (Resource it) -> {
                            original.setURI(change.getToURI());
                        };
                        this.changeSerializer.<Resource>addModification(original, _function);
                        _xblockexpression = original;
                    }
                    _switchResult = _xblockexpression;
                    break;
                case COPY:
                    Resource _xblockexpression_1 = null;
                    {
                        final Resource copy = this.resourceSet.createResource(change.getFromURI());
                        copy.load(this.resourceSet.getURIConverter().createInputStream(change.getFromURI()), null);
                        copy.setURI(change.getToURI());
                        _xblockexpression_1 = copy;
                    }
                    _switchResult = _xblockexpression_1;
                    break;
                default:
                    break;
            }
        }
        final Resource resource = _switchResult;
        return resource;
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource)

Example 95 with Resource

use of org.eclipse.emf.ecore.resource.Resource in project xtext-core by eclipse.

the class DocumentExtensions method newLocation.

public Location newLocation(final EObject owner, final EStructuralFeature feature, final int indexInList) {
    final Resource resource = owner.eResource();
    final ITextRegion textRegion = this.locationInFileProvider.getSignificantTextRegion(owner, feature, indexInList);
    return this.newLocation(resource, textRegion);
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource)

Aggregations

Resource (org.eclipse.emf.ecore.resource.Resource)448 Test (org.junit.Test)210 XtextResource (org.eclipse.xtext.resource.XtextResource)157 EObject (org.eclipse.emf.ecore.EObject)109 URI (org.eclipse.emf.common.util.URI)85 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)76 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)71 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)66 StringInputStream (org.eclipse.xtext.util.StringInputStream)50 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)45 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)36 IOException (java.io.IOException)26 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)24 List (java.util.List)23 EClass (org.eclipse.emf.ecore.EClass)23 EPackage (org.eclipse.emf.ecore.EPackage)23 InternalEObject (org.eclipse.emf.ecore.InternalEObject)23 DerivedStateAwareResource (org.eclipse.xtext.resource.DerivedStateAwareResource)21 ArrayList (java.util.ArrayList)19 IFile (org.eclipse.core.resources.IFile)18