Java Code Examples for org.eclipse.xtext.AbstractMetamodelDeclaration#getEPackage()

The following examples show how to use org.eclipse.xtext.AbstractMetamodelDeclaration#getEPackage() . 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: GrammarAccessFragment.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void replaceResourceURIsWithNsURIs(Grammar grammar, ResourceSet set) {
	for (AbstractMetamodelDeclaration metamodelDecl : grammar.getMetamodelDeclarations()) {
		EPackage pack = metamodelDecl.getEPackage();
		Resource packResource = pack.eResource();
		if (!packResource.getURI().toString().equals(pack.getNsURI())) {
			ResourceSet packResourceSet = packResource.getResourceSet();
			if (packResourceSet != null) {
				EPackage topMost = pack;
				// we need to be aware of empty subpackages
				while (topMost.getESuperPackage() != null
						&& topMost.getESuperPackage().eResource() == topMost.eResource())
					topMost = topMost.getESuperPackage();
				if (packResource.getContents().contains(topMost) && packResource.getContents().size() == 1) {
					if (!topMost.getEClassifiers().isEmpty())
						packResource.setURI(URI.createURI(topMost.getNsURI()));
					else
						moveSubpackagesToNewResource(topMost, set);
				}
				if (!topMost.eResource().getURI().toString().equals(topMost.getNsURI())) 
					movePackageToNewResource(topMost, set);
			}
		}
	}
}
 
Example 2
Source File: AbstractTemplateVariableResolver.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected EClassifier getEClassifierForGrammar(String fqnClassName,
		Grammar grammar) {
	int dotIndex = fqnClassName.indexOf('.');
	String packageName = null;
	String className = fqnClassName;
	if (dotIndex > 0) {
		packageName = fqnClassName.substring(0, dotIndex);
		className = fqnClassName.substring(dotIndex + 1);
	}
	List<AbstractMetamodelDeclaration> allMetamodelDeclarations = GrammarUtil
			.allMetamodelDeclarations(grammar);
	for (AbstractMetamodelDeclaration decl : allMetamodelDeclarations) {
		EPackage pack = decl.getEPackage();
		if (packageName == null || pack.getName().equals(packageName)) {
			EClassifier eClassifier = pack.getEClassifier(className);
			if (eClassifier != null) {
				return eClassifier;
			}
		}
	}
	return null;
}
 
Example 3
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private EClassifierInfos createClassifierInfosFor(Grammar grammar, Set<Grammar> visitedGrammars) {
	if (!visitedGrammars.add(grammar))
		return null;
	final EClassifierInfos result = new EClassifierInfos(grammar);
	for(AbstractMetamodelDeclaration declaration: grammar.getMetamodelDeclarations()) {
		final EPackage referencedEPackage = declaration.getEPackage();
		if (referencedEPackage != null) {
			collectClassInfosOf(result, referencedEPackage, declaration, false);
		}
	}
	for(Grammar usedGrammar: grammar.getUsedGrammars()) {
		EClassifierInfos parent = createClassifierInfosFor(usedGrammar, visitedGrammars);
		if (parent != null)
			result.addParent(parent);
	}
	return result;
}
 
Example 4
Source File: XtextMetamodelReferenceHelper.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private static List<EObject> findReferencedMetamodelWithType(String typeName, List<AbstractMetamodelDeclaration> candidates) {
	AbstractMetamodelDeclaration result = null;
	for (AbstractMetamodelDeclaration metamodel : candidates) {
		if (metamodel instanceof ReferencedMetamodel) {
			EPackage pack = metamodel.getEPackage();
			if (pack != null) {
				final EClassifier classifier = pack.getEClassifier(typeName);
				if (classifier != null) {
					if (result == null)
						result = metamodel;
					else
						return Collections.emptyList();
				}
			}
		}
	}
	if (result != null)
		return Collections.<EObject>singletonList(result);
	return null;
}
 
