use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.
the class ResourceDescriptionsUtil method findReferencesToResources.
/**
* Find all {@link IResourceDescription}s of all resources containing cross-references to any of the objects.
*
* @param descriptions
* {@link IResourceDescriptions} to find the references in.
* @param targetResources
* Target objects.
* @param matchPolicy
* match policy
* @return An {@link Iterable} of all {@link IResourceDescription}s that reference any of the objects.
*/
@SuppressWarnings("PMD.NPathComplexity")
public static Iterable<IResourceDescription> findReferencesToResources(final IResourceDescriptions descriptions, final Set<IResourceDescription> targetResources, final ReferenceMatchPolicy matchPolicy) {
if (targetResources.isEmpty()) {
return ImmutableSet.of();
}
final boolean matchNames = matchPolicy.includes(ReferenceMatchPolicy.IMPORTED_NAMES) || matchPolicy.includes(ReferenceMatchPolicy.UNRESOLVED_IMPORTED_NAMES);
final Set<URI> targetUris = Sets.newHashSetWithExpectedSize(targetResources.size());
final Set<QualifiedName> exportedNames = Sets.newHashSet();
for (IResourceDescription res : targetResources) {
targetUris.add(res.getURI());
if (matchNames) {
for (IEObjectDescription obj : res.getExportedObjects()) {
if (matchPolicy.includes(ReferenceMatchPolicy.IMPORTED_NAMES)) {
exportedNames.add(obj.getName().toLowerCase());
}
if (matchPolicy.includes(ReferenceMatchPolicy.UNRESOLVED_IMPORTED_NAMES)) {
exportedNames.add(QualifiedNames.toUnresolvedName(obj.getName().toLowerCase()));
}
}
}
}
return Iterables.filter(descriptions.getAllResourceDescriptions(), input -> {
if (matchNames) {
for (QualifiedName name : input.getImportedNames()) {
if (exportedNames.contains(name.toLowerCase())) {
// NOPMD
return true;
}
}
}
if (matchPolicy.includes(ReferenceMatchPolicy.REFERENCES)) {
for (IReferenceDescription ref : input.getReferenceDescriptions()) {
if (targetUris.contains(ref.getTargetEObjectUri().trimFragment())) {
return true;
}
}
}
return false;
});
}
use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.
the class AbstractRecursiveScope method getSingleElement.
/**
* {@inheritDoc}
*/
@Override
public synchronized IEObjectDescription getSingleElement(final QualifiedName name) {
// NOPMD NPathComplexity
if (name == null) {
// $NON-NLS-1$
throw new IllegalArgumentException("Null name in getContentByName");
}
try {
if (DEBUG && !invocationTrace.add(name)) {
throw new IllegalStateException(Messages.bind(CYCLIC_DEPENDENCY_MESSAGE, String.valueOf(name)));
}
// NOPMD UseLocaleWithCaseConversions
final QualifiedName lookupName = isIgnoreCase() ? name.toLowerCase() : name;
IEObjectDescription result = null;
result = nameCache.get(lookupName);
if (result != null) {
// NOPMD CompareObjectsWithEquals
return result == NULL_DESCRIPTION ? null : result;
}
if (contentsIterator == null) {
contentsIterator = getAllLocalElements().iterator();
}
while (result == null && contentsIterator.hasNext()) {
result = contentsIterator.next();
QualifiedName thisName = result.getName();
if (isIgnoreCase()) {
// NOPMD UseLocaleWithCaseConversions
thisName = thisName.toLowerCase();
}
if (!isIgnoreCase() || nameCache.get(thisName) == null) {
nameCache.put(thisName, result);
}
if (!lookupName.equals(thisName)) {
// NOPMD NullAssignment
result = null;
}
}
if (result == null) {
result = getParent().getSingleElement(name);
// Possibly cache this result, too. For the time being, let the outer scope use its own cache, otherwise we'll duplicate
// a lot.
}
if (result == null) {
nameCache.put(lookupName, NULL_DESCRIPTION);
// } else {
// ScopeTrace.addTrace(result, getId());
}
return result;
} catch (ConcurrentModificationException e) {
// cache invalidated: retry
reset();
// Remove name from invocation trace, otherwise the intended retry recursive call will fail
if (DEBUG) {
invocationTrace.remove(name);
}
return getSingleElement(name);
} finally {
if (DEBUG) {
invocationTrace.remove(name);
}
}
}
use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.
the class AbstractRecursiveScope method getSingleElement.
/**
* {@inheritDoc}
*/
@Override
public synchronized IEObjectDescription getSingleElement(final EObject object) {
try {
if (DEBUG && !invocationTrace.add(object)) {
throw new IllegalStateException(Messages.bind(CYCLIC_DEPENDENCY_MESSAGE, String.valueOf(object)));
}
final URI key = EcoreUtil.getURI(object);
IEObjectDescription result = objectCache.get(key);
if (result == NULL_DESCRIPTION) {
// NOPMD CompareObjectsWithEquals
return null;
} else if (result == null) {
result = super.getSingleElement(object);
if (result == null) {
objectCache.put(key, NULL_DESCRIPTION);
} else {
objectCache.put(key, result);
}
}
return result;
} finally {
if (DEBUG) {
invocationTrace.remove(object);
}
}
}
use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.
the class ContainerBasedScope method getSingleElement.
// Using QualifiedName#toLowerCase() not String#toLowerCase()
@SuppressWarnings({ "PMD.UseLocaleWithCaseConversions", "PMD.NPathComplexity" })
@Override
public synchronized IEObjectDescription getSingleElement(final QualifiedName name) {
if (nameFunctions != null && nameFunctions.contains(NameFunctions.exportNameFunction())) {
final boolean ignoreCase = isIgnoreCase();
final QualifiedName lookupName = ignoreCase ? name.toLowerCase() : name;
final IEObjectDescription cachedResult = contentByNameCache.get(lookupName);
if (cachedResult != null) {
if (cachedResult != NULL_DESCRIPTION) {
return cachedResult;
}
// Otherwise check for aliasing and if yes revert to normal behavior or delegate or parent otherwise
} else {
QualifiedName namePattern = criteria.getNamePattern();
if (namePattern != null && ignoreCase) {
namePattern = namePattern.toLowerCase();
}
if (namePattern == null || namePattern.equals(lookupName) || (namePattern instanceof QualifiedNamePattern && ((QualifiedNamePattern) namePattern).matches(lookupName))) {
final ContainerQuery copy = ((ContainerQuery.Builder) criteria).copy().name(lookupName).ignoreCase(ignoreCase);
final Iterable<IEObjectDescription> queryResult = copy.execute(container);
IEObjectDescription description = Iterables.getFirst(queryResult, null);
if (description != null) {
contentByNameCache.put(lookupName, description);
return description;
}
contentByNameCache.put(lookupName, NULL_DESCRIPTION);
}
}
// in case of aliasing revert to normal behavior
return nameFunctions.size() > 1 ? super.getSingleElement(name) : getParent().getSingleElement(name);
} else {
return super.getSingleElement(name);
}
}
use of org.eclipse.xtext.resource.IEObjectDescription in project dsl-devkit by dsldevkit.
the class ContainerBasedScope method getLocalElementsByName.
// Using QualifiedName#toLowerCase() not String#toLowerCase()
@SuppressWarnings("PMD.UseLocaleWithCaseConversions")
@Override
protected Iterable<IEObjectDescription> getLocalElementsByName(final QualifiedName name) {
if (nameFunctions != null && nameFunctions.size() == 1 && nameFunctions.contains(NameFunctions.exportNameFunction())) {
final boolean ignoreCase = isIgnoreCase();
final QualifiedName lookupName = ignoreCase ? name.toLowerCase() : name;
QualifiedName namePattern = criteria.getNamePattern();
if (namePattern != null && ignoreCase) {
namePattern = namePattern.toLowerCase();
}
if (namePattern == null || namePattern.equals(lookupName) || (namePattern instanceof QualifiedNamePattern && ((QualifiedNamePattern) namePattern).matches(lookupName))) {
final ContainerQuery copy = ((ContainerQuery.Builder) criteria).copy().name(lookupName).ignoreCase(ignoreCase);
return copy.execute(container);
}
return Collections.<IEObjectDescription>emptyList();
} else {
return super.getLocalElementsByName(name);
}
}
Aggregations