org.eclipse.xtext.linking.impl.IllegalNodeException Java Examples

The following examples show how to use org.eclipse.xtext.linking.impl.IllegalNodeException. 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: ConcreteSyntaxAwareReferenceFinder.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected EObject toValidInstanceOrNull(Resource resource, Predicate<URI> targetURIs, EObject value) {
	EObject result = value;
	if (result.eIsProxy()) {
		URI proxyURI = EcoreUtil.getURI(result);
		if (uriEncoder.isCrossLinkFragment(resource, proxyURI.fragment())) {
			INode node = uriEncoder.decode(resource, proxyURI.fragment()).getThird();
			try {
				String string = linkingHelper.getCrossRefNodeAsString(node, true);
				if (((TargetURIs) targetURIs).getUserData(TargetURIKey.KEY).isMatchingConcreteSyntax(string)) {
					result = resolveInternalProxy(value, resource);
				} else {
					result = null;
				}
			} catch (IllegalNodeException ine) {
				// illegal nodes exist in broken ASTs
				// fired in linkingHelper.getCrossRefNodeAsString(...)
				result = null;
			}
		} else {
			result = resolveInternalProxy(value, resource);
		}
	}
	return result;
}
 
Example #2
Source File: XtextLinkingService.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private List<EObject> getLinkedMetaModel(TypeRef context, EReference ref, ILeafNode text) throws IllegalNodeException {
	final ICompositeNode parentNode = text.getParent();
	BidiIterator<INode> iterator = parentNode.getChildren().iterator();
	while(iterator.hasPrevious()) {
		INode child = iterator.previous();
		if (child instanceof ILeafNode) {
			ILeafNode leaf = (ILeafNode) child;
			if (text == leaf)
				return super.getLinkedObjects(context, ref, text);
			if (!(leaf.getGrammarElement() instanceof Keyword) && !leaf.isHidden()) {
				IScope scope = getScope(context, ref);
				return XtextMetamodelReferenceHelper.findBestMetamodelForType(
						context, text.getText(), leaf.getText(), scope);
			}
		}
	}
	return Collections.emptyList();
}
 
Example #3
Source File: LinkingDiagnosticMessageProvider.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public DiagnosticMessage getUnresolvedProxyMessage(ILinkingDiagnosticContext context) {
	String linkText = "";
	try {
		linkText = context.getLinkText();
	} catch (IllegalNodeException e) {
		linkText = e.getNode().getText();
	}

	String format = "Could not find declaration of %s '%s'";
	String type = context.getReference().getEReferenceType().getName();
	String message = String.format(format, "", linkText);
	if (!type.equals("EObject")) {
		message = String.format(format, type, linkText);
	}
	return new DiagnosticMessage(message, Severity.ERROR, Diagnostic.LINKING_DIAGNOSTIC);
}
 
Example #4
Source File: ErrorAwareLinkingService.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) throws IllegalNodeException {
	final EClass requiredType = ref.getEReferenceType();
	if (requiredType == null)
		return Collections.<EObject> emptyList();

	final String crossRefString = getCrossRefNodeAsString(context, ref, node);
	if (crossRefString != null && !crossRefString.equals("")) {
		final IScope scope = getScope(context, ref);
		QualifiedName qualifiedLinkName = qualifiedNameConverter.toQualifiedName(crossRefString);
		IEObjectDescription eObjectDescription = scope.getSingleElement(qualifiedLinkName);
		IEObjectDescriptionWithError errorDescr;
		Resource resource = context.eResource();
		if (resource != null
				&& (errorDescr = IEObjectDescriptionWithError
						.getDescriptionWithError(eObjectDescription)) != null
				// isNoValidate traverses the file system so it should be the last part of the check
				&& !n4jsCore.isNoValidate(resource.getURI())) {
			addError(context, node, errorDescr);
		} else if (eObjectDescription instanceof UnresolvableObjectDescription) {
			return Collections.<EObject> singletonList((EObject) context.eGet(ref, false));
		}

		if (eObjectDescription != null) {
			EObject candidate = eObjectDescription.getEObjectOrProxy();
			if (!candidate.eIsProxy() && candidate.eResource() == null) {
				// Error is necessary since EMF catches all exceptions in EcoreUtil#resolve
				throw new AssertionError("Found an instance without resource and without URI");
			}

			// if supported, mark object description as used
			if (eObjectDescription instanceof IUsageAwareEObjectDescription) {
				((IUsageAwareEObjectDescription) eObjectDescription).markAsUsed();
			}

			return Collections.singletonList(candidate);
		}
	}
	return Collections.emptyList();
}
 
