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;fp=bundles%2Forg.simantics.utils%2Fsrc%2Forg%2Fsimantics%2Futils%2FReflectionUtils.java;h=5368b2e345a1fd579d3e2cadc3933b81b4a39bee;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 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 index 000000000..5368b2e34 --- /dev/null +++ b/bundles/org.simantics.utils/src/org/simantics/utils/ReflectionUtils.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * 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() + "'"); + } + + } + +}