Search in sources :

Example 6 with JvmField

use of org.eclipse.xtext.common.types.JvmField 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 7 with JvmField

use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.

the class JvmModelTests method testAnonymousClass_01.

@Test
public void testAnonymousClass_01() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def foo() {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("new Runnable() {");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("int field");
        _builder.newLine();
        _builder.append("\t\t");
        _builder.append("override run() {}");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(this.function(_builder.toString()));
        this.typeResolver.resolveTypes(operation.eResource());
        Assert.assertEquals(1, operation.getLocalClasses().size());
        final JvmGenericType anonymous = IterableExtensions.<JvmGenericType>head(operation.getLocalClasses());
        Assert.assertTrue(anonymous.isFinal());
        Assert.assertFalse(anonymous.isStatic());
        Assert.assertTrue(anonymous.isLocal());
        Assert.assertFalse(anonymous.isAnonymous());
        Assert.assertEquals(JvmVisibility.DEFAULT, anonymous.getVisibility());
        Assert.assertEquals(2, anonymous.getSuperTypes().size());
        Assert.assertEquals("java.lang.Runnable", IterableExtensions.<JvmTypeReference>last(anonymous.getSuperTypes()).getQualifiedName());
        Assert.assertEquals(3, anonymous.getMembers().size());
        final JvmMember constructor = IterableExtensions.<JvmMember>last(anonymous.getMembers());
        Assert.assertTrue((constructor instanceof JvmConstructor));
        Assert.assertEquals(0, ((JvmConstructor) constructor).getParameters().size());
        JvmMember _head = IterableExtensions.<JvmMember>head(anonymous.getMembers());
        Assert.assertTrue((_head instanceof JvmField));
        final JvmMember overriding = anonymous.getMembers().get(1);
        Assert.assertTrue((overriding instanceof JvmOperation));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmField(org.eclipse.xtext.common.types.JvmField) Test(org.junit.Test)

Example 8 with JvmField

use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.

the class InferredJvmModelTest method testInferredFunction_05.

@Test
public void testInferredFunction_05() throws Exception {
    XtendFile xtendFile = file("class Foo { def Iterable<? extends CharSequence> create result: newArrayList(s) newList(String s) {} }");
    JvmGenericType inferredType = getInferredType(xtendFile);
    XtendClass xtendClass = (XtendClass) xtendFile.getXtendTypes().get(0);
    EList<JvmMember> jvmMembers = inferredType.getMembers();
    assertEquals(4, jvmMembers.size());
    JvmMember jvmMember = jvmMembers.get(1);
    assertTrue(jvmMember instanceof JvmOperation);
    XtendFunction xtendFunction = (XtendFunction) xtendClass.getMembers().get(0);
    assertEquals(xtendFunction.getName(), jvmMember.getSimpleName());
    assertEquals(JvmVisibility.PUBLIC, jvmMember.getVisibility());
    assertEquals("java.lang.Iterable<? extends java.lang.CharSequence>", ((JvmOperation) jvmMember).getReturnType().getIdentifier());
    JvmField cacheVar = (JvmField) jvmMembers.get(2);
    assertEquals("_createCache_" + xtendFunction.getName(), cacheVar.getSimpleName());
    assertEquals(JvmVisibility.PRIVATE, cacheVar.getVisibility());
    assertEquals("java.util.HashMap<java.util.ArrayList<? extends java.lang.Object>, java.lang.Iterable<? extends java.lang.CharSequence>>", cacheVar.getType().getIdentifier());
    JvmOperation privateInitializer = (JvmOperation) jvmMembers.get(3);
    assertEquals("_init_" + xtendFunction.getName(), privateInitializer.getSimpleName());
    assertEquals(JvmVisibility.PRIVATE, privateInitializer.getVisibility());
    assertEquals("java.util.ArrayList<java.lang.String>", privateInitializer.getParameters().get(0).getParameterType().getIdentifier());
    assertEquals("java.lang.String", privateInitializer.getParameters().get(1).getParameterType().getIdentifier());
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmField(org.eclipse.xtext.common.types.JvmField) Test(org.junit.Test)

Example 9 with JvmField

use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.

the class InferredJvmModelTest method testNameClashWithCreateExtension_00.

@Test
public void testNameClashWithCreateExtension_00() throws Exception {
    XtendFile xtendFile = file("package foo class Foo { def create new String() s(String x) { '' } String _createCache_s }");
    JvmGenericType inferredType = getInferredType(xtendFile);
    JvmField cacheVar = (JvmField) inferredType.getMembers().get(2);
    assertEquals("_createCache_s_1", cacheVar.getSimpleName());
    JvmOperation initializer = (JvmOperation) inferredType.getMembers().get(3);
    assertEquals("_init_s", initializer.getSimpleName());
    JvmField field = (JvmField) inferredType.getMembers().get(4);
    assertEquals("_createCache_s", field.getSimpleName());
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmField(org.eclipse.xtext.common.types.JvmField) Test(org.junit.Test)

Example 10 with JvmField

use of org.eclipse.xtext.common.types.JvmField in project xtext-xtend by eclipse.

the class InferredJvmModelTest method testInferredFunction_04.

@Test
public void testInferredFunction_04() throws Exception {
    XtendFile xtendFile = file("class Foo { def Iterable<CharSequence> create result: newArrayList(s) newList(String s) {} }");
    JvmGenericType inferredType = getInferredType(xtendFile);
    XtendClass xtendClass = (XtendClass) xtendFile.getXtendTypes().get(0);
    EList<JvmMember> jvmMembers = inferredType.getMembers();
    assertEquals(4, jvmMembers.size());
    JvmMember jvmMember = jvmMembers.get(1);
    assertTrue(jvmMember instanceof JvmOperation);
    XtendFunction xtendFunction = (XtendFunction) xtendClass.getMembers().get(0);
    assertEquals(xtendFunction.getName(), jvmMember.getSimpleName());
    assertEquals(JvmVisibility.PUBLIC, jvmMember.getVisibility());
    assertEquals("java.lang.Iterable<java.lang.CharSequence>", ((JvmOperation) jvmMember).getReturnType().getIdentifier());
    JvmField cacheVar = (JvmField) jvmMembers.get(2);
    assertEquals("_createCache_" + xtendFunction.getName(), cacheVar.getSimpleName());
    assertEquals(JvmVisibility.PRIVATE, cacheVar.getVisibility());
    assertEquals("java.util.HashMap<java.util.ArrayList<? extends java.lang.Object>, java.lang.Iterable<java.lang.CharSequence>>", cacheVar.getType().getIdentifier());
    JvmOperation privateInitializer = (JvmOperation) jvmMembers.get(3);
    assertEquals("_init_" + xtendFunction.getName(), privateInitializer.getSimpleName());
    assertEquals(JvmVisibility.PRIVATE, privateInitializer.getVisibility());
    // This used to be a bogus assertion since Iterable<CharSequence> is not assignable from ArrayList<String>
    // assertEquals("java.util.ArrayList<java.lang.String>", privateInitializer.getParameters().get(0).getParameterType().getIdentifier());
    assertEquals("java.util.ArrayList<java.lang.CharSequence>", privateInitializer.getParameters().get(0).getParameterType().getIdentifier());
    assertEquals("java.lang.String", privateInitializer.getParameters().get(1).getParameterType().getIdentifier());
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmField(org.eclipse.xtext.common.types.JvmField) Test(org.junit.Test)

Aggregations

JvmField (org.eclipse.xtext.common.types.JvmField)36 Test (org.junit.Test)24 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)19 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)18 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)17 JvmMember (org.eclipse.xtext.common.types.JvmMember)12 EObject (org.eclipse.emf.ecore.EObject)9 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)9 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)8 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)7 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)5 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)4 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)3 JvmEnumerationLiteral (org.eclipse.xtext.common.types.JvmEnumerationLiteral)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Set (java.util.Set)2 InternalEObject (org.eclipse.emf.ecore.InternalEObject)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2