Java Code Examples for org.eclipse.emf.ecore.EObject#eContainingFeature()

The following examples show how to use org.eclipse.emf.ecore.EObject#eContainingFeature() . 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: TokenSequencePreservingPartialParsingHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void replaceOldSemanticElement(EObject oldElement, IParseResult previousParseResult,
		IParseResult newParseResult) {
	EObject oldSemanticParentElement = oldElement.eContainer();
	if (oldSemanticParentElement != null) {
		EStructuralFeature feature = oldElement.eContainingFeature();
		if (feature.isMany()) {
			List featureValueList = (List) oldSemanticParentElement.eGet(feature);
			int index = featureValueList.indexOf(oldElement);
			unloadSemanticObject(oldElement);
			featureValueList.set(index, newParseResult.getRootASTElement());
		} else {
			unloadSemanticObject(oldElement);
			oldSemanticParentElement.eSet(feature, newParseResult.getRootASTElement());
		}
		((ParseResult) newParseResult).setRootASTElement(previousParseResult.getRootASTElement());
	} else {
		unloadSemanticObject(oldElement);
	}
}
 
Example 2
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
private EObject getNearestClosureOrTypeDeclaration(EObject object, IResolvedTypes resolvedTypes) {
	EObject candidate = object;
	while(candidate != null) {
		if (candidate instanceof XClosure) {
			return candidate;
		}
		if (candidate instanceof XConstructorCall) {
			// skip anonymous class constructors themselves
			if (candidate.eContainingFeature() == XtendPackage.Literals.ANONYMOUS_CLASS__CONSTRUCTOR_CALL) {
				candidate = candidate.eContainer();
			}
		} else if (candidate instanceof XtendTypeDeclaration) {
			return candidate;
		}
		if (candidate instanceof RichString) {
			LightweightTypeReference type = resolvedTypes.getActualType((RichString)candidate);
			if (type != null && type.isType(StringConcatenationClient.class)) {
				return candidate;
			}
		}
		candidate = candidate.eContainer();
	}
	return null;
}
 
Example 3
Source File: TypesUtil.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
private static void prependContainingFeatureName(StringBuilder id, EObject container) {
	EStructuralFeature feature = container.eContainingFeature();
	if (feature != null) {
		String name;
		if (feature.isMany()) {
			Object elements = container.eContainer().eGet(feature);
			int index = 0;
			if (elements instanceof BasicEList) {
				BasicEList<?> elementList = (BasicEList<?>) elements;
				index = elementList.indexOf(container);
			}
			name = feature.getName() + index;
		} else {
			name = feature.getName();
		}
		id.insert(0, ID_SEPARATOR);
		id.insert(0, name);
	}
}
 
Example 4
Source File: ReferenceCCDAValidator.java    From reference-ccda-validator with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public String getPath(EObject eObject) {
	String path = "";
	while (eObject != null && !(eObject instanceof DocumentRoot)) {
		EStructuralFeature feature = eObject.eContainingFeature();
		EObject container = eObject.eContainer();
		Object value = container.eGet(feature);
		if (feature.isMany()) {
			List<?> list = (List<?>) value;
			int index = list.indexOf(eObject) + 1;
			path = "/" + feature.getName() + "[" + index + "]" + path;
		} else {
			path = "/" + feature.getName() + "[1]" + path;
		}
		eObject = eObject.eContainer();
	}
	return path;
}
 
Example 5
Source File: SARLReentrantTypeResolver.java    From sarl with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("all")
private EObject getNearestClosureOrTypeDeclaration(EObject object, IResolvedTypes resolvedTypes) {
	// Overriding for proper lambda expression.
	EObject candidate = object;
	while(candidate != null) {
		if (candidate instanceof XClosure) {
			return candidate;
		}
		if (candidate instanceof XConstructorCall) {
			// skip anonymous class constructors themselves
			if (candidate.eContainingFeature() == XtendPackage.Literals.ANONYMOUS_CLASS__CONSTRUCTOR_CALL) {
				candidate = candidate.eContainer();
			}
		} else if (candidate instanceof XtendTypeDeclaration) {
			return candidate;
		}
		if (candidate instanceof RichString) {
			LightweightTypeReference type = resolvedTypes.getActualType((RichString)candidate);
			if (type != null && type.isType(StringConcatenationClient.class)) {
				return candidate;
			}
		}
		candidate = candidate.eContainer();
	}
	return null;
}
 
