]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/ReflectionUtils.java
Changing existing log4j logging to use slf4j
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / ReflectionUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils;\r
13 \r
14 import java.lang.reflect.ParameterizedType;\r
15 import java.lang.reflect.Type;\r
16 \r
17 public class ReflectionUtils {\r
18 \r
19     /**\r
20      * For a class <code>C extends PC&lt;P&gt;</code>, where <code>PC</code> is\r
21      * a class, not an interface, retrieves class <code>P</code>.\r
22      * \r
23      * @param clazz\r
24      * @return\r
25      */\r
26     public static Class<?> getSingleParameterType(Class<?> clazz) {\r
27 \r
28         Type t = clazz.getGenericSuperclass();\r
29         if(t instanceof Class<?>) {\r
30             throw new UnsupportedOperationException("Missing parameter type for input class '" + clazz.getCanonicalName() + "'");\r
31         } else if (t instanceof ParameterizedType) {\r
32             ParameterizedType type = (ParameterizedType)t;\r
33             Type argType = type.getActualTypeArguments()[0];\r
34             if (argType instanceof Class<?>) {\r
35                 return (Class<?>) argType;\r
36             } else if (argType instanceof ParameterizedType) {\r
37                 // This happens if the input class X<T>\r
38                 // parameter T is also parameterized.\r
39                 ParameterizedType pargType = (ParameterizedType) argType;\r
40                 return (Class<?>) pargType.getRawType();\r
41             } else {\r
42                 throw new UnsupportedOperationException("Unsupported parameter type in class '" + clazz.getCanonicalName() + "': " + argType);\r
43             }\r
44         } else {\r
45             throw new UnsupportedOperationException("Unknown case in class '" + clazz.getCanonicalName() + "'");\r
46         }\r
47 \r
48     }\r
49 \r
50     /**\r
51      * Works like {@link #getSingleParameterType(Class)} assuming the retrieved\r
52      * parameter class extends <code>T</code>.\r
53      * \r
54      * @param clazz\r
55      * @return\r
56      */\r
57     @SuppressWarnings("unchecked")\r
58     public static <T> Class<T> getSingleParameterTypeExtending(Class<?> clazz) {\r
59 \r
60         Type t = clazz.getGenericSuperclass();\r
61         if(t instanceof Class<?>) {\r
62             throw new UnsupportedOperationException("Missing parameter type for input class '" + clazz.getCanonicalName() + "'");\r
63         } else if (t instanceof ParameterizedType) {\r
64             ParameterizedType type = (ParameterizedType)t;\r
65             Type argType = type.getActualTypeArguments()[0];\r
66             if (argType instanceof Class<?>) {\r
67                 return (Class<T>) argType;\r
68             } else if (argType instanceof ParameterizedType) {\r
69                 // This happens if the input class X<T>\r
70                 // parameter T is also parameterized.\r
71                 ParameterizedType pargType = (ParameterizedType) argType;\r
72                 return (Class<T>) pargType.getRawType();\r
73             } else {\r
74                 throw new UnsupportedOperationException("Unsupported parameter type in class '" + clazz.getCanonicalName() + "': " + argType);\r
75             }\r
76         } else {\r
77             throw new UnsupportedOperationException("Unknown case in class '" + clazz.getCanonicalName() + "'");\r
78         }\r
79 \r
80     }\r
81 \r
82 }\r