From 4355629cc632b97d085d5040d1fd8474925ad48a Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Fri, 29 Aug 2014 12:14:36 +0000
Subject: [PATCH] - Provide non-recursive, correct implementation of type-based
 annotation entry filter - Fix NPE in case a filter actually strikes

---
 .../ui/annotation/AnnotationValueService.java | 47 +++++++------------
 .../view/AnnotationLabelProvider.java         |  7 ++-
 2 files changed, 23 insertions(+), 31 deletions(-)

diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/AnnotationValueService.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/AnnotationValueService.java
index 1681a2aa4..12f7d6dbb 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/AnnotationValueService.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/AnnotationValueService.java
@@ -21,7 +21,6 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
-import org.eclipse.emf.common.util.EList;
 import org.eclipse.emf.ecore.EObject;
 import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
 import org.fortiss.tooling.base.model.element.IModelElement;
@@ -81,8 +80,8 @@ public class AnnotationValueService extends
 					for(IAnnotationValueProvider<IAnnotatedSpecification> annotationProvider : registeredHandlers) {
 
 						List<? extends IModelElement> allChildren =
-								getAllChildren(root, element.getClass(),
-										annotationProvider.excludeModelElementTypeFromAnnotatedSpecification());
+								getAllChildren(root, element.getClass(), annotationProvider
+										.excludeModelElementTypeFromAnnotatedSpecification());
 
 						for(final IModelElement e : allChildren) {
 							IAnnotatedSpecification annotatedSpecification =
@@ -128,40 +127,28 @@ public class AnnotationValueService extends
 
 	/**
 	 * Returns all children from element of the given type and filters out children of the given
-	 * filterOut list
+	 * {@code modelElementTypesExcludedFromAnnotatedSpecification} list.
 	 */
 	private <S, T> List<? extends IModelElement> getAllChildren(EObject element,
-			Class<? extends IModelElement> type, List<Class<? extends EObject>> modelElementTypesExcludedFromAnnotatedSpecification) {
+			Class<? extends IModelElement> type,
+			List<Class<? extends EObject>> modelElementTypesExcludedFromAnnotatedSpecification) {
 
-		List<? extends IModelElement> results = new ArrayList<IModelElement>();
-		if(modelElementTypesExcludedFromAnnotatedSpecification != null)
-			getAllChildren2(element, type, modelElementTypesExcludedFromAnnotatedSpecification, results);
-		else
-			results = EcoreUtils.getChildrenWithType(element, type);
-		return results;
-
-	}
-
-	/** depth search for desired elements */
-	@SuppressWarnings("unchecked")
-	private <T, S> void getAllChildren2(EObject element, Class<? extends IModelElement> type,
-			List<Class<? extends EObject>> modelElementTypesExcludedFromAnnotatedSpecification, final List<T> results) {
-
-		EList<EObject> eContents = element.eContents();
-		for(EObject o : eContents) {
-			if(type.isInstance(o)) {
-				results.add((T)o);
-				getAllChildren2(o, type, modelElementTypesExcludedFromAnnotatedSpecification, results);
-			} else {
-				boolean filter = false;
+		List<? extends IModelElement> results = EcoreUtils.getChildrenWithType(element, type);
+		if(modelElementTypesExcludedFromAnnotatedSpecification != null) {
+			List<IModelElement> toRemove = new ArrayList<IModelElement>();
+			for(IModelElement modelElement : results) {
 				for(Class<?> c : modelElementTypesExcludedFromAnnotatedSpecification) {
-					if(c.isInstance(o))
-						filter = true;
+					if(c.isInstance(modelElement)) {
+						toRemove.add(modelElement);
+						break;
+					}
 				}
-				if(!filter)
-					getAllChildren2(o, type, modelElementTypesExcludedFromAnnotatedSpecification, results);
 			}
+
+			results.removeAll(toRemove);
 		}
+
+		return results;
 	}
 
 	/** {@inheritDoc} */
diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/AnnotationLabelProvider.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/AnnotationLabelProvider.java
index 54278df14..a19d91091 100644
--- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/AnnotationLabelProvider.java
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/view/AnnotationLabelProvider.java
@@ -63,7 +63,12 @@ public class AnnotationLabelProvider extends ColumnLabelProvider {
 		if(element instanceof AnnotationEntry) {
 			AnnotationEntry annotationEntry = (AnnotationEntry)element;
 
-			Object value = annotationEntry.getSpecificationValue(specClass, instanceKey);
+			Object value = null;
+			try {
+				value = annotationEntry.getSpecificationValue(specClass, instanceKey);
+			} catch(Exception e) {
+				// Ignore. Return "" in this case.
+			}
 			if(value != null) {
 				return value.toString();
 			}
-- 
GitLab