Search in sources :

Example 31 with IReferenceDescription

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

the class DefaultReferenceDescriptionTest method testSpecialReferences.

@Test
public void testSpecialReferences() {
    EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
    ePackage.setName("test");
    ePackage.setNsPrefix("test");
    ePackage.setNsURI("test");
    EClass eClass = EcoreFactory.eINSTANCE.createEClass();
    eClass.setName("Test");
    eClass.getESuperTypes().add(EcorePackage.Literals.EPACKAGE);
    ePackage.getEClassifiers().add(eClass);
    EReference eReference1 = EcoreFactory.eINSTANCE.createEReference();
    eReference1.setContainment(false);
    eReference1.setName("onlyExportedRef");
    eReference1.setEType(EcorePackage.Literals.EPACKAGE);
    eClass.getEStructuralFeatures().add(eReference1);
    EReference eReference2 = EcoreFactory.eINSTANCE.createEReference();
    eReference2.setContainment(true);
    eReference2.setName("containmentRef");
    eReference2.setEType(EcorePackage.Literals.EPACKAGE);
    eClass.getEStructuralFeatures().add(eReference2);
    EReference eReference3 = EcoreFactory.eINSTANCE.createEReference();
    eReference3.setContainment(false);
    eReference3.setTransient(true);
    eReference3.setName("transientRef");
    eReference3.setEType(EcorePackage.Literals.EPACKAGE);
    eClass.getEStructuralFeatures().add(eReference3);
    EReference eReference4 = EcoreFactory.eINSTANCE.createEReference();
    eReference4.setContainment(false);
    eReference4.setVolatile(true);
    eReference4.setName("volatileRef");
    eReference4.setEType(EcorePackage.Literals.EPACKAGE);
    eClass.getEStructuralFeatures().add(eReference4);
    EReference eReference5 = EcoreFactory.eINSTANCE.createEReference();
    eReference5.setContainment(false);
    eReference5.setDerived(true);
    eReference5.setName("derivedRef");
    eReference5.setEType(EcorePackage.Literals.EPACKAGE);
    eClass.getEStructuralFeatures().add(eReference5);
    EObject object = ePackage.getEFactoryInstance().create(eClass);
    object.eSet(EcorePackage.Literals.ENAMED_ELEMENT__NAME, "testname");
    object.eSet(eReference1, EcorePackage.eINSTANCE);
    object.eSet(eReference2, ePackage.getEFactoryInstance().create(eClass));
    object.eSet(eReference3, EcorePackage.eINSTANCE);
    object.eSet(eReference4, EcorePackage.eINSTANCE);
    object.eSet(eReference5, EcorePackage.eINSTANCE);
    Resource testResource = new XMIResourceImpl(URI.createPlatformResourceURI("test.ecore", true));
    testResource.getContents().add(object);
    IResourceDescription resourceDescription = createResourceDescription(testResource);
    assertEquals("Only one external reference expected", 1, size(resourceDescription.getReferenceDescriptions()));
    IReferenceDescription referenceDescription = resourceDescription.getReferenceDescriptions().iterator().next();
    assertEquals(-1, referenceDescription.getIndexInList());
    assertEquals(EcoreUtil.getURI(object), referenceDescription.getSourceEObjectUri());
    assertEquals(eReference1, referenceDescription.getEReference());
    assertEquals(EcoreUtil.getURI(EcorePackage.eINSTANCE), referenceDescription.getTargetEObjectUri());
    assertEquals(EcoreUtil.getURI(object), referenceDescription.getContainerEObjectURI());
}
Also used : EClass(org.eclipse.emf.ecore.EClass) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) EReference(org.eclipse.emf.ecore.EReference) XMIResourceImpl(org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl) EPackage(org.eclipse.emf.ecore.EPackage) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) Test(org.junit.Test)

Example 32 with IReferenceDescription

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

the class DefaultReferenceDescriptionTest method testCrossResourceContainment.

