/** * collects all TypeVariable -> Type mappings from the generic interfaces */ private static void collectActualTypeArguments(final Type[] genericInterfaces, final Map<TypeVariable<?>, Type> map) { for (Type genericInterface : genericInterfaces) { if (genericInterface instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) genericInterface; collectActualTypeArguments(parameterizedType, map); Type rawType = parameterizedType.getRawType(); collectActualTypeArguments(getGenericInterfaces(rawType), map); } else { collectActualTypeArguments(getGenericInterfaces(genericInterface), map); } } }
/** * Try to resolve the property type for the supplied methods and target class. The supplied read and write methods * should resemble a Java bean property (setter and getter) * * @param targetClass the target class of the property * @param readMethod the read method of the property * @param writeMethod the write method of the property * @return the actual class of the property type */ public static Class<?> resolvePropertyType(final Class<?> targetClass, final Method readMethod, final Method writeMethod) { Class<?> type = null; if (readMethod != null) { type = resolveReturnType(targetClass, readMethod); } if ((type == null) && (writeMethod != null)) { type = resolveParameterType(targetClass, writeMethod); } return type; }
/** * resolves the parameter return type of the method */ private static Class<?> resolveParameterType(final Class<?> targetClass, final Method method) { // get the generic parametertypes of the method Type[] types = method.getGenericParameterTypes(); // check for correct argument list length and type if ((types == null) || (types.length < 1)) return null; // get the actual type argument (expect the type to be a TypeVariable) return resolveType(targetClass, types[0]); }
private Class determinePropertyType() { Method readMethod = getBridgedReadMethod(); Method writeMethod = getBridgedWriteMethod(); Class returnType = null; try { returnType = TypeResolver.resolvePropertyType(clazz, readMethod, writeMethod); } catch (Exception ignore) { } if (returnType != null) { return returnType; } if (readMethod == null && writeMethod == null) { throw new MappingException("No read or write method found for field (" + fieldName + ") in class (" + clazz + ")"); } if (readMethod == null) { return determineByWriteMethod(writeMethod); } else { try { return readMethod.getReturnType(); } catch (Exception e) { // let us try the set method - the field might have inacessible 'get' method return determineByWriteMethod(writeMethod); } } }
private static Class<?> resolveType(final Class<?> targetClass, final Type type) { if (type instanceof Class) return (Class<?>) type; if (!(type instanceof TypeVariable<?>)) { return null; } Type targetType = null; Map<TypeVariable<?>, Type> typeMap = getTypeMap(targetClass); for (Map.Entry<TypeVariable<?>, Type> typeVariableEntry : typeMap.entrySet()) { TypeVariable<?> typeVariable = typeVariableEntry.getKey(); if (typeVariable.getName().equals(((TypeVariable) type).getName())) { targetType = typeVariableEntry.getValue(); break; } } if (targetType instanceof Class) { return (Class<?>) targetType; } return null; }
/** * returns an already cached [TypeVariable -> Type] map for the targetClass or build a new one. */ private static Map<TypeVariable<?>, Type> getTypeMap(final Class<?> targetClass) { // check if a TypeMap already exists for this class if (typeMaps.containsKey(targetClass)) { return typeMaps.get(targetClass); } // build a new TypeVariable -> Type map Map<TypeVariable<?>, Type> map = new HashMap<TypeVariable<?>, Type>(); Class<?> clazz = targetClass; while (!Object.class.equals(clazz)) { Type genericSuperClass = clazz.getGenericSuperclass(); if (genericSuperClass instanceof ParameterizedType) { collectActualTypeArguments((ParameterizedType) genericSuperClass, map); } collectActualTypeArguments(clazz.getGenericInterfaces(), map); clazz = clazz.getSuperclass(); } typeMaps.put(targetClass, map); return map; }
private Class determinePropertyType() { Method readMethod = getBridgedReadMethod(); Method writeMethod = getBridgedWriteMethod(); Class returnType = null; try { returnType = TypeResolver.resolvePropertyType(clazz, readMethod, writeMethod); } catch (Exception ignore) { } if (returnType != null) { return returnType; } if (readMethod == null && writeMethod == null) { throw new MappingException( "No read or write method found for field (" + fieldName + ") in class (" + clazz + ")"); } if (readMethod == null) { return determineByWriteMethod(writeMethod); } else { try { return readMethod.getReturnType(); } catch (Exception e) { // let us try the set method - the field might have inacessible // 'get' method return determineByWriteMethod(writeMethod); } } }
private static Class<?> resolveType(final Class<?> targetClass, final Type type) { if (type instanceof Class) return (Class<?>) type; if (!(type instanceof TypeVariable<?>)) { return null; } Type targetType = null; Map<TypeVariable<?>, Type> typeMap = getTypeMap(targetClass); for (Map.Entry<TypeVariable<?>, Type> typeVariableEntry : typeMap.entrySet()) { TypeVariable<?> typeVariable = typeVariableEntry.getKey(); if (typeVariable.getName().equals(((TypeVariable) type).getName())) { targetType = typeVariableEntry.getValue(); break; } } if (targetType instanceof Class) { return (Class<?>) targetType; } return null; }
/** * returns an already cached [TypeVariable -> Type] map for the targetClass * or build a new one. */ private static Map<TypeVariable<?>, Type> getTypeMap(final Class<?> targetClass) { // check if a TypeMap already exists for this class if (typeMaps.containsKey(targetClass)) { return typeMaps.get(targetClass); } // build a new TypeVariable -> Type map Map<TypeVariable<?>, Type> map = new HashMap<TypeVariable<?>, Type>(); Class<?> clazz = targetClass; while (!Object.class.equals(clazz)) { Type genericSuperClass = clazz.getGenericSuperclass(); if (genericSuperClass instanceof ParameterizedType) { collectActualTypeArguments((ParameterizedType) genericSuperClass, map); } collectActualTypeArguments(clazz.getGenericInterfaces(), map); clazz = clazz.getSuperclass(); } typeMaps.put(targetClass, map); return map; }
/** * collects all TypeVariable -> Type mappings from the generic interfaces */ private static void collectActualTypeArguments(final Type[] genericInterfaces, final Map<TypeVariable<?>, Type> map) { for (Type genericInterface : genericInterfaces) { if (genericInterface instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) genericInterface; collectActualTypeArguments(parameterizedType, map); Type rawType = parameterizedType.getRawType(); collectActualTypeArguments(getGenericInterfaces(rawType), map); } else { collectActualTypeArguments(getGenericInterfaces(genericInterface), map); } } }
/** * Try to resolve the property type for the supplied methods and target * class. The supplied read and write methods should resemble a Java bean * property (setter and getter) * * @param targetClass the target class of the property * @param readMethod the read method of the property * @param writeMethod the write method of the property * @return the actual class of the property type */ public static Class<?> resolvePropertyType(final Class<?> targetClass, final Method readMethod, final Method writeMethod) { Class<?> type = null; if (readMethod != null) { type = resolveReturnType(targetClass, readMethod); } if ((type == null) && (writeMethod != null)) { type = resolveParameterType(targetClass, writeMethod); } return type; }
private Class determinePropertyType() { Method readMethod = getBridgedReadMethod(); Method writeMethod = getBridgedWriteMethod(); Class returnType = null; try { returnType = TypeResolver.resolvePropertyType(clazz, readMethod, writeMethod); } catch (Exception ignore) { } if (returnType != null) { return returnType; } if (readMethod == null && writeMethod == null) { throw new MappingException( "No read or write method found for field (" + fieldName + ") in class (" + clazz + ")"); } if (readMethod == null) { return determineByWriteMethod(writeMethod); } else { try { return readMethod.getReturnType(); } catch (Exception e) { // let us try the set method - the field might have inacessible // 'get' method return determineByWriteMethod(writeMethod); } } }
/** * resolves the parameter return type of the method */ private static Class<?> resolveParameterType(final Class<?> targetClass, final Method method) { // get the generic parametertypes of the method Type[] types = method.getGenericParameterTypes(); // check for correct argument list length and type if ((types == null) || (types.length < 1)) return null; // get the actual type argument (expect the type to be a TypeVariable) return resolveType(targetClass, types[0]); }
/** * resolves the generic return type of the method */ private static Class<?> resolveReturnType(final Class<?> targetClass, final Method method) { // get the generic return type Type type = method.getGenericReturnType(); // get the actual type argument (expect the type to be a TypeVariable) return resolveType(targetClass, type); }
/** * resolves the generic return type of the method */ private static Class<?> resolveReturnType(final Class<?> targetClass, final Method method) { // get the generic return type Type type = method.getGenericReturnType(); // get the actual type argument (expect the type to be a TypeVariable) return resolveType(targetClass, type); }