]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/ReflectionUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / ReflectionUtils.java
diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/ReflectionUtils.java b/bundles/org.simantics.utils/src/org/simantics/utils/ReflectionUtils.java
new file mode 100644 (file)
index 0000000..5368b2e
--- /dev/null
@@ -0,0 +1,82 @@
+/*******************************************************************************\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