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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils;
\r
14 import java.lang.reflect.ParameterizedType;
\r
15 import java.lang.reflect.Type;
\r
17 public class ReflectionUtils {
\r
20 * For a class <code>C extends PC<P></code>, where <code>PC</code> is
\r
21 * a class, not an interface, retrieves class <code>P</code>.
\r
26 public static Class<?> getSingleParameterType(Class<?> clazz) {
\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
42 throw new UnsupportedOperationException("Unsupported parameter type in class '" + clazz.getCanonicalName() + "': " + argType);
\r
45 throw new UnsupportedOperationException("Unknown case in class '" + clazz.getCanonicalName() + "'");
\r
51 * Works like {@link #getSingleParameterType(Class)} assuming the retrieved
\r
52 * parameter class extends <code>T</code>.
\r
57 @SuppressWarnings("unchecked")
\r
58 public static <T> Class<T> getSingleParameterTypeExtending(Class<?> clazz) {
\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
74 throw new UnsupportedOperationException("Unsupported parameter type in class '" + clazz.getCanonicalName() + "': " + argType);
\r
77 throw new UnsupportedOperationException("Unknown case in class '" + clazz.getCanonicalName() + "'");
\r