Example 5
Source File: XtextLinkerTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private void checkPackageRemovalAfterGrammarChange(final boolean isRemoved, final String originalGrammar, final int offset, final int length, final String replacement) throws Exception {
  final XtextResource resource = this.getResourceFromStringAndExpect(originalGrammar, 1);
  EObject _get = resource.getContents().get(0);
  Grammar grammar = ((Grammar) _get);
  AbstractMetamodelDeclaration generatedMetamodel = grammar.getMetamodelDeclarations().get(0);
  EPackage ePackage = generatedMetamodel.getEPackage();
  Assert.assertEquals(ePackage.eResource().getResourceSet(), resource.getResourceSet());
  resource.update(offset, length, replacement);
  if (isRemoved) {
    Assert.assertNull(ePackage.eResource().getResourceSet());
  } else {
    Assert.assertEquals(ePackage.eResource().getResourceSet(), resource.getResourceSet());
  }
  EObject _get_1 = resource.getContents().get(0);
  grammar = ((Grammar) _get_1);
  generatedMetamodel = grammar.getMetamodelDeclarations().get(0);
  ePackage = generatedMetamodel.getEPackage();
  Assert.assertEquals(resource.getResourceSet(), ePackage.eResource().getResourceSet());
}
 
Example 6
Source File: XtextProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void completeTypeRef_Classifier(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	Grammar grammar = GrammarUtil.getGrammar(model);
	ContentAssistContext.Builder myContextBuilder = context.copy();
	myContextBuilder.setMatcher(new ClassifierPrefixMatcher(context.getMatcher(), classifierQualifiedNameConverter));
	if (model instanceof TypeRef) {
		ICompositeNode node = NodeModelUtils.getNode(model);
		if (node != null) {
			int offset = node.getOffset();
			Region replaceRegion = new Region(offset, context.getReplaceRegion().getLength()
					+ context.getReplaceRegion().getOffset() - offset);
			myContextBuilder.setReplaceRegion(replaceRegion);
			myContextBuilder.setLastCompleteNode(node);
			StringBuilder availablePrefix = new StringBuilder(4);
			for (ILeafNode leaf : node.getLeafNodes()) {
				if (leaf.getGrammarElement() != null && !leaf.isHidden()) {
					if ((leaf.getTotalLength() + leaf.getTotalOffset()) < context.getOffset())
						availablePrefix.append(leaf.getText());
					else
						availablePrefix.append(leaf.getText().substring(0,
								context.getOffset() - leaf.getTotalOffset()));
				}
				if (leaf.getTotalOffset() >= context.getOffset())
					break;
			}
			myContextBuilder.setPrefix(availablePrefix.toString());
		}
	}
	ContentAssistContext myContext = myContextBuilder.toContext();
	for (AbstractMetamodelDeclaration declaration : grammar.getMetamodelDeclarations()) {
		if (declaration.getEPackage() != null) {
			createClassifierProposals(declaration, model, myContext, acceptor);
		}
	}
}
 
Example 7
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public AbstractMetamodelDeclaration findMetamodel(Grammar grammar, String alias, String containedClassifier) {
	final List<AbstractMetamodelDeclaration> declarations = grammar.getMetamodelDeclarations();
	AbstractMetamodelDeclaration result = null;
	for (AbstractMetamodelDeclaration decl : declarations) {
		if (isSameAlias(decl.getAlias(), alias)) {
			EPackage pack = decl.getEPackage();
			if (pack != null && pack.getEClassifier(containedClassifier) != null) {
				if (result != null)
					return null;
				result = decl;
			}
		}
	}
	return result;
}
 
Example 8
Source File: XtextLinkingService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private List<EObject> getPackage(String nsUri, Grammar grammar, Set<Grammar> visitedGrammars) {
	if (!visitedGrammars.add(grammar))
		return null;
	for(AbstractMetamodelDeclaration declaration: grammar.getMetamodelDeclarations()) {
		EPackage pack = declaration.getEPackage();
		if (pack != null && nsUri.equals(pack.getNsURI()))
			return Collections.<EObject>singletonList(pack);
	}
	for (Grammar usedGrammar: grammar.getUsedGrammars()) {
		List<EObject> result = getPackage(nsUri, usedGrammar, visitedGrammars);
		if (result != null)
			return result;
	}
	return null;
}
 
