private Class<? extends Serializable> resolvePropertyType(Class<?> baseClazz, String property) { Class<?> clazz = baseClazz; String[] propertyParts = property.split("\\."); for (int i = 0; i < propertyParts.length; i++) { Method getter = ReflectionUtils.getGetter(clazz, propertyParts[i]); if (getter == null) { Field field = ReflectionUtils.getField(clazz, propertyParts[i]); if (field == null) { throw new IllegalArgumentException("Couldn't find property '" + propertyParts[i] + "' on type '" + clazz + "' while resolving path '" + property + "' on type '" + baseClazz + "'"); } Class<?>[] typeArguments = ReflectionUtils.getResolvedFieldTypeArguments(clazz, field); if (typeArguments.length == 0) { clazz = ReflectionUtils.getResolvedFieldType(clazz, field); } else { clazz = typeArguments[typeArguments.length - 1]; } } else { Class<?>[] typeArguments = ReflectionUtils.getResolvedMethodReturnTypeArguments(clazz, getter); if (typeArguments.length == 0) { clazz = ReflectionUtils.getResolvedMethodReturnType(clazz, getter); } else { clazz = typeArguments[typeArguments.length - 1]; } } } return (Class<? extends Serializable>) clazz; }
if (ReflectionUtils.isSetter(m)) { attributeName = getAttributeName(m); Method getter = ReflectionUtils.getGetter(viewType, attributeName); if (ReflectionUtils.getResolvedMethodParameterTypes(viewType, m)[0] != ReflectionUtils.getResolvedMethodReturnType(viewType, getter)) { context.addError("The setter '" + m.getName() + "' of the class '" + viewType.getName() + "' must accept an argument of the same type as it's corresponding getter returns!"); } else if (!ReflectionUtils.isGetter(m)) { context.addError("The given method '" + m.getName() + "' from the entity view '" + viewType.getName() + "' is no bean style getter or setter!"); } else { attributeName = getAttributeName(m); Method setter = ReflectionUtils.getSetter(viewType, attributeName); if (setter != null && ReflectionUtils.getResolvedMethodParameterTypes(viewType, setter)[0] != ReflectionUtils.getResolvedMethodReturnType(viewType, m)) { context.addError("The getter '" + m.getName() + "' of the class '" + viewType.getName() + "' must have the same return type as it's corresponding setter accepts!");
public static Class<?> getResolvedMethodReturnType(Class<?> clazz, String methodName, Class<?>... parameterTypes) { return getResolvedMethodReturnType(clazz, getMethod(clazz, methodName, parameterTypes)); }
public static Class<?>[] getResolvedMethodReturnTypeArguments(Class<?> clazz, String methodName, Class<?>... parameterTypes) { return getResolvedMethodReturnTypeArguments(clazz, getMethod(clazz, methodName, parameterTypes)); }
public static Class<?> getResolvedFieldType(Class<?> clazz, String fieldName) { return getResolvedFieldType(clazz, getField(clazz, fieldName)); }
private static Class<?> findEntityViewClass(Class<?> repositoryClass, Method methodToAnalyze, EntityViewManager evm) { Class<?> entityViewClass; Class<?>[] typeArguments = ReflectionUtils.getResolvedMethodReturnTypeArguments(repositoryClass, methodToAnalyze); if (typeArguments.length == 0) { entityViewClass = ReflectionUtils.getResolvedMethodReturnType(repositoryClass, methodToAnalyze); } else { entityViewClass = typeArguments[typeArguments.length - 1]; } ManagedViewType<?> managedViewType = evm.getMetamodel().managedView(entityViewClass); if (managedViewType == null) { return null; } return managedViewType.getJavaType(); }
if (attr.getJavaMember() instanceof Method) { Method method = (Method) attr.getJavaMember(); Class<?>[] typeArguments = ReflectionUtils.getResolvedMethodReturnTypeArguments(resolverBaseClass, method); Class<?>[] typeArguments = ReflectionUtils.getResolvedFieldTypeArguments(resolverBaseClass, field); if (attr.getJavaMember() instanceof Method) { Method method = (Method) attr.getJavaMember(); Class<?>[] typeArguments = ReflectionUtils.getResolvedMethodReturnTypeArguments(resolverBaseClass, method); Class<?>[] typeArguments = ReflectionUtils.getResolvedFieldTypeArguments(resolverBaseClass, field); return jpaReportedFieldClass; fieldClass = ReflectionUtils.getResolvedMethodReturnType(resolverBaseClass, method); if (fieldClass == null) { fieldClass = resolveType(resolverBaseClass, method.getGenericReturnType()); fieldClass = ReflectionUtils.getResolvedFieldType(resolverBaseClass, field); if (fieldClass == null) { fieldClass = resolveType(resolverBaseClass, field.getGenericType());
Class<?>[] typeArguments = ReflectionUtils.getResolvedMethodReturnTypeArguments(resolverBaseClass, method); Class<?>[] typeArguments = ReflectionUtils.getResolvedFieldTypeArguments(resolverBaseClass, field);
/** * Returns the type of a field if it exists within the class. Calling this * method is equal to calling # * {@link ReflectionUtils#getField(java.lang.Class, java.lang.String) * } with * a null check and finally return the type via getType(). * * @param clazz The class within to look for the field with the given field * name * @param fieldName The name of the field to be returned * @return The type of the field if it can be found, otherwise null * @see ReflectionUtils#getField(java.lang.Class, java.lang.String) */ public static Class<?> getFieldType(Class<?> clazz, String fieldName) { Field f = getField(clazz, fieldName); if (f == null) { return null; } return f.getType(); }
final Method getter = ReflectionUtils.getGetter( current, properties[i]); getters.add(getter); current = ReflectionUtils .getResolvedMethodReturnType(current, getter); leafGetter = ReflectionUtils.getGetter(current, properties[getterChainLength]); leafSetter = ReflectionUtils.getSetter(current, properties[getterChainLength]);
public static Class<?>[] getResolvedFieldTypeArguments(Class<?> clazz, String fieldName) { return getResolvedFieldTypeArguments(clazz, getField(clazz, fieldName)); }
ViewAttributeAccessor(EntityViewManagerImpl evm, MethodAttribute<?, ?> attribute, boolean readonly) { Class<?> clazz = attribute.getDeclaringType().getJavaType(); Method getter = ReflectionUtils.getGetter(clazz, attribute.getName()); if (!Modifier.isPublic(getter.getModifiers()) || !Modifier.isPublic(getter.getDeclaringClass().getModifiers())) { try { getter.setAccessible(true); } catch (Exception e) { throw new RuntimeException("Couldn't make method for entity view attribute accessible for reading!", e); } } this.getter = getter; if (readonly) { this.field = null; } else { Class<?> proxyClass = evm.getProxyFactory().getProxy(evm, (ManagedViewTypeImplementor<Object>) attribute.getDeclaringType(), null); Field f = ReflectionUtils.getField(proxyClass, attribute.getName()); try { f.setAccessible(true); } catch (Exception e) { throw new RuntimeException("Couldn't make field for entity view attribute accessible for writing!", e); } this.field = f; } }
@Override protected Class<?>[] getTypeArguments() { return ReflectionUtils.getResolvedMethodReturnTypeArguments(getDeclaringType().getJavaType(), getJavaMethod()); }
public static boolean isSorted(Class<?> clazz, Method method) { return isSorted(ReflectionUtils.getResolvedMethodReturnType(clazz, method)); }
private static AttributeAccessor forEntityAttribute(EntityViewManagerImpl evm, Class<?> entityClass, Attribute<?, ?> attribute, Class<?> targetType) { Member member = attribute.getJavaMember(); if (member instanceof Field) { return new EntityFieldAttributeAccessor(evm.getJpaProvider(), (Field) member, targetType); } else if (member instanceof Method) { Method getter = ReflectionUtils.getGetter(entityClass, attribute.getName()); Method setter = ReflectionUtils.getSetter(entityClass, attribute.getName()); return new EntityMethodAttributeAccessor(getter, setter, targetType); } else { throw new IllegalArgumentException("Unsupported java member for id attribute: " + member); } } }
@SuppressWarnings("unchecked") private Y getValue(X target, boolean nullSafe) { initialize(); try { Object leafObj = getLeafObject(target, nullSafe); final Method getter = leafGetter == null && leafObj != null ? ReflectionUtils .getGetter( leafObj.getClass(), explodedPropertyPath[explodedPropertyPath.length - 1]) : leafGetter; return nullSafe && leafObj == null ? null : (Y) getter .invoke(leafObj); } catch (RuntimeException ex) { throw ex; } catch (Exception ex) { throw new RuntimeException(ex); } }
public final void setValue(X target, Y value) { initialize(); try { Object leafObj = getLeafObject(target, false); final Method setter = leafSetter == null && leafObj != null ? ReflectionUtils .getSetter( leafObj.getClass(), explodedPropertyPath[explodedPropertyPath.length - 1]) : leafSetter; setter.invoke(leafObj, value); } catch (RuntimeException ex) { throw ex; } catch (Exception ex) { throw new RuntimeException(ex); } }
final Class<?> primitiveIdClass = ReflectionUtils.getPrimitiveClassOfWrapper(idClass); if (primitiveIdClass != null) { return Collections.singletonList(entityType.getId(primitiveIdClass).getName());
private static void validateTypesCompatible(List<TargetType> possibleTargets, Class<?> targetType, Class<?> targetElementType, boolean subtypesAllowed, boolean singular, MetamodelBuildingContext context, ExpressionLocation expressionLocation, String location) { final Class<?> expressionType = targetType; if (!possibleTargets.isEmpty()) { boolean error = true; for (TargetType t : possibleTargets) { if (isCompatible(t, targetType, targetElementType, subtypesAllowed, singular)) { error = false; break; } } if (error) { if (targetType.isPrimitive()) { targetType = ReflectionUtils.getObjectClassOfPrimitve(targetType); } else { targetType = ReflectionUtils.getPrimitiveClassOfWrapper(targetType); } if (targetType != null) { for (TargetType t : possibleTargets) { if (isCompatible(t, targetType, targetElementType, subtypesAllowed, singular)) { error = false; break; } } } } if (error) { context.addError(typeCompatibilityError(possibleTargets, expressionType, targetElementType, expressionLocation, location)); } } }
private static Class<?> findEntityViewClass(Class<?> repositoryClass, Method methodToAnalyze, EntityViewManager evm) { Class<?> entityViewClass; Class<?>[] typeArguments = ReflectionUtils.getResolvedMethodReturnTypeArguments(repositoryClass, methodToAnalyze); if (typeArguments.length == 0) { entityViewClass = ReflectionUtils.getResolvedMethodReturnType(repositoryClass, methodToAnalyze); } else { entityViewClass = typeArguments[typeArguments.length - 1]; } ManagedViewType<?> managedViewType = evm.getMetamodel().managedView(entityViewClass); if (managedViewType == null) { return null; } return managedViewType.getJavaType(); }