Example #5
Source File: ScopeProviderAccess.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns a bunch of descriptions most of which are actually {@link IIdentifiableElementDescription describing identifiables}. 
 * The provided iterable is never empty but it may contain a single {@link ErrorDescription error description}.
 * 
 * @return the available descriptions.
 */
public Iterable<IEObjectDescription> getCandidateDescriptions(XExpression expression, EReference reference, /* @Nullable */ EObject toBeLinked,
		IFeatureScopeSession session, IResolvedTypes types) throws IllegalNodeException {
	if (toBeLinked == null) {
		return Collections.emptyList();
	}
	if (!toBeLinked.eIsProxy()) {
		throw new IllegalStateException(expression + " was already linked to " + toBeLinked);
	}
	URI uri = EcoreUtil.getURI(toBeLinked);
	String fragment = uri.fragment();
	if (encoder.isCrossLinkFragment(expression.eResource(), fragment)) {
		INode node = encoder.getNode(expression, fragment);
		final EClass requiredType = reference.getEReferenceType();
		if (requiredType == null)
			return Collections.emptyList();

		final String crossRefString = linkingHelper.getCrossRefNodeAsString(node, true);
		if (crossRefString != null && !crossRefString.equals("")) {
			QualifiedName qualifiedLinkName = qualifiedNameConverter.toQualifiedName(crossRefString);
			if (!qualifiedLinkName.isEmpty()) {
				final IScope scope = session.getScope(expression, reference, types);
				Iterable<IEObjectDescription> descriptions = scope.getElements(qualifiedLinkName);
				if (Iterables.isEmpty(descriptions)) {
					INode errorNode = getErrorNode(expression, node);
					if (errorNode != node) {
						qualifiedLinkName = getErrorName(errorNode);
					}
					return Collections.<IEObjectDescription>singletonList(new ErrorDescription(getErrorNode(expression, node), qualifiedLinkName));
				}
				return descriptions;
			} else {
				return Collections.<IEObjectDescription>singletonList(new ErrorDescription(null /* followUp problem */));
			}
		}
		return Collections.emptyList();
	} else {
		throw new IllegalStateException(expression + " uses unsupported uri fragment " + uri);
	}
}
 
Example #6
Source File: XtextLinkingService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) throws IllegalNodeException {
	if (ref == XtextPackage.eINSTANCE.getGrammar_UsedGrammars())
		return getUsedGrammar((Grammar) context, node);
	if (ref == XtextPackage.eINSTANCE.getTypeRef_Metamodel())
		return getLinkedMetaModel((TypeRef)context, ref, (ILeafNode) node);
	if (ref == XtextPackage.eINSTANCE.getAbstractMetamodelDeclaration_EPackage() && context instanceof GeneratedMetamodel)
		return createPackage((GeneratedMetamodel) context, (ILeafNode) node);
	if (ref == XtextPackage.eINSTANCE.getAbstractMetamodelDeclaration_EPackage() && context instanceof ReferencedMetamodel)
		return getPackage((ReferencedMetamodel)context, (ILeafNode) node);
	return super.getLinkedObjects(context, ref, node);
}
 
Example #7
Source File: LazyLinkingResource.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.3
 */