Example 6
Source File: N4JSPostProcessor.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * If 'object' is a type or a constituent of a type (e.g. field of a class) in 'internalTypes', then move the type
 * to 'exposedInternalTypes'.
 */
private static void exposeType(Object object) {
	if (!(object instanceof EObject) || ((EObject) object).eIsProxy()) {
		return;
	}

	// object might not be a type but reside inside a type, e.g. field of a class
	// --> so search for the root, i.e. the ancestor directly below the TModule
	EObject rootTMP = (EObject) object;
	while (rootTMP != null && !(rootTMP.eContainer() instanceof TModule)) {
		rootTMP = rootTMP.eContainer();
	}
	final EObject root = rootTMP; // must be final for the lambda below

	if (root instanceof Type
			&& root.eContainingFeature() == TypesPackage.eINSTANCE.getTModule_InternalTypes()) {
		final TModule module = (TModule) root.eContainer();
		EcoreUtilN4.doWithDeliver(false, () -> {
			module.getExposedInternalTypes().add((Type) root);
		}, module, root); // note: root already contained in resource, so suppress notifications also in root!

		// everything referenced by the type we just moved to 'exposedInternalTypes' has to be exposed as well
		// (this is required, for example, if 'root' is a structural type, see:
		// org.eclipse.n4js.xpect.ui.tests/testdata_ui/typesystem/structuralTypeRefWithMembersAcrossFiles/Main.n4js.xt)
		exposeTypesReferencedBy(root);
	}
}
 
Example 7
Source File: UnresolvedFeatureCallTypeAwareMessageProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isStaticMemberCallTarget(final EObject contextObject) {
  boolean candidate = ((contextObject instanceof XFeatureCall) && (contextObject.eContainingFeature() == 
    XbasePackage.Literals.XMEMBER_FEATURE_CALL__MEMBER_CALL_TARGET));
  if (candidate) {
    EObject _eContainer = contextObject.eContainer();
    XMemberFeatureCall memberFeatureCall = ((XMemberFeatureCall) _eContainer);
    boolean _isExplicitStatic = memberFeatureCall.isExplicitStatic();
    if (_isExplicitStatic) {
      return true;
    }
  }
  return false;
}
 
Example 8
Source File: SerializationUtil.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public static void fillIdToEObjectMap(EObject eObject, List<EObject> map) {
	if (eObject.eContainingFeature() == null || !eObject.eContainingFeature().isTransient()) {
		map.add(eObject);
		EList<EObject> eContents = eObject.eContents();

		for (EObject child : eContents) {
			fillIdToEObjectMap(child, map);
		}
	}
}
 
Example 9
Source File: DiagnosticConverterImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return the location data for the given diagnostic.
 */
protected IssueLocation getLocationData(EObject obj, EStructuralFeature structuralFeature, int index) {
	INode parserNode = NodeModelUtils.getNode(obj);
	if (parserNode != null) {
		if (structuralFeature != null) {
			List<INode> nodes = NodeModelUtils.findNodesForFeature(obj, structuralFeature);
			if (index < 0) // insignificant index
				index = 0;
			if (nodes.size()>index)
				parserNode = nodes.get(index);
		}
		return getLocationForNode(parserNode);
	} else if (obj.eContainer() != null) {
		EObject container = obj.eContainer();
		EStructuralFeature containingFeature = obj.eContainingFeature();
		return getLocationData(container, containingFeature,
				containingFeature.isMany() ? ((EList<?>) container.eGet(containingFeature)).indexOf(obj)
						: ValidationMessageAcceptor.INSIGNIFICANT_INDEX);
	}
	
	// place issue at start of document since we lack location information
	IssueLocation startOfDocumentLocation = new IssueLocation();
	startOfDocumentLocation.offset = 0;
	startOfDocumentLocation.length = 0;
	startOfDocumentLocation.lineNumber = 1;
	startOfDocumentLocation.column = 1;
	startOfDocumentLocation.lineNumberEnd = 1;
	startOfDocumentLocation.columnEnd = 1;
	return new IssueLocation();
}
 
