From 4afc26833f29c7d42374ad6bd0d9064ad92c2e2f Mon Sep 17 00:00:00 2001
From: Simon Barner <barner@fortiss.org>
Date: Fri, 1 Sep 2017 09:15:51 +0000
Subject: [PATCH] Add isDirectlyDerived(), a predicate whether a given {@code
 type} is directly derived from a given {@code superType}, or whether the
 {@code type} implements the {@code superType} interface. refs 2950

---
 .../tooling/kernel/utils/JavaUtils.java       | 26 ++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/JavaUtils.java b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/JavaUtils.java
index 8ed4945f0..650f89aca 100644
--- a/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/JavaUtils.java
+++ b/org.fortiss.tooling.kernel/trunk/src/org/fortiss/tooling/kernel/utils/JavaUtils.java
@@ -28,7 +28,7 @@ import org.apache.commons.lang.WordUtils;
  * @author doebber
  * @author $Author$
  * @version $Rev$
- * @ConQAT.Rating GREEN Hash: 0BCFE18A0C3A2F20A258EEB72B7AD343
+ * @ConQAT.Rating YELLOW Hash: 15E009EC41779E81E68F52D2958BC419
  */
 public class JavaUtils {
 
@@ -87,4 +87,28 @@ public class JavaUtils {
 
 		return buffer.toString();
 	}
+
+	/**
+	 * Predicate whether a given {@code type} is directly derived from a given {@code superType}, or
+	 * whether the {@code type} implements the {@code superType} interface.
+	 * 
+	 * @param type
+	 *            Type to for which to check if it is directly derived from the given
+	 *            {@code superType}.
+	 * @param superType
+	 *            Super-type which should be tested for the given {@code type}.
+	 * 
+	 * @return {@code true} iff a given {@code type} is directly derived from a given
+	 *         {@code superType}, or if the {@code type} implements the {@code superType} interface.
+	 */
+	public static boolean isDirectlyDerived(Class<?> type, Class<?> superType) {
+		for(Class<?> iFace : type.getInterfaces()) {
+			if(iFace.equals(superType)) {
+				return true;
+			}
+		}
+	
+		Class<?> typeSuperClass = type.getSuperclass();
+		return typeSuperClass != null && typeSuperClass.equals(superType);
+	}
 }
-- 
GitLab