protected void createAndAddDiagnostic(Triple<EObject, EReference, INode> triple, IllegalNodeException ex) {
	if (isValidationDisabled())
		return;
	ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticMessageContext(triple);
	DiagnosticMessage message = linkingDiagnosticMessageProvider.getIllegalNodeMessage(context, ex);
	if (message != null) {
		List<Diagnostic> list = getDiagnosticList(message);
		Diagnostic diagnostic = createDiagnostic(triple, message);
		if (!list.contains(diagnostic))
			list.add(diagnostic);
	}
}
 
Example #8
Source File: CrossRefTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) throws IllegalNodeException {
	if (oneOffResult != null) {
		List<EObject> result = Lists.newArrayList(oneOffResult);
		oneOffResult = null;
		return result;
	}
	return super.getLinkedObjects(context, ref, node);
}
 
Example #9
Source File: OperationOverloadingLinkingService.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node) throws IllegalNodeException {
	if (context instanceof ArgumentExpression && isOperationCall(context)) {
		return getLinkedOperation((ArgumentExpression) context, ref, node);
	}
	return super.getLinkedObjects(context, ref, node);
}
 
Example #10
Source File: GamlLinkingService.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Override default in order to supply a stub object. If the default implementation isn't able to resolve the link,
 * assume it to be a local resource.
 */
@Override
public List<EObject> getLinkedObjects(final EObject context, final EReference ref, final INode node)
		throws IllegalNodeException {
	final List<EObject> result = super.getLinkedObjects(context, ref, node);
	// If the default implementation resolved the link, return it
	if (null != result && !result.isEmpty()) { return result; }
	final String name = getCrossRefNodeAsString(node);
	if (GamlPackage.eINSTANCE.getTypeDefinition()
			.isSuperTypeOf(ref.getEReferenceType())) { return addSymbol(name, ref.getEReferenceType()); }
	if (GamlPackage.eINSTANCE.getVarDefinition()
			.isSuperTypeOf(ref.getEReferenceType())) { return addSymbol(name, ref.getEReferenceType());
	// if (name.startsWith("pref_")) {
	// return addSymbol(name, ref.getEReferenceType());
	// } else {
	// if (context.eContainer() instanceof Parameter) {
	// final Parameter p = (Parameter) context.eContainer();
	// if (p.getLeft() == context) { return addSymbol(name, ref.getEReferenceType()); }
	// }
	// }
	// if (stubbedRefs.containsKey(name)) { return stubbedRefs.get(name); }
	}
	final GamlResource resource = (GamlResource) context.eResource();
	final IExecutionContext additionalContext = resource.getCache().getOrCreate(resource).get("linking");
	if (additionalContext != null) {
		if (additionalContext
				.hasLocalVar(name)) { return Collections.singletonList(create(name, ref.getEReferenceType())); }
	}
	return Collections.EMPTY_LIST;
}
 
Example #11
Source File: UnresolvedFeatureCallTypeAwareMessageProvider.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public DiagnosticMessage getUnresolvedProxyMessage(final ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context) {
  String _xtrycatchfinallyexpression = null;
  try {
    _xtrycatchfinallyexpression = context.getLinkText();
  } catch (final Throwable _t) {
    if (_t instanceof IllegalNodeException) {
      final IllegalNodeException e = (IllegalNodeException)_t;
      _xtrycatchfinallyexpression = e.getNode().getText();
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  }
  String linkText = _xtrycatchfinallyexpression;
  if ((linkText == null)) {
    return null;
  }
  EObject contextObject = context.getContext();
  boolean _isStaticMemberCallTarget = this.isStaticMemberCallTarget(contextObject);
  if (_isStaticMemberCallTarget) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append(linkText);
    _builder.append(" cannot be resolved to a type.");
    return new DiagnosticMessage(_builder.toString(), Severity.ERROR, Diagnostic.LINKING_DIAGNOSTIC, 
      UnresolvedFeatureCallTypeAwareMessageProvider.TYPE_LITERAL);
  }
  if ((contextObject instanceof XAbstractFeatureCall)) {
    boolean _isOperation = ((XAbstractFeatureCall)contextObject).isOperation();
    boolean _not = (!_isOperation);
    if (_not) {
      return this.handleUnresolvedFeatureCall(context, ((XAbstractFeatureCall)contextObject), linkText);
    }
  }
  EClass referenceType = context.getReference().getEReferenceType();
  StringConcatenation _builder_1 = new StringConcatenation();
  _builder_1.append(linkText);
  _builder_1.append(" cannot be resolved");
  String _typeName = this.getTypeName(referenceType, context.getReference());
  _builder_1.append(_typeName);
  _builder_1.append(".");
  final String msg = _builder_1.toString();
  return new DiagnosticMessage(msg, Severity.ERROR, Diagnostic.LINKING_DIAGNOSTIC, linkText);
}
 
