]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/ReflectionUtils.java
Search deeply for generic superclass in ReflectionUtils
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / ReflectionUtils.java
index 5368b2e345a1fd579d3e2cadc3933b81b4a39bee..7f0f6c589a4c8f290db1c48eccf915535e477340 100644 (file)
@@ -1,82 +1,90 @@
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.utils;\r
-\r
-import java.lang.reflect.ParameterizedType;\r
-import java.lang.reflect.Type;\r
-\r
-public class ReflectionUtils {\r
-\r
-    /**\r
-     * For a class <code>C extends PC&lt;P&gt;</code>, where <code>PC</code> is\r
-     * a class, not an interface, retrieves class <code>P</code>.\r
-     * \r
-     * @param clazz\r
-     * @return\r
-     */\r
-    public static Class<?> getSingleParameterType(Class<?> clazz) {\r
-\r
-        Type t = clazz.getGenericSuperclass();\r
-        if(t instanceof Class<?>) {\r
-            throw new UnsupportedOperationException("Missing parameter type for input class '" + clazz.getCanonicalName() + "'");\r
-        } else if (t instanceof ParameterizedType) {\r
-            ParameterizedType type = (ParameterizedType)t;\r
-            Type argType = type.getActualTypeArguments()[0];\r
-            if (argType instanceof Class<?>) {\r
-                return (Class<?>) argType;\r
-            } else if (argType instanceof ParameterizedType) {\r
-                // This happens if the input class X<T>\r
-                // parameter T is also parameterized.\r
-                ParameterizedType pargType = (ParameterizedType) argType;\r
-                return (Class<?>) pargType.getRawType();\r
-            } else {\r
-                throw new UnsupportedOperationException("Unsupported parameter type in class '" + clazz.getCanonicalName() + "': " + argType);\r
-            }\r
-        } else {\r
-            throw new UnsupportedOperationException("Unknown case in class '" + clazz.getCanonicalName() + "'");\r
-        }\r
-\r
-    }\r
-\r
-    /**\r
-     * Works like {@link #getSingleParameterType(Class)} assuming the retrieved\r
-     * parameter class extends <code>T</code>.\r
-     * \r
-     * @param clazz\r
-     * @return\r
-     */\r
-    @SuppressWarnings("unchecked")\r
-    public static <T> Class<T> getSingleParameterTypeExtending(Class<?> clazz) {\r
-\r
-        Type t = clazz.getGenericSuperclass();\r
-        if(t instanceof Class<?>) {\r
-            throw new UnsupportedOperationException("Missing parameter type for input class '" + clazz.getCanonicalName() + "'");\r
-        } else if (t instanceof ParameterizedType) {\r
-            ParameterizedType type = (ParameterizedType)t;\r
-            Type argType = type.getActualTypeArguments()[0];\r
-            if (argType instanceof Class<?>) {\r
-                return (Class<T>) argType;\r
-            } else if (argType instanceof ParameterizedType) {\r
-                // This happens if the input class X<T>\r
-                // parameter T is also parameterized.\r
-                ParameterizedType pargType = (ParameterizedType) argType;\r
-                return (Class<T>) pargType.getRawType();\r
-            } else {\r
-                throw new UnsupportedOperationException("Unsupported parameter type in class '" + clazz.getCanonicalName() + "': " + argType);\r
-            }\r
-        } else {\r
-            throw new UnsupportedOperationException("Unknown case in class '" + clazz.getCanonicalName() + "'");\r
-        }\r
-\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.utils;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+
+public class ReflectionUtils {
+
+    /**
+     * For a class <code>C extends PC&lt;P&gt;</code>, where <code>PC</code> is
+     * a class, not an interface, retrieves class <code>P</code>.
+     * 
+     * @param clazz
+     * @return
+     */
+    public static Class<?> getSingleParameterType(Class<?> clazz) {
+
+        Type t = clazz.getGenericSuperclass();
+        if(t instanceof Class<?>) {
+            throw new UnsupportedOperationException("Missing parameter type for input class '" + clazz.getCanonicalName() + "'");
+        } else if (t instanceof ParameterizedType) {
+            ParameterizedType type = (ParameterizedType)t;
+            Type argType = type.getActualTypeArguments()[0];
+            if (argType instanceof Class<?>) {
+                return (Class<?>) argType;
+            } else if (argType instanceof ParameterizedType) {
+                // This happens if the input class X<T>
+                // parameter T is also parameterized.
+                ParameterizedType pargType = (ParameterizedType) argType;
+                return (Class<?>) pargType.getRawType();
+            } else {
+                throw new UnsupportedOperationException("Unsupported parameter type in class '" + clazz.getCanonicalName() + "': " + argType);
+            }
+        } else {
+            throw new UnsupportedOperationException("Unknown case in class '" + clazz.getCanonicalName() + "'");
+        }
+
+    }
+
+    /**
+     * Works like {@link #getSingleParameterType(Class)} assuming the retrieved
+     * parameter class extends <code>T</code>.
+     * 
+     * @param clazz
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> Class<T> getSingleParameterTypeExtending(Class<?> clazz) {
+
+        Type t = clazz.getGenericSuperclass();
+        while (t instanceof Class<?>) {
+            Class<?> cl = (Class) t;
+            t = cl.getGenericSuperclass();
+            if (t == null) {
+                // according to javadoc, we have reached java.lang.Object so no can do
+                break;
+            }
+        }
+        if(t instanceof Class<?>) {
+            throw new UnsupportedOperationException("Missing parameter type for input class '" + clazz.getCanonicalName() + "'");
+        } else if (t instanceof ParameterizedType) {
+            ParameterizedType type = (ParameterizedType)t;
+            Type argType = type.getActualTypeArguments()[0];
+            if (argType instanceof Class<?>) {
+                return (Class<T>) argType;
+            } else if (argType instanceof ParameterizedType) {
+                // This happens if the input class X<T>
+                // parameter T is also parameterized.
+                ParameterizedType pargType = (ParameterizedType) argType;
+                return (Class<T>) pargType.getRawType();
+            } else {
+                throw new UnsupportedOperationException("Unsupported parameter type in class '" + clazz.getCanonicalName() + "': " + argType);
+            }
+        } else {
+            throw new UnsupportedOperationException("Unknown case in class '" + clazz.getCanonicalName() + "'");
+        }
+
+    }
+
+}