@Test
public void testCrossResourceContainment() {
    EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
    ePackage.setName("test");
    ePackage.setNsPrefix("test");
    ePackage.setNsURI("test");
    EClass eClass = EcoreFactory.eINSTANCE.createEClass();
    eClass.setName("Test");
    ePackage.getEClassifiers().add(eClass);
    EAttribute nameAttribute = EcoreFactory.eINSTANCE.createEAttribute();
    nameAttribute.setName("name");
    nameAttribute.setID(true);
    nameAttribute.setEType(EcorePackage.Literals.ESTRING);
    eClass.getEStructuralFeatures().add(nameAttribute);
    EReference containmentRef = EcoreFactory.eINSTANCE.createEReference();
    containmentRef.setContainment(true);
    containmentRef.setName("crossResourceContainment");
    containmentRef.setEType(eClass);
    containmentRef.setResolveProxies(true);
    eClass.getEStructuralFeatures().add(containmentRef);
    EReference containerRef = EcoreFactory.eINSTANCE.createEReference();
    containerRef.setName("containerRef");
    containerRef.setEType(eClass);
    containerRef.setResolveProxies(true);
    containerRef.setEOpposite(containmentRef);
    containmentRef.setEOpposite(containerRef);
    eClass.getEStructuralFeatures().add(containerRef);
    EObject container = ePackage.getEFactoryInstance().create(eClass);
    EObject child = ePackage.getEFactoryInstance().create(eClass);
    Resource containerResource = new XMIResourceImpl(URI.createPlatformResourceURI("container.ecore", true));
    Resource childResource = new XMIResourceImpl(URI.createPlatformResourceURI("child.ecore", true));
    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResources().add(containerResource);
    resourceSet.getResources().add(childResource);
    containerResource.getContents().add(container);
    childResource.getContents().add(child);
    container.eSet(containmentRef, child);
    assertTrue(container.eResource() != child.eResource());
    {
        IResourceDescription containerDescription = createResourceDescription(containerResource);
        IReferenceDescription onlyContainerElement = Iterables.getOnlyElement(containerDescription.getReferenceDescriptions());
        assertEquals(-1, onlyContainerElement.getIndexInList());
        assertEquals(EcoreUtil.getURI(container), onlyContainerElement.getSourceEObjectUri());
        assertEquals(containmentRef, onlyContainerElement.getEReference());
        assertEquals(EcoreUtil.getURI(child), onlyContainerElement.getTargetEObjectUri());
    }
    {
        IResourceDescription childDescription = createResourceDescription(childResource);
        IReferenceDescription onlyChildElement = Iterables.getOnlyElement(childDescription.getReferenceDescriptions());
        assertEquals(-1, onlyChildElement.getIndexInList());
        assertEquals(EcoreUtil.getURI(child), onlyChildElement.getSourceEObjectUri());
        assertEquals(containerRef, onlyChildElement.getEReference());
        assertEquals(EcoreUtil.getURI(container), onlyChildElement.getTargetEObjectUri());
    }
}
Also used : EClass(org.eclipse.emf.ecore.EClass) EAttribute(org.eclipse.emf.ecore.EAttribute) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) EReference(org.eclipse.emf.ecore.EReference) XMIResourceImpl(org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl) EPackage(org.eclipse.emf.ecore.EPackage) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) Test(org.junit.Test)

Example 33 with IReferenceDescription

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

the class DefaultReferenceDescriptionTest method testLazyLinkingProxyReferences.

/**
 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=330812
 */
