X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.utils%2Fsrc%2Forg%2Fsimantics%2Futils%2FReflectionUtils.java;h=7f0f6c589a4c8f290db1c48eccf915535e477340;hp=5368b2e345a1fd579d3e2cadc3933b81b4a39bee;hb=8905e67e82991121305889e6474a124d6ec79bf8;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/ReflectionUtils.java b/bundles/org.simantics.utils/src/org/simantics/utils/ReflectionUtils.java index 5368b2e34..7f0f6c589 100644 --- a/bundles/org.simantics.utils/src/org/simantics/utils/ReflectionUtils.java +++ b/bundles/org.simantics.utils/src/org/simantics/utils/ReflectionUtils.java @@ -1,82 +1,90 @@ -/******************************************************************************* - * 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 C extends PC<P>, where PC is - * a class, not an interface, retrieves class P. - * - * @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 - // 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 T. - * - * @param clazz - * @return - */ - @SuppressWarnings("unchecked") - public static Class getSingleParameterTypeExtending(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 - // 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() + "'"); - } - - } - -} +/******************************************************************************* + * 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 C extends PC<P>, where PC is + * a class, not an interface, retrieves class P. + * + * @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 + // 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 T. + * + * @param clazz + * @return + */ + @SuppressWarnings("unchecked") + public static Class 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) argType; + } else if (argType instanceof ParameterizedType) { + // This happens if the input class X + // 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() + "'"); + } + + } + +}