@Test public void testTypeOf() { try { typeOf(1, ParameterizedSuperType.class); fail(); } catch (UnsupportedOperationException uoe) { } assertEquals(String.class, typeOf(1, expandType(lookupGenericType(Map.class, ParameterizedSuperType.class), ParameterizedSuperType.class))); }
public final static Type typeOf(int parameterIdx, Type fromType) { if (fromType instanceof Class<?>) { Class<?> tClass = (Class<?>) fromType; TypeVariable<?>[] tvs = tClass.getTypeParameters(); if (tvs.length > parameterIdx) return expandType(tvs[parameterIdx], fromType); } else if (fromType instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) fromType; Type[] ts = pType.getActualTypeArguments(); if (ts.length > parameterIdx) return ts[parameterIdx]; } throw new UnsupportedOperationException("Couldn't find parameter at " + parameterIdx + " from type " + fromType + " , you should first locate the parameterized type, expand it and then use typeOf."); }
TypeUtil { public final static Type typeOf(int parameterIdx, Type fromType) { if (fromType instanceof Class<?>) { Class<?> tClass = (Class<?>) fromType; TypeVariable<?>[] tvs = tClass.getTypeParameters(); if (tvs.length > parameterIdx) return expandType(tvs[parameterIdx], fromType); } else if (fromType instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) fromType; Type[] ts = pType.getActualTypeArguments(); if (ts.length > parameterIdx) return ts[parameterIdx]; } throw new UnsupportedOperationException("Couldn't find parameter at " + parameterIdx + " from type " + fromType + " , you should first locate the parameterized type, expand it and then use typeOf."); } }
TypeUtil { public final static Type typeOf(int parameterIdx, Type fromType) { if (fromType instanceof Class<?>) { Class<?> tClass = (Class<?>) fromType; TypeVariable<?>[] tvs = tClass.getTypeParameters(); if (tvs.length > parameterIdx) return expandType(tvs[parameterIdx], fromType); } else if (fromType instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) fromType; Type[] ts = pType.getActualTypeArguments(); if (ts.length > parameterIdx) return ts[parameterIdx]; } throw new UnsupportedOperationException("Couldn't find parameter at " + parameterIdx + " from type " + fromType + " , you should first locate the parameterized type, expand it and then use typeOf."); }  }
TypeUtil { public final static Type typeOf(int parameterIdx, Type fromType) { if (fromType instanceof Class<?>) { Class<?> tClass = (Class<?>) fromType; TypeVariable<?>[] tvs = tClass.getTypeParameters(); if (tvs.length > parameterIdx) return expandType(tvs[parameterIdx], fromType); } else if (fromType instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) fromType; Type[] ts = pType.getActualTypeArguments(); if (ts.length > parameterIdx) return ts[parameterIdx]; } throw new UnsupportedOperationException("Couldn't find parameter at " + parameterIdx + " from type " + fromType + " , you should first locate the parameterized type, expand it and then use typeOf."); }  final static Class<?> wrap(Class<?> clazz); final static Type expandType(final Type type, final Type rootType); final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass); final static Class<?> getRawClass(Type type); final static Type getCollectionType(Type type); final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type, Class<?> inClass); final static boolean match(Type type, Type oType, boolean strictMatch); final static Type typeOf(int parameterIdx, Type fromType); }
TypeUtil { public final static Type typeOf(int parameterIdx, Type fromType) { if (fromType instanceof Class<?>) { Class<?> tClass = (Class<?>) fromType; TypeVariable<?>[] tvs = tClass.getTypeParameters(); if (tvs.length > parameterIdx) return expandType(tvs[parameterIdx], fromType); } else if (fromType instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) fromType; Type[] ts = pType.getActualTypeArguments(); if (ts.length > parameterIdx) return ts[parameterIdx]; } throw new UnsupportedOperationException("Couldn't find parameter at " + parameterIdx + " from type " + fromType + " , you should first locate the parameterized type, expand it and then use typeOf."); }  final static Class<?> wrap(Class<?> clazz); final static Type expandType(final Type type, final Type rootType); final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass); final static Class<?> getRawClass(Type type); final static Type getCollectionType(Type type); final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type, Class<?> inClass); final static boolean match(Type type, Type oType, boolean strictMatch); final static Type typeOf(int parameterIdx, Type fromType);  }