@Test
public void testLazyLinkingProxyReferences() {
    URI resourceUri = URI.createPlatformResourceURI("test.ecore", true);
    LazyURIEncoder lazyURIEncoder = new LazyURIEncoder();
    ResourceSet resourceSet = new ResourceSetImpl();
    Resource testResource = resourceSet.createResource(resourceUri);
    EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
    ePackage.setName("test");
    ePackage.setNsPrefix("test");
    ePackage.setNsURI("test");
    testResource.getContents().add(ePackage);
    EClass eClass = EcoreFactory.eINSTANCE.createEClass();
    eClass.setName("Test");
    ePackage.getEClassifiers().add(eClass);
    EClass multiRefType = EcoreFactory.eINSTANCE.createEClass();
    ((InternalEObject) multiRefType).eSetProxyURI(EcoreUtil.getURI(EcorePackage.Literals.EPACKAGE));
    eClass.getESuperTypes().add(multiRefType);
    EClass multiRefType2 = EcoreFactory.eINSTANCE.createEClass();
    URI dummyProxyUri = resourceUri.appendFragment(lazyURIEncoder.encode(eClass, EcorePackage.Literals.ECLASS__ESUPER_TYPES, null));
    ((InternalEObject) multiRefType2).eSetProxyURI(dummyProxyUri);
    eClass.getESuperTypes().add(multiRefType2);
    EAttribute nameAttribute = EcoreFactory.eINSTANCE.createEAttribute();
    nameAttribute.setName("name");
    eClass.getEStructuralFeatures().add(nameAttribute);
    EDataType singleRefType = EcoreFactory.eINSTANCE.createEDataType();
    ((InternalEObject) singleRefType).eSetProxyURI(EcoreUtil.getURI(EcorePackage.Literals.ESTRING));
    nameAttribute.setEType(singleRefType);
    assertTrue(multiRefType.eIsProxy());
    assertTrue(multiRefType2.eIsProxy());
    assertTrue(lazyURIEncoder.isCrossLinkFragment(testResource, EcoreUtil.getURI(multiRefType2).fragment()));
    assertTrue(singleRefType.eIsProxy());
    IResourceDescription resourceDescription = createResourceDescription(testResource);
    Iterable<IReferenceDescription> referenceDescriptions = resourceDescription.getReferenceDescriptions();
    assertEquals("Unexpected additional resources were loaded", 1, resourceSet.getResources().size());
    assertEquals("Unexpected reference was exported", 3, Iterables.size(referenceDescriptions));
    IReferenceDescription referenceDescription = Iterables.get(referenceDescriptions, 0);
    assertEquals(0, referenceDescription.getIndexInList());
    assertEquals(EcoreUtil.getURI(eClass), referenceDescription.getSourceEObjectUri());
    assertEquals(EcorePackage.Literals.ECLASS__ESUPER_TYPES, referenceDescription.getEReference());
    assertEquals(EcoreUtil.getURI(EcorePackage.Literals.EPACKAGE), referenceDescription.getTargetEObjectUri());
    referenceDescription = Iterables.get(referenceDescriptions, 1);
    assertEquals(-1, referenceDescription.getIndexInList());
    assertEquals(EcoreUtil.getURI(nameAttribute.getEGenericType()), referenceDescription.getSourceEObjectUri());
    assertEquals(EcorePackage.Literals.EGENERIC_TYPE__ECLASSIFIER, referenceDescription.getEReference());
    assertEquals(EcoreUtil.getURI(EcorePackage.Literals.ESTRING), referenceDescription.getTargetEObjectUri());
    referenceDescription = Iterables.get(referenceDescriptions, 2);
    assertEquals(-1, referenceDescription.getIndexInList());
    assertEquals(EcoreUtil.getURI(eClass.getEGenericSuperTypes().get(0)), referenceDescription.getSourceEObjectUri());
    assertEquals(EcorePackage.Literals.EGENERIC_TYPE__ECLASSIFIER, referenceDescription.getEReference());
    assertEquals(EcoreUtil.getURI(EcorePackage.Literals.EPACKAGE), referenceDescription.getTargetEObjectUri());
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) EDataType(org.eclipse.emf.ecore.EDataType) LazyURIEncoder(org.eclipse.xtext.linking.lazy.LazyURIEncoder) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) EPackage(org.eclipse.emf.ecore.EPackage) EClass(org.eclipse.emf.ecore.EClass) EAttribute(org.eclipse.emf.ecore.EAttribute) InternalEObject(org.eclipse.emf.ecore.InternalEObject) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) Test(org.junit.Test)

Example 34 with IReferenceDescription

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

the class DefaultReferenceDescriptionTest method testgetReferenceDescriptionForMultiValue.

