diff --git a/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/scoping/AbstractPolymorphicScopeProvider.java b/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/scoping/AbstractPolymorphicScopeProvider.java index 34f15771bf..981f6cd544 100644 --- a/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/scoping/AbstractPolymorphicScopeProvider.java +++ b/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/scoping/AbstractPolymorphicScopeProvider.java @@ -114,6 +114,25 @@ protected void registerForeignObject(final EObject context, final XtextResource ImplicitReferencesAdapter.findOrCreate(originalResource).addImplicitReference(contextResource.getURI()); } + /** + * Calls {@link #registerForeignObject(EObject, XtextResource, Resource)} if the context object is in another {@link XtextResource} than the + * original resource. + *
+ * {@link EObject#eResource()} walks up the containers of the context object, so it is computed only once. + *
+ * + * @param context + * the context object, must not be {@code null} + * @param originalResource + * our resource, may be {@code null} + */ + private void registerIfForeignObject(final EObject context, final Resource originalResource) { + final Resource contextResource = context.eResource(); + if (contextResource != originalResource && contextResource instanceof XtextResource) { + registerForeignObject(context, (XtextResource) contextResource, originalResource); + } + } + // "Exported" operation (from IScopeProvider). As this is the only operation known from the interface, this is // the sole entry point that is used by all clients, except our own generated code, which *knows* that the // scope provider is an AbstractPolymorphicScopeProvider. This also means that scoping always is rooted at @@ -141,9 +160,7 @@ public IScope getScope(final EObject context, final EReference reference) { * @return the scope */ public IScope getScope(final EObject context, final EReference reference, final String scopeName, final Resource originalResource) { - if (context.eResource() != originalResource && context.eResource() instanceof XtextResource) { - registerForeignObject(context, (XtextResource) context.eResource(), originalResource); - } + registerIfForeignObject(context, originalResource); String nameToFind = scopeName == null ? SCOPE_STRING : scopeName; IScope scope = internalGetScope(context, reference, nameToFind, originalResource); if (scope == null) { @@ -230,9 +247,7 @@ public IScope getScope(final EObject context, final EClass type) { * @return the scope */ public IScope getScope(final EObject context, final EClass type, final String scopeName, final Resource originalResource) { - if (context.eResource() != originalResource && context.eResource() instanceof XtextResource) { - registerForeignObject(context, (XtextResource) context.eResource(), originalResource); - } + registerIfForeignObject(context, originalResource); final IScope result = internalGetScope(context, type, scopeName == null ? SCOPE_STRING : scopeName, originalResource); return (result == null) ? IScope.NULLSCOPE : result; } @@ -394,16 +409,18 @@ protected IScope internalGetScope(final EObject context, final ENamedElement nam Object globalCacheKey = null; if (originalResource != null) { + // EObject.eResource() walks up the containers, so it is computed once for both cache checks, one of which is always made + final boolean contained = context.eResource() != null; final ResourceSet resourceSet = originalResource.getResourceSet(); if (resourceSet != null) { final Adapter scopeCacheAdapter = EcoreUtil.getAdapter(resourceSet.eAdapters(), IResourceSetScopeCache.class); - if (scopeCacheAdapter instanceof IResourceSetScopeCache && doGlobalCache(context, namedElement, scopeName, originalResource)) { + if (scopeCacheAdapter instanceof IResourceSetScopeCache && contained && doGlobalCache(context, namedElement, scopeName, originalResource)) { resourceSetScopeCache = (IResourceSetScopeCache) scopeCacheAdapter; globalCacheKey = getGlobalCacheKey(context, namedElement, scopeName); result = resourceSetScopeCache.get(globalCacheKey); } } - if (result == null && doCache(context, namedElement, scopeName, originalResource)) { + if (result == null && contained && doCache(context, namedElement, scopeName, originalResource)) { resourceScopeCache = ResourceCache.getOrCreateResourceCache("AbstractPolymorphicScopeProvider#resourceScopeCache", originalResource); //$NON-NLS-1$ cacheKey = getCacheKey(context, namedElement, scopeName); result = resourceScopeCache.get(cacheKey); @@ -427,19 +444,17 @@ protected IScope internalGetScope(final EObject context, final ENamedElement nam * Check whether the scope should be cached for the given lookup. * * @param context - * the context, must not be {@code null} + * the context, must not be {@code null} and must be contained in a resource * @param namedElement * the named element (either {@link EClass} or {@link EReference}), must not be {@code null} * @param scopeName * the scope name, must not be {@code null} * @param originalResource * the original resource, must not be {@code null} - * @return {@code true} if {@code context} is contained in a resource and the scope should be cached + * @return {@code true} if the scope should be cached */ private boolean doCache(final EObject context, final ENamedElement namedElement, final String scopeName, final Resource originalResource) { - if (context.eResource() == null) { - return false; - } else if (namedElement instanceof EReference) { + if (namedElement instanceof EReference) { return doCache(context, (EReference) namedElement, scopeName, originalResource); } else if (namedElement instanceof EClass) { return doCache(context, (EClass) namedElement, scopeName, originalResource); @@ -451,19 +466,17 @@ private boolean doCache(final EObject context, final ENamedElement namedElement, * Check whether the scope should be globally cached for the given lookup. * * @param context - * the context, must not be {@code null} + * the context, must not be {@code null} and must be contained in a resource * @param namedElement * the namedElement (either {@link EClass} or {@link EReference}), must not be {@code null} * @param scopeName * the scope name, must not be {@code null} * @param originalResource * the original resource, must not be {@code null} - * @return {@code true} if {@code context} is contained in a resource and the scope should be globally cached + * @return {@code true} if the scope should be globally cached */ private boolean doGlobalCache(final EObject context, final ENamedElement namedElement, final String scopeName, final Resource originalResource) { - if (context.eResource() == null) { - return false; - } else if (namedElement instanceof EReference) { + if (namedElement instanceof EReference) { return doGlobalCache(context, (EReference) namedElement, scopeName, originalResource); } else if (namedElement instanceof EClass) { return doGlobalCache(context, (EClass) namedElement, scopeName, originalResource);