Example #12
Source File: LazyLinkingResource.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.4
 */
protected EObject getEObject(String uriFragment, Triple<EObject, EReference, INode> triple) throws AssertionError {
	cyclicLinkingDetectionCounter++;
	if (cyclicLinkingDetectionCounter > cyclicLinkingDectectionCounterLimit) {
		if (!resolving.add(triple)) {
			return handleCyclicResolution(triple);
		}
	}
	try {
		Set<String> unresolveableProxies = getUnresolvableURIFragments();
		if (unresolveableProxies.contains(uriFragment))
			return null;
		EReference reference = triple.getSecond();
		try {
			List<EObject> linkedObjects = getLinkingService().getLinkedObjects(
					triple.getFirst(), 
					reference,
					triple.getThird());

			if (linkedObjects.isEmpty()) {
				if (isUnresolveableProxyCacheable(triple))
					unresolveableProxies.add(uriFragment);
				createAndAddDiagnostic(triple);
				return null;
			}
			if (linkedObjects.size() > 1)
				throw new IllegalStateException("linkingService returned more than one object for fragment "
						+ uriFragment);
			EObject result = linkedObjects.get(0);
			if (!EcoreUtil2.isAssignableFrom(reference.getEReferenceType(), result.eClass())) {
				log.error("An element of type " + result.getClass().getName()
						+ " is not assignable to the reference " + reference.getEContainingClass().getName()
						+ "." + reference.getName());
				if (isUnresolveableProxyCacheable(triple))
					unresolveableProxies.add(uriFragment);
				createAndAddDiagnostic(triple);
				return null;
			}
			// remove previously added error markers, since everything should be fine now
			unresolveableProxies.remove(uriFragment);
			removeDiagnostic(triple);
			return result;
		} catch (CyclicLinkingException e) {
			if (e.triple.equals(triple)) {
				log.error(e.getMessage(), e);
				if (isUnresolveableProxyCacheable(triple))
					unresolveableProxies.add(uriFragment);
				createAndAddDiagnostic(triple);
				return null;
			} else {
				throw e;
			}
		}
	} catch (IllegalNodeException ex) {
		createAndAddDiagnostic(triple, ex);
		return null;
	} finally {
		if (cyclicLinkingDetectionCounter > cyclicLinkingDectectionCounterLimit) {
			resolving.remove(triple);
		}
		cyclicLinkingDetectionCounter--;
	}
}
 
Example #13
Source File: GamlLinkingErrorMessageProvider.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
@Override
public DiagnosticMessage getIllegalNodeMessage(final ILinkingDiagnosticContext context,
		final IllegalNodeException ex) {
	final String message = ex.getMessage();
	return new DiagnosticMessage(message, Severity.ERROR, Diagnostic.LINKING_DIAGNOSTIC);
}
 
Example #14
Source File: ILinkingService.java    From xtext-core with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Returns all {@link EObject}s referenced by the given link text in the
 * given context. But does not set the references or modifies the passed
 * information somehow. The returned list might contain proxy instances.
 */
List<EObject> getLinkedObjects(EObject context, EReference reference, INode node) throws IllegalNodeException;
 
Example #15
Source File: ILinkingDiagnosticMessageProvider.java    From xtext-core with Eclipse Public License 2.0 votes vote down vote up
DiagnosticMessage getIllegalNodeMessage(ILinkingDiagnosticContext context, IllegalNodeException ex);