Example 10
Source File: TextRegionAccessToString.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected String toString(IEObjectRegion region) {
	EObject obj = region.getSemanticElement();
	StringBuilder builder = new StringBuilder(Strings.padEnd(toClassWithName(obj), textWidth, ' ') + " ");
	EObject element = region.getGrammarElement();
	if (element instanceof AbstractElement)
		builder.append(grammarToString.apply((AbstractElement) element));
	else if (element instanceof AbstractRule)
		builder.append(((AbstractRule) element).getName());
	else
		builder.append(": ERROR: No grammar element.");
	List<String> segments = Lists.newArrayList();
	EObject current = obj;
	while (current.eContainer() != null) {
		EObject container = current.eContainer();
		EStructuralFeature containingFeature = current.eContainingFeature();
		StringBuilder segment = new StringBuilder();
		segment.append(toClassWithName(container));
		segment.append("/");
		segment.append(containingFeature.getName());
		if (containingFeature.isMany()) {
			int index = ((List<?>) container.eGet(containingFeature)).indexOf(current);
			segment.append("[" + index + "]");
		}
		current = container;
		segments.add(segment.toString());
	}
	if (!segments.isEmpty()) {
		builder.append(" path:");
		builder.append(Joiner.on("=").join(segments));
	}
	return builder.toString();
}
 
Example 11
Source File: EcoreUtil2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return the next sibling of the passed eObject or null
 * @since 2.1
 */
public static EObject getNextSibling(EObject eObject) {
	EObject next = null;
	if (eObject.eContainingFeature()!=null && eObject.eContainingFeature().isMany()) {
		@SuppressWarnings("unchecked")
		List<EObject> siblings = (List<EObject>) eObject.eContainer().eGet(eObject.eContainingFeature());
		int indexOf = siblings.indexOf(eObject);
		if (indexOf < siblings.size() - 1) {
			next = siblings.get(indexOf + 1);
		}
	}
	return next;
}
 
Example 12
Source File: EcoreUtil2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return the previous sibling of the passed eObject or null
 * @since 2.1
 */
public static EObject getPreviousSibling(EObject eObject) {
	EObject previous = null;
	if (eObject.eContainingFeature()!=null && eObject.eContainingFeature().isMany()) {
		@SuppressWarnings("unchecked")
		List<EObject> siblings = (List<EObject>) eObject.eContainer().eGet(eObject.eContainingFeature());
		int indexOf = siblings.indexOf(eObject);
		if (indexOf > 0) {
			previous = siblings.get(indexOf - 1);
		}
	}
	return previous;
}
 
