Search in sources :

Example 1 with StorageAwareResource

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

the class ConstantExpressionsInterpreter method findVisibleFeatures.

/**
 * looks up the static final fields which are accessible in unqualified form for the given expression.
 * That essentially includes static imports and the fields declared in the containing types
 */
protected Map<String, JvmIdentifiableElement> findVisibleFeatures(final XExpression expression) {
    HashMap<String, JvmIdentifiableElement> _xblockexpression = null;
    {
        Resource _eResource = expression.eResource();
        final Resource res = _eResource;
        boolean _matched = false;
        if (res instanceof StorageAwareResource) {
            boolean _isLoadedFromStorage = ((StorageAwareResource) res).isLoadedFromStorage();
            if (_isLoadedFromStorage) {
                _matched = true;
                return CollectionLiterals.<String, JvmIdentifiableElement>newHashMap();
            }
        }
        JvmDeclaredType _switchResult_1 = null;
        JvmIdentifiableElement _nearestLogicalContainer = this.containerProvider.getNearestLogicalContainer(expression);
        final JvmIdentifiableElement cont = _nearestLogicalContainer;
        boolean _matched_1 = false;
        if (cont instanceof JvmGenericType) {
            _matched_1 = true;
            _switchResult_1 = ((JvmGenericType) cont);
        }
        if (!_matched_1) {
            if (cont instanceof JvmMember) {
                _matched_1 = true;
                _switchResult_1 = ((JvmMember) cont).getDeclaringType();
            }
        }
        final JvmDeclaredType container = _switchResult_1;
        Pair<String, JvmDeclaredType> _mappedTo = Pair.<String, JvmDeclaredType>of("visibleFeaturesForAnnotationValues", container);
        final Provider<HashMap<String, JvmIdentifiableElement>> _function = () -> {
            final HashMap<String, JvmIdentifiableElement> result = CollectionLiterals.<String, JvmIdentifiableElement>newHashMap();
            Resource _eResource_1 = expression.eResource();
            final XImportSection section = this.importSectionLocator.getImportSection(((XtextResource) _eResource_1));
            if ((section != null)) {
                EList<XImportDeclaration> _importDeclarations = section.getImportDeclarations();
                for (final XImportDeclaration imp : _importDeclarations) {
                    boolean _isStatic = imp.isStatic();
                    if (_isStatic) {
                        final String importedTypeName = imp.getImportedTypeName();
                        if ((importedTypeName != null)) {
                            final JvmType type = this.findTypeByName(imp, importedTypeName);
                            boolean _matched_2 = false;
                            if (type instanceof JvmGenericType) {
                                _matched_2 = true;
                                this.collectAllVisibleFields(((JvmDeclaredType) type), result);
                            }
                            if (!_matched_2) {
                                if (type instanceof JvmEnumerationType) {
                                    _matched_2 = true;
                                    EList<JvmEnumerationLiteral> _literals = ((JvmEnumerationType) type).getLiterals();
                                    for (final JvmEnumerationLiteral feature : _literals) {
                                        result.put(feature.getSimpleName(), feature);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            this.collectAllVisibleFields(container, result);
            return result;
        };
        _xblockexpression = this.cache.<HashMap<String, JvmIdentifiableElement>>get(_mappedTo, expression.eResource(), _function);
    }
    return _xblockexpression;
}
Also used : JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XImportSection(org.eclipse.xtext.xtype.XImportSection) HashMap(java.util.HashMap) Resource(org.eclipse.emf.ecore.resource.Resource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) XtextResource(org.eclipse.xtext.resource.XtextResource) TypeResource(org.eclipse.xtext.common.types.access.TypeResource) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XtextResource(org.eclipse.xtext.resource.XtextResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) JvmType(org.eclipse.xtext.common.types.JvmType) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration) ProcessorInstanceForJvmTypeProvider(org.eclipse.xtend.core.macro.ProcessorInstanceForJvmTypeProvider) ILogicalContainerProvider(org.eclipse.xtext.xbase.jvmmodel.ILogicalContainerProvider) Provider(com.google.inject.Provider) IScopeProvider(org.eclipse.xtext.scoping.IScopeProvider) JvmEnumerationLiteral(org.eclipse.xtext.common.types.JvmEnumerationLiteral) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmEnumerationType(org.eclipse.xtext.common.types.JvmEnumerationType) Pair(org.eclipse.xtext.xbase.lib.Pair) XAnnotationElementValuePair(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationElementValuePair)

Example 2 with StorageAwareResource

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

the class ConstantExpressionsInterpreter method isResolveProxies.

protected boolean isResolveProxies(final EObject ctx) {
    boolean _switchResult = false;
    Resource _eResource = ctx.eResource();
    final Resource res = _eResource;
    boolean _matched = false;
    if (res instanceof StorageAwareResource) {
        _matched = true;
        _switchResult = ((StorageAwareResource) res).isLoadedFromStorage();
    }
    if (!_matched) {
        _switchResult = false;
    }
    return _switchResult;
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) XtextResource(org.eclipse.xtext.resource.XtextResource) TypeResource(org.eclipse.xtext.common.types.access.TypeResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource)

Example 3 with StorageAwareResource

use of org.eclipse.xtext.resource.persistence.StorageAwareResource 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 4 with StorageAwareResource

use of org.eclipse.xtext.resource.persistence.StorageAwareResource 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 5 with StorageAwareResource

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

the class ResourceStorageTest method testFailedWrite.

@Test(expected = IOException.class)
public void testFailedWrite() throws Exception {
    final XtendFile file = this.file("class C{}");
    ByteArrayOutputStream _byteArrayOutputStream = new ByteArrayOutputStream();
    Resource _eResource = file.eResource();
    new BatchLinkableResourceStorageWritable(_byteArrayOutputStream, false) {

        @Override
        protected void writeAssociationsAdapter(final BatchLinkableResource resource, final OutputStream zipOut) throws IOException {
            final Function1<Adapter, Boolean> _function = (Adapter it) -> {
                return Boolean.valueOf((it instanceof JvmModelAssociator.Adapter));
            };
            final Adapter removeMe = IterableExtensions.<Adapter>findFirst(resource.eAdapters(), _function);
            Assert.assertTrue(resource.eAdapters().remove(removeMe));
            super.writeAssociationsAdapter(resource, zipOut);
        }
    }.writeResource(((StorageAwareResource) _eResource));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Resource(org.eclipse.emf.ecore.resource.Resource) BatchLinkableResource(org.eclipse.xtext.xbase.resource.BatchLinkableResource) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) BatchLinkableResourceStorageWritable(org.eclipse.xtext.xbase.resource.BatchLinkableResourceStorageWritable) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) DocumentationAdapter(org.eclipse.xtext.xbase.compiler.DocumentationAdapter) Adapter(org.eclipse.emf.common.notify.Adapter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StorageAwareResource(org.eclipse.xtext.resource.persistence.StorageAwareResource) Test(org.junit.Test)

Aggregations

StorageAwareResource (org.eclipse.xtext.resource.persistence.StorageAwareResource)15 Resource (org.eclipse.emf.ecore.resource.Resource)14 Test (org.junit.Test)9 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)8 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 URI (org.eclipse.emf.common.util.URI)6 EObject (org.eclipse.emf.ecore.EObject)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)5 BatchLinkableResource (org.eclipse.xtext.xbase.resource.BatchLinkableResource)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)4 ResourceStorageLoadable (org.eclipse.xtext.resource.persistence.ResourceStorageLoadable)4 IFile (org.eclipse.core.resources.IFile)3 InMemoryURIConverter (org.eclipse.xtend.core.tests.util.InMemoryURIConverter)3 JvmMember (org.eclipse.xtext.common.types.JvmMember)3 AbsoluteURI (org.eclipse.xtext.generator.trace.AbsoluteURI)3 SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)3 XtextResource (org.eclipse.xtext.resource.XtextResource)3 IResourceStorageFacade (org.eclipse.xtext.resource.persistence.IResourceStorageFacade)3