@Test
public void testgetReferenceDescriptionForMultiValue() throws Exception {
    with(new LangATestLanguageStandaloneSetup());
    XtextResource targetResource = getResource("type C type D", "bar.langatestlanguage");
    EObject typeC = targetResource.getContents().get(0).eContents().get(0);
    EObject typeD = targetResource.getContents().get(0).eContents().get(1);
    XtextResource resource = (XtextResource) targetResource.getResourceSet().createResource(URI.createURI("foo.langatestlanguage"));
    resource.load(new StringInputStream("type A implements B,C,D type B"), null);
    EcoreUtil2.resolveLazyCrossReferences(resource, CancelIndicator.NullImpl);
    IResourceDescription resDesc = resource.getResourceServiceProvider().getResourceDescriptionManager().getResourceDescription(resource);
    Iterable<IReferenceDescription> descriptions = resDesc.getReferenceDescriptions();
    Collection<IReferenceDescription> collection = Lists.newArrayList(descriptions);
    assertEquals(2, collection.size());
    Iterator<IReferenceDescription> iterator = descriptions.iterator();
    IReferenceDescription refDesc1 = iterator.next();
    IReferenceDescription refDesc2 = iterator.next();
    Main m = (Main) resource.getParseResult().getRootASTElement();
    assertEquals(m.getTypes().get(0), resource.getResourceSet().getEObject(refDesc1.getSourceEObjectUri(), false));
    assertEquals(typeC, resource.getResourceSet().getEObject(refDesc1.getTargetEObjectUri(), false));
    assertEquals(1, refDesc1.getIndexInList());
    assertEquals(LangATestLanguagePackage.Literals.TYPE__IMPLEMENTS, refDesc1.getEReference());
    assertEquals(m.getTypes().get(0), resource.getResourceSet().getEObject(refDesc2.getSourceEObjectUri(), false));
    assertEquals(typeD, resource.getResourceSet().getEObject(refDesc2.getTargetEObjectUri(), false));
    assertEquals(2, refDesc2.getIndexInList());
    assertEquals(LangATestLanguagePackage.Literals.TYPE__IMPLEMENTS, refDesc2.getEReference());
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) XtextResource(org.eclipse.xtext.resource.XtextResource) Main(org.eclipse.xtext.linking.langATestLanguage.Main) LangATestLanguageStandaloneSetup(org.eclipse.xtext.linking.LangATestLanguageStandaloneSetup) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) Test(org.junit.Test)

Example 35 with IReferenceDescription

use of org.eclipse.xtext.resource.IReferenceDescription in project xtext-eclipse by eclipse.

the class IntegrationTest method testReexportedSource.

@Test
public void testReexportedSource() throws Exception {
    IJavaProject foo = createJavaProject("foo");
    IJavaProject bar = createJavaProject("bar");
    IJavaProject baz = createJavaProject("baz");
    addNature(foo.getProject(), XtextProjectHelper.NATURE_ID);
    addNature(bar.getProject(), XtextProjectHelper.NATURE_ID);
    addNature(baz.getProject(), XtextProjectHelper.NATURE_ID);
    IFile file = foo.getProject().getFile("foo.jar");
    file.create(jarInputStream(new TextFile("foo/Foo" + F_EXT, "object Foo")), true, monitor());
    IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(file.getFullPath(), null, null, true);
    addToClasspath(foo, newLibraryEntry);
    addToClasspath(bar, JavaCore.newProjectEntry(foo.getPath(), true));
    addToClasspath(baz, JavaCore.newProjectEntry(bar.getPath(), false));
    IFile bazFile = createFile("baz/src/Baz" + F_EXT, "object Baz references Foo");
    build();
    assertEquals(0, countMarkers(bazFile));
    assertEquals(2, countResourcesInIndex());
    Iterator<IReferenceDescription> references = getContainedReferences(URI.createPlatformResourceURI(bazFile.getFullPath().toString(), true)).iterator();
    IReferenceDescription next = references.next();
    assertFalse(references.hasNext());
    assertEquals("platform:/resource/baz/src/Baz.buildertestlanguage#/", next.getSourceEObjectUri().toString());
    assertEquals("archive:platform:/resource/foo/foo.jar!/foo/Foo.buildertestlanguage#/", next.getTargetEObjectUri().toString());
    assertEquals(-1, next.getIndexInList());
    assertEquals(BuilderTestLanguagePackage.Literals.ELEMENT__REFERENCES, next.getEReference());
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IFile(org.eclipse.core.resources.IFile) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) TextFile(org.eclipse.xtext.ui.testing.util.JavaProjectSetupUtil.TextFile) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) Test(org.junit.Test)

Aggregations

IReferenceDescription (org.eclipse.xtext.resource.IReferenceDescription)42 URI (org.eclipse.emf.common.util.URI)18 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)17 EObject (org.eclipse.emf.ecore.EObject)12 Test (org.junit.Test)11 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)9 SubMonitor (org.eclipse.core.runtime.SubMonitor)7 EReference (org.eclipse.emf.ecore.EReference)6 Resource (org.eclipse.emf.ecore.resource.Resource)6 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)6 XtextResource (org.eclipse.xtext.resource.XtextResource)6 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)5 InternalEObject (org.eclipse.emf.ecore.InternalEObject)5 IFile (org.eclipse.core.resources.IFile)3 IProject (org.eclipse.core.resources.IProject)3 EClass (org.eclipse.emf.ecore.EClass)3 EPackage (org.eclipse.emf.ecore.EPackage)3 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 Main (org.eclipse.xtext.linking.langATestLanguage.Main)3