Example 9
Source File: XtextLinkingService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isReferencedByUsedGrammar(Grammar grammar, String nsURI, Set<Grammar> visitedGrammars) {
	if (!visitedGrammars.add(grammar)) 
		return false;
	for(AbstractMetamodelDeclaration decl: grammar.getMetamodelDeclarations()) {
		EPackage pack = decl.getEPackage();
		if (pack != null && nsURI.equals(pack.getNsURI())) {
			return true;
		}
	}
	for (Grammar usedGrammar: grammar.getUsedGrammars()) {
		if (isReferencedByUsedGrammar(usedGrammar, nsURI, visitedGrammars))
			return true;
	}
	return false;
}
 
Example 10
Source File: XtextScopeProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected IScope createReferencedPackagesScope(Grammar g) {
	final Collection<EClassifier> allClassifiers = new ArrayList<EClassifier>();
	for(AbstractMetamodelDeclaration decl: g.getMetamodelDeclarations()) {
		if (decl.getEPackage() != null)
			allClassifiers.addAll(decl.getEPackage().getEClassifiers());
	}
	return createClassifierScope(allClassifiers);
}
 
Example 11
Source File: XtextLinkerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private void checkRegisteredPackageNotUnloadedAfterGrammarChange(final String originalGrammar, final int offset, final int length, final String replacement) throws Exception {
  final XtextResource resource = this.getResourceFromString(originalGrammar);
  EObject _get = resource.getContents().get(0);
  final Grammar grammar = ((Grammar) _get);
  final AbstractMetamodelDeclaration generatedMetamodel = grammar.getMetamodelDeclarations().get(0);
  final EPackage ePackage = generatedMetamodel.getEPackage();
  Assert.assertNull(((InternalEObject) ePackage).eProxyURI());
  resource.update(offset, length, replacement);
  Assert.assertNull(((InternalEObject) ePackage).eProxyURI());
}
 
Example 12
Source File: EPackageScopeProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Collects all EPackages referenced by a given grammar and its mixins.
 *
 * @param grammar
 *          grammar to collect packages for
 * @return all referenced EPackages
 */
public Map<String, EPackage> collectPackages(final Grammar grammar) {
  final Map<String, EPackage> result = new HashMap<String, EPackage>();
  for (final Grammar usedGrammar : grammar.getUsedGrammars()) {
    result.putAll(collectPackages(usedGrammar));
  }
  for (final AbstractMetamodelDeclaration decl : grammar.getMetamodelDeclarations()) {
    final EPackage ePackage = decl.getEPackage();
    if (ePackage != null) { // NOPMD
      result.put(ePackage.getNsURI(), ePackage);
    }
  }
  return result;
}
 
