/** * Gets the clazz declaring the typevariable. * * @param declaringClazz * The declaring clazz. * @param target * The type variable we're looking for. * @return The clazz or one of it's declaring classes. */ public static Class<?> getDeclaringClass(final Class<?> declaringClazz, final TypeVariable<?> target) { Class<?> source = declaringClazz; boolean equals = false; // Test if we can find the declaring clazz. for (final TypeVariable<?> typeVariable : declaringClazz.getTypeParameters()) { if (typeVariable.equals(target)) { equals = true; } } // Search it's declaring clazz. final Class<?> nextDeclaring = declaringClazz.getDeclaringClass(); if (!equals && !CheckUtil.isNull(nextDeclaring)) { source = GenericsUtil.getDeclaringClass(nextDeclaring, target); } return source; }
final Class<?> declaringClass = GenericsUtil.getDeclaringClass(property.getDeclaringType(), typeVariable); int index = -1; final TypeVariable<?>[] typeVariables = declaringClass.getTypeParameters();
/** * Finds originating type for this field. * * @param properties * The fields to look through. * @return The true type. */ public static Type getRealType(final Deque<? extends Member> properties) { final Member property = properties.pop(); final Type fieldType = property.getGenericType(); Type result = null; if (fieldType instanceof TypeVariable) { final TypeVariable<?> typeVariable = (TypeVariable<?>) fieldType; final Class<?> declaringType = GenericsUtil.getDeclaringClass(property.getDeclaringType(), typeVariable); final TypeVariable<?> superTypeVariable = GenericsUtil.getSuperDeclaration(property, typeVariable); final int param = GenericsUtil.getRealParameter(superTypeVariable); result = GenericsUtil.getPropertyGenericType(declaringType, param, properties); } else if (fieldType instanceof Class) { result = fieldType; } else if (fieldType instanceof ParameterizedType) { final ParameterizedType pType = (ParameterizedType) fieldType; result = pType.getRawType(); } return result; }