Search in sources :

Example 1 with ResourceStorageLoadable

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

the class ResourceStorageTest method testWriteAndLoad.

@Test
public void testWriteAndLoad() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("package foo");
        _builder.newLine();
        _builder.newLine();
        _builder.append("class Bar {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("def dispatch myMethod(String s) {}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("/**");
        _builder.newLine();
        _builder.append("\t ");
        _builder.append("* Hello myMethod ");
        _builder.newLine();
        _builder.append("\t ");
        _builder.append("*/");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("def dispatch myMethod(CharSequence cs) {}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        _builder.append("class Foo {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("def dispatch other(String it) {");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("var x = \"\"");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("x = length.toString");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("println(x)");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final String contents = _builder.toString();
        final XtendFile file = this.file(contents);
        final ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ((ResourceStorageFacade) this.resourceStorageFacade).setStoreNodeModel(true);
        Resource _eResource = file.eResource();
        this.resourceStorageFacade.createResourceStorageWritable(bout).writeResource(((StorageAwareResource) _eResource));
        byte[] _byteArray = bout.toByteArray();
        ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
        final ResourceStorageLoadable in = this.resourceStorageFacade.createResourceStorageLoadable(_byteArrayInputStream);
        Resource _createResource = file.eResource().getResourceSet().createResource(URI.createURI("synthetic:/test/MyClass.xtend"));
        final StorageAwareResource resource = ((StorageAwareResource) _createResource);
        final InMemoryURIConverter converter = new InMemoryURIConverter();
        converter.addModel(resource.getURI().toString(), contents);
        ResourceSet _resourceSet = resource.getResourceSet();
        _resourceSet.setURIConverter(converter);
        EList<Resource> _resources = file.eResource().getResourceSet().getResources();
        _resources.add(resource);
        resource.loadFromStorage(in);
        EObject _get = resource.getContents().get(1);
        final JvmGenericType jvmClass = ((JvmGenericType) _get);
        Assert.assertEquals("java.lang.CharSequence", IterableExtensions.<JvmFormalParameter>head(((JvmOperation[]) Conversions.unwrapArray(jvmClass.getDeclaredOperations(), JvmOperation.class))[2].getParameters()).getParameterType().getQualifiedName());
        Assert.assertEquals("java.lang.Object", ((JvmOperation[]) Conversions.unwrapArray(jvmClass.getDeclaredOperations(), JvmOperation.class))[2].getReturnType().getQualifiedName());
        Assert.assertEquals("Hello myMethod", IterableExtensions.<DocumentationAdapter>head(Iterables.<DocumentationAdapter>filter(((JvmOperation[]) Conversions.unwrapArray(jvmClass.getDeclaredOperations(), JvmOperation.class))[1].eAdapters(), DocumentationAdapter.class)).getDocumentation());
        Assert.assertEquals(resource.getURI(), resource.getResourceDescription().getURI());
        Assert.assertEquals(2, IterableExtensions.size(resource.getResourceDescription().getExportedObjects()));
        Assert.assertEquals("foo.Bar", IterableExtensions.<IEObjectDescription>head(resource.getResourceDescription().getExportedObjects()).getQualifiedName().toString());
        final BidiTreeIterator<INode> restoredNodes = NodeModelUtils.findActualNodeFor(IterableExtensions.<EObject>head(resource.getContents())).getAsTreeIterable().iterator();
        final BidiTreeIterator<INode> originalNodes = NodeModelUtils.findActualNodeFor(file).getAsTreeIterable().iterator();
        while (restoredNodes.hasNext()) {
            {
                final INode rest = restoredNodes.next();
                final INode orig = originalNodes.next();
                Assert.assertEquals(orig.getStartLine(), rest.getStartLine());
                Assert.assertEquals(orig.getEndLine(), rest.getEndLine());
                Assert.assertEquals(orig.getOffset(), rest.getOffset());
                Assert.assertEquals(orig.getEndOffset(), rest.getEndOffset());
                Assert.assertEquals(orig.getLength(), rest.getLength());
                Assert.assertEquals(orig.getTotalStartLine(), rest.getTotalStartLine());
                Assert.assertEquals(orig.getTotalEndLine(), rest.getTotalEndLine());
                Assert.assertEquals(orig.getTotalOffset(), rest.getTotalOffset());
                Assert.assertEquals(orig.getTotalEndOffset(), rest.getTotalEndOffset());
                Assert.assertEquals(orig.getTotalLength(), rest.getTotalLength());
                Assert.assertSame(orig.getGrammarElement(), rest.getGrammarElement());
                Assert.assertEquals(file.eResource().getURIFragment(orig.getSemanticElement()), resource.getURIFragment(rest.getSemanticElement()));
                Assert.assertEquals(orig.getText(), rest.getText());
            }
        }
        Assert.assertFalse(originalNodes.hasNext());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) InMemoryURIConverter(org.eclipse.xtend.core.tests.util.InMemoryURIConverter) INode(org.eclipse.xtext.nodemodel.INode) ResourceStorageFacade(org.eclipse.xtext.resource.persistence.ResourceStorageFacade) IResourceStorageFacade(org.eclipse.xtext.resource.persistence.IResourceStorageFacade) Resource(org.eclipse.emf.ecore.resource.Resource) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) DocumentationAdapter(org.eclipse.xtext.xbase.compiler.DocumentationAdapter) ResourceStorageLoadable(org.eclipse.xtext.resource.persistence.ResourceStorageLoadable) ByteArrayInputStream(java.io.ByteArrayInputStream) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Test(org.junit.Test)

Example 2 with ResourceStorageLoadable

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

the class ResourceStorageTest method testConstantValueIsPersisted_01.

@Test
public void testConstantValueIsPersisted_01() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("class C {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("static val CONSTANT = Object");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final String contents = _builder.toString();
        final XtendFile file = this.file(contents);
        final ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ((ResourceStorageFacade) this.resourceStorageFacade).setStoreNodeModel(true);
        Resource _eResource = file.eResource();
        this.resourceStorageFacade.createResourceStorageWritable(bout).writeResource(((StorageAwareResource) _eResource));
        byte[] _byteArray = bout.toByteArray();
        ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
        final ResourceStorageLoadable in = this.resourceStorageFacade.createResourceStorageLoadable(_byteArrayInputStream);
        Resource _createResource = file.eResource().getResourceSet().createResource(URI.createURI("synthetic:/test/MyClass.xtend"));
        final StorageAwareResource resource = ((StorageAwareResource) _createResource);
        final InMemoryURIConverter converter = new InMemoryURIConverter();
        converter.addModel(resource.getURI().toString(), contents);
        ResourceSet _resourceSet = resource.getResourceSet();
        _resourceSet.setURIConverter(converter);
        EList<Resource> _resources = file.eResource().getResourceSet().getResources();
        _resources.add(resource);
        resource.loadFromStorage(in);
        EObject _get = resource.getContents().get(1);
        final JvmGenericType jvmClass = ((JvmGenericType) _get);
        JvmMember _last = IterableExtensions.<JvmMember>last(jvmClass.getMembers());
        final JvmField field = ((JvmField) _last);
        Assert.assertFalse(field.isConstant());
        Assert.assertTrue(field.isSetConstant());
        Assert.assertNull(field.getConstantValue());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) InMemoryURIConverter(org.eclipse.xtend.core.tests.util.InMemoryURIConverter) ResourceStorageFacade(org.eclipse.xtext.resource.persistence.ResourceStorageFacade) IResourceStorageFacade(org.eclipse.xtext.resource.persistence.IResourceStorageFacade) Resource(org.eclipse.emf.ecore.resource.Resource) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ResourceStorageLoadable(org.eclipse.xtext.resource.persistence.ResourceStorageLoadable) ByteArrayInputStream(java.io.ByteArrayInputStream) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmField(org.eclipse.xtext.common.types.JvmField) Test(org.junit.Test)

Example 3 with ResourceStorageLoadable

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

the class PortableURIsTest method testPortableReferenceDescriptions.

@Test
public void testPortableReferenceDescriptions() {
    try {
        final XtextResourceSet resourceSet = this.<XtextResourceSet>get(XtextResourceSet.class);
        Resource _createResource = resourceSet.createResource(URI.createURI("hubba:/bubba.langatestlanguage"));
        final StorageAwareResource resourceA = ((StorageAwareResource) _createResource);
        Resource _createResource_1 = resourceSet.createResource(URI.createURI("hubba:/bubba2.langatestlanguage"));
        final StorageAwareResource resourceB = ((StorageAwareResource) _createResource_1);
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("type B");
        _builder.newLine();
        resourceB.load(this.getAsStream(_builder.toString()), null);
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("import \'hubba:/bubba2.langatestlanguage\'");
        _builder_1.newLine();
        _builder_1.newLine();
        _builder_1.append("type A extends B");
        _builder_1.newLine();
        resourceA.load(this.getAsStream(_builder_1.toString()), null);
        final ByteArrayOutputStream bout = new ByteArrayOutputStream();
        final ResourceStorageWritable writable = resourceA.getResourceStorageFacade().createResourceStorageWritable(bout);
        writable.writeResource(resourceA);
        byte[] _byteArray = bout.toByteArray();
        ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
        final ResourceStorageLoadable loadable = resourceA.getResourceStorageFacade().createResourceStorageLoadable(_byteArrayInputStream);
        Resource _createResource_2 = resourceSet.createResource(URI.createURI("hubba:/bubba3.langatestlanguage"));
        final StorageAwareResource resourceC = ((StorageAwareResource) _createResource_2);
        resourceC.loadFromStorage(loadable);
        final IReferenceDescription refDesc = IterableExtensions.<IReferenceDescription>head(resourceC.getResourceDescription().getReferenceDescriptions());
        EObject _head = IterableExtensions.<EObject>head(resourceB.getContents());
        Assert.assertSame(IterableExtensions.<Type>head(((Main) _head).getTypes()), resourceSet.getEObject(refDesc.getTargetEObjectUri(), false));
        EObject _head_1 = IterableExtensions.<EObject>head(resourceC.getContents());
        Assert.assertSame(IterableExtensions.<Type>head(((Main) _head_1).getTypes()), resourceSet.getEObject(refDesc.getSourceEObjectUri(), false));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceStorageWritable(org.eclipse.xtext.resource.persistence.ResourceStorageWritable) ResourceStorageLoadable(org.eclipse.xtext.resource.persistence.ResourceStorageLoadable) ByteArrayInputStream(java.io.ByteArrayInputStream) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Main(org.eclipse.xtext.linking.langATestLanguage.Main) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) Test(org.junit.Test)

Example 4 with ResourceStorageLoadable

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

the class ResourceStorageTest method testConstantValueIsPersisted.

@Test
public void testConstantValueIsPersisted() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("class C {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("static val CONSTANT = \'a\' + \'b\' + 0");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final String contents = _builder.toString();
        final XtendFile file = this.file(contents);
        final ByteArrayOutputStream bout = new ByteArrayOutputStream();
        ((ResourceStorageFacade) this.resourceStorageFacade).setStoreNodeModel(true);
        Resource _eResource = file.eResource();
        this.resourceStorageFacade.createResourceStorageWritable(bout).writeResource(((StorageAwareResource) _eResource));
        byte[] _byteArray = bout.toByteArray();
        ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_byteArray);
        final ResourceStorageLoadable in = this.resourceStorageFacade.createResourceStorageLoadable(_byteArrayInputStream);
        Resource _createResource = file.eResource().getResourceSet().createResource(URI.createURI("synthetic:/test/MyClass.xtend"));
        final StorageAwareResource resource = ((StorageAwareResource) _createResource);
        final InMemoryURIConverter converter = new InMemoryURIConverter();
        converter.addModel(resource.getURI().toString(), contents);
        ResourceSet _resourceSet = resource.getResourceSet();
        _resourceSet.setURIConverter(converter);
        EList<Resource> _resources = file.eResource().getResourceSet().getResources();
        _resources.add(resource);
        resource.loadFromStorage(in);
        EObject _get = resource.getContents().get(1);
        final JvmGenericType jvmClass = ((JvmGenericType) _get);
        JvmMember _last = IterableExtensions.<JvmMember>last(jvmClass.getMembers());
        final JvmField field = ((JvmField) _last);
        Assert.assertTrue(field.isConstant());
        Assert.assertTrue(field.isSetConstant());
        Assert.assertEquals("ab0", field.getConstantValue());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) InMemoryURIConverter(org.eclipse.xtend.core.tests.util.InMemoryURIConverter) ResourceStorageFacade(org.eclipse.xtext.resource.persistence.ResourceStorageFacade) IResourceStorageFacade(org.eclipse.xtext.resource.persistence.IResourceStorageFacade) Resource(org.eclipse.emf.ecore.resource.Resource) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ResourceStorageLoadable(org.eclipse.xtext.resource.persistence.ResourceStorageLoadable) ByteArrayInputStream(java.io.ByteArrayInputStream) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmField(org.eclipse.xtext.common.types.JvmField) Test(org.junit.Test)

Example 5 with ResourceStorageLoadable

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

the class ResourceStorageFacade method getOrCreateResourceStorageLoadable.

/**
 * Finds or creates a ResourceStorageLoadable for the given resource.
 * Clients should first call shouldLoadFromStorage to check whether there exists a storage version
 * of the given resource.
 *
 * @return an IResourceStorageLoadable
 */
@Override
public ResourceStorageLoadable getOrCreateResourceStorageLoadable(final StorageAwareResource resource) {
    try {
        final ResourceStorageProviderAdapter stateProvider = IterableExtensions.<ResourceStorageProviderAdapter>head(Iterables.<ResourceStorageProviderAdapter>filter(resource.getResourceSet().eAdapters(), ResourceStorageProviderAdapter.class));
        if ((stateProvider != null)) {
            final ResourceStorageLoadable inputStream = stateProvider.getResourceStorageLoadable(resource);
            if ((inputStream != null)) {
                return inputStream;
            }
        }
        InputStream _xifexpression = null;
        boolean _exists = resource.getResourceSet().getURIConverter().exists(this.getBinaryStorageURI(resource.getURI()), CollectionLiterals.<Object, Object>emptyMap());
        if (_exists) {
            _xifexpression = resource.getResourceSet().getURIConverter().createInputStream(this.getBinaryStorageURI(resource.getURI()));
        } else {
            InputStream _xblockexpression = null;
            {
                final AbstractFileSystemAccess2 fsa = this.getFileSystemAccess(resource);
                final String outputRelativePath = this.computeOutputPath(resource);
                _xblockexpression = fsa.readBinaryFile(outputRelativePath);
            }
            _xifexpression = _xblockexpression;
        }
        final InputStream inputStream_1 = _xifexpression;
        return this.createResourceStorageLoadable(inputStream_1);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : ResourceStorageProviderAdapter(org.eclipse.xtext.resource.persistence.ResourceStorageProviderAdapter) ResourceStorageLoadable(org.eclipse.xtext.resource.persistence.ResourceStorageLoadable) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AbstractFileSystemAccess2(org.eclipse.xtext.generator.AbstractFileSystemAccess2)

Aggregations

ResourceStorageLoadable (org.eclipse.xtext.resource.persistence.ResourceStorageLoadable)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 EObject (org.eclipse.emf.ecore.EObject)4 Resource (org.eclipse.emf.ecore.resource.Resource)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)4 StorageAwareResource (org.eclipse.xtext.resource.persistence.StorageAwareResource)4 Test (org.junit.Test)4 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 InMemoryURIConverter (org.eclipse.xtend.core.tests.util.InMemoryURIConverter)3 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)3 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)3 IResourceStorageFacade (org.eclipse.xtext.resource.persistence.IResourceStorageFacade)3 ResourceStorageFacade (org.eclipse.xtext.resource.persistence.ResourceStorageFacade)3 BatchLinkableResource (org.eclipse.xtext.xbase.resource.BatchLinkableResource)3 JvmField (org.eclipse.xtext.common.types.JvmField)2 JvmMember (org.eclipse.xtext.common.types.JvmMember)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (org.eclipse.emf.common.util.URI)1