Object extractValue(T instance, SingularAttribute<? super T, ?> attr) { Class<?> cls = instance.getClass(); Method getter = Reflection.findGetter(cls, attr.getName(), false); if (getter != null) return Reflection.get(instance, getter); Field field = Reflection.findField(cls, attr.getName(), false); if (field != null) return Reflection.get(instance, field); return null; }
/** * Return the setter method matching the given property name, optionally * throwing an exception if none. The property must also have a getter. */ public static Method findSetter(Class cls, String prop, boolean mustExist) { Method getter = findGetter(cls, prop, mustExist); return (getter == null) ? null : findSetter(cls, prop, getter.getReturnType(), mustExist); }
/** * Return the value of the property named by the hint key. */ public static Object hintToGetter(Object target, String hintKey) { if (target == null || hintKey == null) return null; Method getter = Reflection.findGetter(target.getClass(), hintKey, true); return Reflection.get(target, getter); }
private void setValue(int i, Object o, Object val) { if (meta.getAccessType() == ClassMetaData.ACCESS_PROPERTY) { if (!meta.isIntercepting()) { Method meth = Reflection.findSetter(meta.getDescribedType(), meta.getField(i).getName(), true); Reflection.set(o, meth, val); } else { Field field = Reflection.findField(meta.getDescribedType(), toFieldName(i), true); Reflection.set(o, field, val); } } else { Field field = (Field) meta.getField(i).getBackingMember(); Reflection.set(o, field, val); } }
private Object getValue(int i, Object o) { if (meta.getAccessType() == ClassMetaData.ACCESS_PROPERTY) { Field field = Reflection.findField(meta.getDescribedType(), toFieldName(i), true); return Reflection.get(o, field); } else { Field field = (Field) meta.getField(i).getBackingMember(); return Reflection.get(o, field); } }
/** * Set the value of the given field in the given object. * Same behavior as above methods, but parameter ordering is rearranged * to simplify usage from generated bytecodes. * * @since 1.0.0 */ public static void set(Object target, boolean value, Field field) { set(target, field, value); }
/** * Return the value of the given field in the given object. */ public static int getInt(Object target, Field field) { if (target == null || field == null) return 0; makeAccessible(field, field.getModifiers()); try { return field.getInt(target); } catch (Throwable t) { throw wrapReflectionException(t, _loc.get("get-field", target, field)); } }
/** * Set the value of the property named by the hint key. */ public static void hintToSetter(Object target, String hintKey, Object value) { if (target == null || hintKey == null) return; Method setter = Reflection.findSetter(target.getClass(), hintKey, true); if (value instanceof String) { if ("null".equals(value)) value = null; else { try { value = StringUtil.parse((String) value, setter.getParameterTypes()[0]); } catch (Exception e) { throw new UserException(_loc.get("bad-setter-hint-arg", hintKey, value, setter.getParameterTypes()[0])). setCause(e); } } } Reflection.set(target, setter, value); }
/** * Return the setter method matching the given property name, optionally * throwing an exception if none. */ public static Method findSetter(Class cls, String prop, Class param, boolean mustExist) { Method m = getSetterMethod(cls, prop); if (m != null) { return m; } String name = "set" + StringUtils.capitalize(prop); try { for (Class c = cls; c != null && c != Object.class; c = c.getSuperclass()) { m = getDeclaredMethod(c, name, param); if (m != null) { setSetterMethod(cls, prop, m); return m; } } } catch (Exception e) { throw new GeneralException(e); } if (mustExist) throw new UserException(_loc.get("bad-setter", cls, prop)); return null; }
Method m = getGetterMethod(cls, prop); if (m != null) { return m; m = getDeclaredMethod(c, "get" + capProp, null); if (m != null) { setGetterMethod(cls, prop, m); return m; } else { m = getDeclaredMethod(c, "is" + capProp, null); if (m != null && (m.getReturnType() == boolean.class || m.getReturnType() == Boolean.class)) { setGetterMethod(cls, prop, m); return m; } else { m = getDeclaredMethod(c, "get" + prop, null); if (m != null) { setGetterMethod(cls, prop, m); return m; } else { m = getDeclaredMethod(c, "is" + prop, null); if (m != null && (m.getReturnType() == boolean.class || m.getReturnType() == Boolean.class)) { setGetterMethod(cls, prop, m); return m;
/** * Return the return value of the given getter in the given object. */ public static byte getByte(Object target, Method getter) { Object o = get(target, getter); return (o == null) ? (byte) 0 : ((Number) o).byteValue(); }
/** * Gets the value of the given field of the original state of the object. * * @param field name of a persistent property * @return value of the given field in the original instance * @exception IllegalArgumentException if the named field is not a persistent property */ public Object getOriginalFieldValue(String field) { try { return Reflection.getValue(_original, field, true); } catch (Exception e) { throw new IllegalArgumentException(field + " does not exist in " + _original); } }
private Method getBackingMember(FieldMetaData fmd) { Member back = fmd.getBackingMember(); if (Method.class.isInstance(back)) return (Method)back; Method getter = Reflection.findGetter(meta.getDescribedType(), fmd.getName(), false); if (getter != null) fmd.backingMember(getter); return getter; }
/** * Return the field with the given name, optionally throwing an exception * if none. */ public static Field findField(Class cls, String name, boolean mustExist) { try { Field f; for (Class c = cls; c != null && c != Object.class; c = c.getSuperclass()) { f = getDeclaredField(c, name); if (f != null) return f; } } catch (Exception e) { throw new GeneralException(e); } if (mustExist) throw new UserException(_loc.get("bad-field", cls, name)); return null; }
/** * Invokes <code>cls.getDeclaredMethods()</code>, and returns the method * that matches the <code>name</code> and <code>param</code> arguments. * Avoids the exception thrown by <code>Class.getDeclaredMethod()</code> * for performance reasons. <code>param</code> may be null. Additionally, * if there are multiple methods with different return types, this will * return the method defined in the least-derived class. * * @since 0.9.8 */ static Method getDeclaredMethod(Class cls, String name, Class param) { Method[] methods = (Method[]) AccessController.doPrivileged( J2DoPrivHelper.getDeclaredMethodsAction(cls)); Method candidate = null; for (int i = 0 ; i < methods.length; i++) { if (name.equals(methods[i].getName())) { Class[] methodParams = methods[i].getParameterTypes(); if (param == null && methodParams.length == 0) candidate = mostDerived(methods[i], candidate); else if (param != null && methodParams.length == 1 && param.equals(methodParams[0])) candidate = mostDerived(methods[i], candidate); } } return candidate; }
private void hintToSetter(FetchPlan fetchPlan, String k, Object value) { if (fetchPlan == null || k == null) return; Method setter = Reflection.findSetter(fetchPlan.getClass(), k, true); Class paramType = setter.getParameterTypes()[0]; if (Enum.class.isAssignableFrom(paramType) && value instanceof String) value = Enum.valueOf(paramType, (String) value); Filters.hintToSetter(fetchPlan, k, value); }
/** * Return the getter method matching the given property name, optionally * throwing an exception if none. */ public static Method findGetter(Class cls, String prop, boolean mustExist) { prop = StringUtils.capitalize(prop); String name = "get" + prop; Method m; try { // this algorithm searches for a get<prop> or is<prop> method in // a breadth-first manner. for (Class c = cls; c != null && c != Object.class; c = c.getSuperclass()) { m = getDeclaredMethod(c, name, null); if (m != null) { return m; } else { m = getDeclaredMethod(c, "is" + prop, null); if (m != null && (m.getReturnType() == boolean.class || m.getReturnType() == Boolean.class)) return m; } } } catch (Exception e) { throw new GeneralException(e); } if (mustExist) throw new UserException(_loc.get("bad-getter", cls, prop)); return null; }
private void setValue(int i, Object o, Object val) { FieldMetaData fmd = meta.getField(i); if (AccessCode.isProperty(fmd.getAccessType())) { if (!meta.isIntercepting()) { Method meth = Reflection.findSetter(meta.getDescribedType(), meta.getField(i).getName(), true); Reflection.set(o, meth, val); } else { Field field = Reflection.findField(meta.getDescribedType(), toFieldName(i), true); Reflection.set(o, field, val); } } else { Field field = (Field) meta.getField(i).getBackingMember(); Reflection.set(o, field, val); } }
private Object getValue(int i, Object o) { FieldMetaData fmd = meta.getField(i); if (AccessCode.isProperty(fmd.getAccessType())) { Field field = Reflection.findField(meta.getDescribedType(), toFieldName(i), true); return Reflection.get(o, field); } else { Field field = (Field) meta.getField(i).getBackingMember(); return Reflection.get(o, field); } }