Example 13
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private EClassifierInfo createEClassifierInfo(TypeRef typeRef, String name) throws TransformationException {
	if (eClassifierInfos.getInfo(typeRef) != null)
		throw new IllegalArgumentException("Cannot create EClass for same type twice "
				+ typeRef.getClassifier().getName());
	//					+ GrammarUtil.getQualifiedName(typeRef));

	String classifierName = GrammarUtil.getTypeRefName(typeRef);
	if (classifierName == null)
		classifierName = name;
	if (classifierName == null)
		throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot reference unnamed type.", typeRef);

	AbstractMetamodelDeclaration metaModel = typeRef.getMetamodel();
	if (metaModel == null)
		throw new TransformationException(TransformationErrorCode.UnknownMetaModelAlias, "Cannot create type for " + classifierName
				+ " because its EPackage is unknown.", typeRef);
	EPackage generatedEPackage = getGeneratedEPackage(metaModel);
	if (generatedEPackage == null) {
		throw new TransformationException(TransformationErrorCode.CannotCreateTypeInSealedMetamodel,
				"Cannot create type '" + classifierName + "' in alias " + typeRef.getMetamodel().getAlias(), typeRef);
	}

	EClassifier classifier = generatedEPackage.getEClassifier(classifierName);
	if (classifier == null) {
		if (GrammarUtil.containingParserRule(typeRef) != null) {
			classifier = EcoreFactory.eINSTANCE.createEClass();
		} else if (GrammarUtil.containingEnumRule(typeRef) != null) {
			classifier = EcoreFactory.eINSTANCE.createEEnum();
		} else {
			for (AbstractMetamodelDeclaration mmd : grammar.getMetamodelDeclarations()) {
				if (mmd instanceof ReferencedMetamodel && mmd.getEPackage() != null && mmd.getEPackage().getNsURI().equals(EcorePackage.eNS_URI)) {
					throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot create datatype " + classifierName, typeRef);
				}
			}
			throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Cannot create datatype " + classifierName + ". If this is supposed to return EString, make sure you have imported '"+EcorePackage.eNS_URI+"'", typeRef);
		}
		classifier.setName(classifierName);
		generatedEPackage.getEClassifiers().add(classifier);
		typeRef.setClassifier(classifier);

		EClassifierInfo result;
		if (classifier instanceof EClass)
			result = EClassifierInfo.createEClassInfo((EClass) classifier, true, getGeneratedEPackageURIs(), GrammarUtil.getGrammar(typeRef));
		else // datatype or enum
			result = EClassifierInfo.createEDataTypeInfo((EDataType) classifier, true);

		if (!eClassifierInfos.addInfo(typeRef, result))
			throw new IllegalStateException("cannot add type for typeRef twice: '" + classifierName + "'");
		SourceAdapter.adapt(classifier, typeRef);
		return result;
	}
	typeRef.setClassifier(classifier);
	SourceAdapter.adapt(classifier, typeRef);
	return eClassifierInfos.getInfo(classifier);
}
 
Example 14
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private EPackage getGeneratedEPackage(AbstractMetamodelDeclaration metaModel) {
	if (metaModel instanceof GeneratedMetamodel)
		return metaModel.getEPackage();
	return null;
}
 
Example 15
Source File: GrammarAccessFragment2.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected void addAllGrammarsToResource(final Resource resource, final Grammar grammar, final Set<Grammar> visitedGrammars) {
  boolean _add = visitedGrammars.add(grammar);
  boolean _not = (!_add);
  if (_not) {
    return;
  }
  resource.getContents().add(grammar);
  EList<AbstractMetamodelDeclaration> _metamodelDeclarations = grammar.getMetamodelDeclarations();
  for (final AbstractMetamodelDeclaration metamodelDecl : _metamodelDeclarations) {
    {
      final EPackage pack = metamodelDecl.getEPackage();
      final Resource packResource = pack.eResource();
      String _string = packResource.getURI().toString();
      String _nsURI = pack.getNsURI();
      boolean _notEquals = (!Objects.equal(_string, _nsURI));
      if (_notEquals) {
        final ResourceSet packResourceSet = packResource.getResourceSet();
        if ((packResourceSet != null)) {
          EPackage topMost = pack;
          while (((topMost.getESuperPackage() != null) && (topMost.getESuperPackage().eResource() == topMost.eResource()))) {
            topMost = topMost.getESuperPackage();
          }
          if ((packResource.getContents().contains(topMost) && (packResource.getContents().size() == 1))) {
            boolean _isEmpty = topMost.getEClassifiers().isEmpty();
            boolean _not_1 = (!_isEmpty);
            if (_not_1) {
              packResource.setURI(URI.createURI(topMost.getNsURI()));
            } else {
              this.moveSubpackagesToNewResource(topMost, resource.getResourceSet());
            }
          }
          boolean _equals = topMost.eResource().getURI().toString().equals(topMost.getNsURI());
          boolean _not_2 = (!_equals);
          if (_not_2) {
            this.movePackageToNewResource(topMost, resource.getResourceSet());
          }
        }
      }
    }
  }
  EList<Grammar> _usedGrammars = grammar.getUsedGrammars();
  for (final Grammar usedGrammar : _usedGrammars) {
    this.addAllGrammarsToResource(resource, usedGrammar, visitedGrammars);
  }
}