Example 13
Source File: XbaseHighlightingCalculator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @deprecated override {@link #highlightReferenceJvmType(IHighlightedPositionAcceptor, EObject, EReference, EObject)}
 * 		or {@link #highlightFeature(IHighlightedPositionAcceptor, EObject, org.eclipse.emf.ecore.EStructuralFeature, String...)}
 * 		in order to customize the coloring of references of {@link JvmType JvmTypes}.
 */
@Deprecated
protected void highlightReferenceJvmType(IHighlightedPositionAcceptor acceptor, EObject referencer,
		EReference reference, EObject resolvedReferencedObject, String highlightingConfiguration) {
	highlightDeprecation(acceptor, referencer, reference, resolvedReferencedObject);
	
	final Object referencersContainingFeature = referencer.eContainingFeature();
	
	if (resolvedReferencedObject instanceof JvmTypeParameter) {
		// may happen in cast expressions
		highlightFeature(acceptor, referencer, reference, TYPE_VARIABLE);
		
	} else if (referencer instanceof JvmParameterizedTypeReference
				&& (referencersContainingFeature == TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__ARGUMENTS
						|| referencersContainingFeature == TypesPackage.Literals.JVM_TYPE_CONSTRAINT__TYPE_REFERENCE
						|| referencersContainingFeature == XbasePackage.Literals.XABSTRACT_FEATURE_CALL__TYPE_ARGUMENTS
						|| referencersContainingFeature == XbasePackage.Literals.XCONSTRUCTOR_CALL__TYPE_ARGUMENTS)) {
		// case 1: 'referencer' is a type reference within the arguments reference of another (parameterized) type reference
		//  'referencer' definitely is a type argument and to be colored as such
		//  (if 'resolvedReferencedObject' is not a type parameter, which is tested above)
		// case 2: type reference is nested in a JvmWildcardTypeReference -> JvmTypeConstraint
		// case 3: the type reference is part of the type arguments of a method call
		
		if (resolvedReferencedObject instanceof JvmEnumerationType) {
			highlightFeature(acceptor, referencer, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, ENUM);
			
		} else if (resolvedReferencedObject instanceof JvmGenericType) {
			highlightFeature(acceptor, referencer, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, getStyle((JvmGenericType) resolvedReferencedObject));
			
		} else if (resolvedReferencedObject instanceof JvmAnnotationType) {
			highlightFeature(acceptor, referencer, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, highlightingConfiguration);
		}
		highlightFeature(acceptor, referencer, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, TYPE_ARGUMENT);
		
	} else if (resolvedReferencedObject instanceof JvmDeclaredType) {
		if (referencer instanceof XImportDeclaration) {
			// don't highlight import statements
			return;
			
		} else if (resolvedReferencedObject instanceof JvmEnumerationType) {
			highlightFeature(acceptor, referencer, reference, ENUM);
			
		} else if (resolvedReferencedObject instanceof JvmGenericType) {
			highlightFeature(acceptor, referencer, reference, getStyle((JvmGenericType) resolvedReferencedObject));
			
		} else if (resolvedReferencedObject instanceof JvmAnnotationType) {
			highlightFeature(acceptor, referencer, reference, highlightingConfiguration);
		}
	}
}
 
Example 14
Source File: CreateXtendTypeQuickfixes.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void addQuickfixes(Issue issue, IssueResolutionAcceptor issueResolutionAcceptor,
		IXtextDocument xtextDocument, XtextResource resource, 
		EObject referenceOwner, EReference unresolvedReference)
		throws Exception {
	String typeString = (issue.getData() != null && issue.getData().length > 0) 
			? issue.getData()[0] 
			: xtextDocument.get(issue.getOffset(), issue.getLength());
	Pair<String, String> packageAndType = typeNameGuesser.guessPackageAndTypeName(referenceOwner, typeString);
	String typeName = packageAndType.getSecond();
	if(isEmpty(typeName)) 
		return;
	String explicitPackage = packageAndType.getFirst();
	boolean isLocal = isEmpty(explicitPackage) || explicitPackage.equals(getPackage(resource));
	if(isLocal) 
		explicitPackage = "";
	if(isEmpty(packageAndType.getSecond()))
		return;
	if (unresolvedReference == XbasePackage.Literals.XCONSTRUCTOR_CALL__CONSTRUCTOR) {
		if(((XConstructorCall)referenceOwner).getConstructor().eIsProxy()) {
			if(isTypeMissing(referenceOwner, typeName, explicitPackage)) {
				newJavaClassQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
				newXtendClassQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
				if(isLocal)
					newLocalXtendClassQuickfix(typeName, resource, issue, issueResolutionAcceptor);
			}
		}
	} else if(unresolvedReference == XbasePackage.Literals.XTYPE_LITERAL__TYPE
			|| unresolvedReference == TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE) {
		EStructuralFeature eContainingFeature = referenceOwner.eContainingFeature();
		if(eContainingFeature == XtendPackage.Literals.XTEND_CLASS__EXTENDS) {
			newJavaClassQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
			newXtendClassQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
			if(isLocal)
				newLocalXtendClassQuickfix(typeName, resource, issue, issueResolutionAcceptor);
		} else if(eContainingFeature == XtendPackage.Literals.XTEND_CLASS__IMPLEMENTS
				|| eContainingFeature == XtendPackage.Literals.XTEND_INTERFACE__EXTENDS) {
			newJavaInterfaceQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
			newXtendInterfaceQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
			if(isLocal)
				newLocalXtendInterfaceQuickfix(typeName, resource, issue, issueResolutionAcceptor);
		} else {
			newJavaClassQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
			newJavaInterfaceQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
			newXtendClassQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
			newXtendInterfaceQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
			if(isLocal) {
				newLocalXtendClassQuickfix(typeName, resource, issue, issueResolutionAcceptor);				
				newLocalXtendInterfaceQuickfix(typeName, resource, issue, issueResolutionAcceptor);
			}
		}
	} else if(unresolvedReference == XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE) {
		newJavaAnnotationQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
		newXtendAnnotationQuickfix(typeName, explicitPackage, resource, issue, issueResolutionAcceptor);
		if(isLocal) 
			newLocalXtendAnnotationQuickfix(typeName, resource, issue, issueResolutionAcceptor);
	}
}