Skip to content
Snippets Groups Projects
Commit 9571ce8c authored by Simon Barner's avatar Simon Barner
Browse files

Override getRegisteredHandlers(Class)

- In contrast to the implementation of EObjectAwareServiceBase.getRegisteredHandlers(Class), this implementation returns all IAnnotationValueProviders that have
been registered for the given class or any of its super-classes or interfaces.

- As a result, value providers may now be bound to different base classes of a given model element, e.g. for GenericECU, annotations that have been bound to ExecutionUnit and IPlatformArchitectureElement will be return (old behavior: only annotations bound to the nearest match, i.e. ExecutionUnit, would have been returned).

- TODO: Investigate if this behavior is also (optionally) desirable for other services and should be merged / integrated to EObjectAwareServiceBase.
refs 1841
parent 2dcb3324
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,13 @@ package org.fortiss.tooling.base.ui.annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import org.eclipse.emf.ecore.EObject;
import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
......@@ -51,6 +57,10 @@ public class AnnotationValueService extends
/** The handler class attribute name. */
private static final String HANDLER_CLASS_ATTRIBUTE_NAME = "binding";
/** Cache for lookup of registered value providers */
Map<Class<?>, List<IAnnotationValueProvider<IAnnotatedSpecification>>> valueProviderCache =
new HashMap<Class<?>, List<IAnnotationValueProvider<IAnnotatedSpecification>>>();
/**
*
*/
......@@ -58,6 +68,69 @@ public class AnnotationValueService extends
super();
}
/**
* <p>
* In contrast to the implementation of
* {@link EObjectAwareServiceBase#getRegisteredHandlers(Class)}, this implementation of
* {@link #getRegisteredHandlers(Class)} returns all {@link IAnnotationValueProvider}s that have
* been registered for the given class or any of its super-classes or interfaces.
* </p>
* <p>
* TODO Investigate if this behavior is also (optionally) desirable for other services and
* should be merged to {@link EObjectAwareServiceBase}.
* </p>
*/
@Override
public List<IAnnotationValueProvider<IAnnotatedSpecification>> getRegisteredHandlers(
Class<?> modelElementClass) {
// Check if there is a cached result
List<IAnnotationValueProvider<IAnnotatedSpecification>> valueProviderList =
valueProviderCache.get(modelElementClass);
if(valueProviderList != null) {
return valueProviderList;
}
Set<IAnnotationValueProvider<IAnnotatedSpecification>> valueProviderSet =
new HashSet<IAnnotationValueProvider<IAnnotatedSpecification>>();
// Queue to walk super-type hierarchy
Queue<Class<?>> q = new LinkedList<Class<?>>();
q.add(modelElementClass);
while(!q.isEmpty()) {
Class<?> current = q.poll();
// Determine registered IAnnotationValueProviders for the current class
// (nearest match)
List<IAnnotationValueProvider<IAnnotatedSpecification>> currentHandlers =
super.getRegisteredHandlers(current);
if(currentHandlers != null) {
valueProviderSet.addAll(currentHandlers);
}
// Also, investigate super-types
Class<?> superClass = current.getSuperclass();
if(superClass != null && superClass != Object.class) {
q.add(superClass);
}
for(Class<?> iface : current.getInterfaces()) {
q.add(iface);
}
}
valueProviderList =
new ArrayList<IAnnotationValueProvider<IAnnotatedSpecification>>(valueProviderSet);
// Cache the result as the lookup that walks the entire super-type hierarchy is really
// expensive (in particular since super.getRegisteredHandlers(current); also partially walks
// the super-type hierarchy to perform a nearest match.
valueProviderCache.put(modelElementClass, valueProviderList);
return valueProviderList;
}
/** {@inheritDoc} */
@Override
public List<AnnotationEntry> getValues(final IModelElement element) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment