Java Code Examples for org.eclipse.emf.ecore.EPackage#setNsPrefix()

The following examples show how to use org.eclipse.emf.ecore.EPackage#setNsPrefix() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XtextLinkingService.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private List<EObject> createPackage(GeneratedMetamodel generatedMetamodel, ILeafNode text) {
	final String nsURI = getMetamodelNsURI(text);
	final URI uri = URI.createURI(nsURI);
	if (uri == null || isReferencedByUsedGrammar(generatedMetamodel, nsURI))
		return Collections.emptyList();
	EPackage pack = ((GeneratedMetamodelImpl)generatedMetamodel).basicGetEPackage();
	if (pack != null && !pack.eIsProxy())
		return singletonList((EObject)pack);
	final EPackage generatedEPackage = EcoreFactory.eINSTANCE.createEPackage();
	generatedEPackage.setName(generatedMetamodel.getName());
	generatedEPackage.setNsPrefix(generatedMetamodel.getName());
	generatedEPackage.setNsURI(nsURI);
	final Resource generatedPackageResource = new EcoreResourceFactoryImpl().createResource(uri);
	XtextResourceSet resourceSet = (XtextResourceSet) generatedMetamodel.eResource().getResourceSet();
	if (!resourceSet.getURIResourceMap().containsKey(generatedPackageResource.getURI())) {
		generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource);
	} else {
		generatedPackageResource.setURI(URI.createURI(nsURI+"_"+generatedMetamodel.hashCode()));
		generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource);
	}
	generatedPackageResource.getContents().add(generatedEPackage);
	return Collections.<EObject>singletonList(generatedEPackage);
}
 
Example 2
Source File: EcoreUtil2Test.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testGetAllReferencedObjects() {
	EClass a = createEClass("a");
	EClass b = createEClass("b");
	
	EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
	pack.setName("empty");
	pack.setNsPrefix("empty");
	pack.setNsURI("empty");
	pack.getEClassifiers().add(a);
	pack.getEClassifiers().add(b);
	
	EReference ref = EcoreFactory.eINSTANCE.createEReference();
	a.getEStructuralFeatures().add(ref);
	ref.setUpperBound(1);
	ref.setEType(b);
	EObject objA = pack.getEFactoryInstance().create(a);
	EObject objB = pack.getEFactoryInstance().create(b);
	List<EObject> res = EcoreUtil2.getAllReferencedObjects(objA, ref);
	assertNotNull(res);
	assertTrue(res.isEmpty());
	res = EcoreUtil2.getAllReferencedObjects(objA, ref);
	assertNotNull(res);
	objA.eSet(ref, objB);
}
 
Example 3
Source File: LinkedEditingRefactoringIntegrationTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testRefactorEcoreCrossLanguage() throws Exception {
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	ePackage.setNsPrefix("test");
	ePackage.setNsURI("http://test");
	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName(TEST_CLASS);
	ePackage.getEClassifiers().add(eClass);
	Resource ecoreResource = new ResourceSetImpl().createResource(URI.createPlatformResourceURI(TEST_PROJECT + "/Test.ecore", true));
	ecoreResource.getContents().add(ePackage);
	ecoreResource.save(null);
	ecoreResource.unload();
	project.refreshLocal(IResource.DEPTH_INFINITE, null);
	
	String model = "ref test." + TEST_CLASS;
	IFile file = createFile(TEST_PROJECT + "/ref.referringtestlanguage", model);
	waitForBuild();
	final XtextEditor editor = openEditor(file);
	final TextSelection selection = new TextSelection(model.indexOf(TEST_CLASS), TEST_CLASS.length());
	editor.getSelectionProvider().setSelection(selection);
	waitForDisplay();
	IRenameElementContext context = editor.getDocument().readOnly(new IUnitOfWork<IRenameElementContext, XtextResource>() {
		@Override
		public IRenameElementContext exec(XtextResource state) throws Exception {
			Reference ref = (Reference) state.getContents().get(0).eContents().get(0);
			EObject referenced = ref.getReferenced();
			assertNotNull(referenced);
			return new IRenameElementContext.Impl(EcoreUtil.getURI(referenced), referenced.eClass(), editor, selection, state.getURI());
		}
	});
	renameRefactoringController.startRefactoring(context);
	waitForDisplay();
	pressKeys(editor, "NewTestClass\n");
	waitForReconciler(editor);
	waitForDisplay();
	waitForBuild();
	ecoreResource.load(null);
	assertEquals("NewTestClass", ((EPackage)ecoreResource.getContents().get(0)).getEClassifiers().get(0).getName());
}
 
Example 4
Source File: TypeHierarchyHelperTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	errorAcceptorMock = new FailOnErrorAcceptor();
	metamodel = XtextFactory.eINSTANCE.createGeneratedMetamodel();
	Grammar grammar = XtextFactory.eINSTANCE.createGrammar();
	grammar.getMetamodelDeclarations().add(metamodel);
	infos = new EClassifierInfos(grammar);
	INT = EcoreFactory.eINSTANCE.createEDataType();
	STRING = EcoreFactory.eINSTANCE.createEDataType();
	EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
	pack.setNsURI("myURI");
	pack.setName("myName");
	pack.setNsPrefix("myPrefix");
	metamodel.setEPackage(pack);
}
 
Example 5
Source File: Schema.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public EPackage createEPackage(String name) {
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName(name);
	ePackage.setNsPrefix(name);
	ePackage.setNsURI(name);
	packages.put(ePackage.getName(), ePackage);
	return ePackage;
}
 
Example 6
Source File: XtextGrammarRefactoringIntegrationTest.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testRefactorXtextGrammarWithGeneratedClassifierAndModelWithRefToClassifier() throws Exception {
	ResourceSet rs = resourceSetProvider.get();
	EcoreFactory eInstance = EcoreFactory.eINSTANCE;
	Resource ecoreModelResource = createEcoreModel(rs, ecoreURI, initialModelRoot);
	EClass greetingClass = getGreetingClass(ecoreModelResource);
	EReference greetingRefLocal = getReferenceoGreeting(ecoreModelResource, greetingClass);
	String greetingClassFragment = EcoreUtil.getURI(greetingClass).fragment();
	String greetingRefFragment = EcoreUtil.getURI(greetingRefLocal).fragment();

	EPackage refPackage = eInstance.createEPackage();
	refPackage.setName("myDsl2");
	refPackage.setNsPrefix("myDsl2");
	refPackage.setNsURI("http://testrefactoring2");
	EClass modelRefClass = eInstance.createEClass();
	refPackage.getEClassifiers().add(modelRefClass);
	modelRefClass.setName("ModelRef");
	EReference reference = eInstance.createEReference();
	reference.setName("ref");
	reference.setLowerBound(0);
	reference.setUpperBound(-1);
	reference.setEType(greetingClass);
	modelRefClass.getEStructuralFeatures().add(reference);
	Resource refToGreetingResource = createEcoreModel(rs,
			URI.createPlatformResourceURI(TEST_PROJECT + "/src/org/xtext/example/mydsl/" + "MyDsl2.ecore", true),
			refPackage);
	refToGreetingResource.unload();
	ecoreModelResource.unload();
	waitForBuild();
	waitForDisplay();
	XtextEditor editor = openEditor(grammarFile);
	doRefactoring(editor);
	waitForBuild();
	checkConsistenceOfGrammar(editor);
	ecoreModelResource.load(null);
	String renamedGreetingClassFragment = greetingClassFragment.replaceFirst(CLASSIFIERNAME, REFACTOREDCLASSIFIERNAME);
	EObject renamedGreetingClass = ecoreModelResource.getEObject(renamedGreetingClassFragment);
	assertNotNull(renamedGreetingClass);
	assertEquals(REFACTOREDCLASSIFIERNAME, SimpleAttributeResolver.NAME_RESOLVER.apply(renamedGreetingClass));
	EReference greetingReference = (EReference) ecoreModelResource.getEObject(greetingRefFragment);
	EClassifier eType = greetingReference.getEType();
	assertFalse(eType.eIsProxy());
	assertEquals(REFACTOREDCLASSIFIERNAME, eType.getName());

	refToGreetingResource.load(null);
	EReference externalReferenceToGreeting = getReferenceoGreeting(refToGreetingResource, eType);
	assertFalse(externalReferenceToGreeting.getEType().eIsProxy());
	assertEquals(REFACTOREDCLASSIFIERNAME, externalReferenceToGreeting.getEType().getName());

}
 
Example 7
Source File: DefaultReferenceDescriptionTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@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());
}
 
Example 8
Source File: DefaultReferenceDescriptionTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@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());
	}
}
 
Example 9
Source File: DefaultReferenceDescriptionTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/** @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());
}