/** Find the nearest superclass (or same class) that is public to this one. */ private static <C> Class<? super C> findAccessibleClass(Class<C> clazz) { if (Modifier.isPublic(clazz.getModifiers())) { return clazz; } else { Class<? super C> sup = clazz.getSuperclass(); if (sup == null) { return Object.class; // handle interfaces } return findAccessibleClass(sup); } }
/** Create a support with methods specified by name. * The instance class will be examined for the named methods. * But if the instance class is not public, the nearest public superclass * will be used instead, so that the getters and setters remain accessible. * @param instance (Bean) object to work on * @param valueType type of the property * @param getter name of getter method, can be <code>null</code> * @param setter name of setter method, can be <code>null</code> * @exception NoSuchMethodException if the getter or setter methods cannot be found */ public Reflection(Object instance, Class<T> valueType, String getter, String setter) throws NoSuchMethodException { this( instance, valueType, ( // find the getter () getter == null) ? null : findAccessibleClass(instance.getClass()).getMethod(getter), ( // find the setter (valueType) setter == null) ? null : findAccessibleClass(instance.getClass()).getMethod( setter, new Class<?>[] { valueType } ) ); }
private static Method findGetter(Object instance, Class valueType, String property) throws NoSuchMethodException { NoSuchMethodException nsme; try { return findAccessibleClass(instance.getClass()).getMethod( firstLetterToUpperCase(property, "get") ); } catch (NoSuchMethodException e) { if (valueType != boolean.class) { throw e; } else { nsme = e; } } // Is of type boolean and "get" getter does not exist try { return findAccessibleClass(instance.getClass()).getMethod( firstLetterToUpperCase(property, "is") ); } catch (NoSuchMethodException e) { throw e; } }
private static Method findGetter( Object instance, Class valueType, String property ) throws NoSuchMethodException { NoSuchMethodException nsme; try { return findAccessibleClass (instance.getClass ()).getMethod ( firstLetterToUpperCase (property, "get"), new Class[0] ); } catch ( NoSuchMethodException e ) { if ( valueType != boolean.class ) { throw e; } else { nsme = e; } } // Is of type boolean and "get" getter does not exist try { return findAccessibleClass (instance.getClass ()).getMethod ( firstLetterToUpperCase (property, "is"), new Class[0] ); } catch ( NoSuchMethodException e ) { throw e; } }
/** Create a support with methods specified by name. * The instance class will be examined for the named methods. * But if the instance class is not public, the nearest public superclass * will be used instead, so that the getters and setters remain accessible. * @param instance (Bean) object to work on * @param valueType type of the property * @param getter name of getter method, can be <code>null</code> * @param setter name of setter method, can be <code>null</code> * @exception NoSuchMethodException if the getter or setter methods cannot be found */ public Reflection (Object instance, Class valueType, String getter, String setter) throws NoSuchMethodException { this ( instance, valueType, // find the getter () getter == null ? null : findAccessibleClass (instance.getClass ()).getMethod ( getter, new Class[0] ), // find the setter (valueType) setter == null ? null : findAccessibleClass (instance.getClass ()).getMethod ( setter, new Class[] { valueType } ) ); }
/** Create a support with methods specified by name. * The instance class will be examined for the named methods. * But if the instance class is not public, the nearest public superclass * will be used instead, so that the getters and setters remain accessible. * @param instance (Bean) object to work on * @param valueType type of the property * @param getter name of getter method, can be <code>null</code> * @param setter name of setter method, can be <code>null</code> * @exception NoSuchMethodException if the getter or setter methods cannot be found */ public Reflection (Object instance, Class valueType, String getter, String setter) throws NoSuchMethodException { this ( instance, valueType, // find the getter () getter == null ? null : findAccessibleClass (instance.getClass ()).getMethod ( getter, new Class[0] ), // find the setter (valueType) setter == null ? null : findAccessibleClass (instance.getClass ()).getMethod ( setter, new Class[] { valueType } ) ); }
/** Create a support based on the property name. * The getter and setter methods are constructed by capitalizing the first * letter in the name of propety and prefixing it with <code>get</code> and * <code>set</code>, respectively. * * @param instance object to work on * @param valueType type of the property * @param property name of property * @exception NoSuchMethodException if the getter or setter methods cannot be found */ public Reflection (Object instance, Class valueType, String property) throws NoSuchMethodException { this ( instance, valueType, findGetter( instance, valueType, property ), findAccessibleClass (instance.getClass ()).getMethod ( firstLetterToUpperCase (property, "set"), new Class[] { valueType } ) ); }
private static Method findGetter( Object instance, Class valueType, String property ) throws NoSuchMethodException { NoSuchMethodException nsme; try { return findAccessibleClass (instance.getClass ()).getMethod ( firstLetterToUpperCase (property, "get"), new Class[0] ); } catch ( NoSuchMethodException e ) { if ( valueType != boolean.class ) { throw e; } else { nsme = e; } } // Is of type boolean and "get" getter does not exist try { return findAccessibleClass (instance.getClass ()).getMethod ( firstLetterToUpperCase (property, "is"), new Class[0] ); } catch ( NoSuchMethodException e ) { throw e; } }
/** Create a support based on the property name. * The getter and setter methods are constructed by capitalizing the first * letter in the name of propety and prefixing it with <code>get</code> and * <code>set</code>, respectively. * * @param instance object to work on * @param valueType type of the property * @param property name of property * @exception NoSuchMethodException if the getter or setter methods cannot be found */ public Reflection (Object instance, Class valueType, String property) throws NoSuchMethodException { this ( instance, valueType, findGetter( instance, valueType, property ), findAccessibleClass (instance.getClass ()).getMethod ( firstLetterToUpperCase (property, "set"), new Class[] { valueType } ) ); }
/** Find the nearest superclass (or same class) that is public to this one. */ private static Class findAccessibleClass (Class clazz) { if (Modifier.isPublic (clazz.getModifiers ())) { return clazz; } else { Class sup = clazz.getSuperclass (); if (sup == null) return Object.class; // handle interfaces return findAccessibleClass (sup); } }
/** Find the nearest superclass (or same class) that is public to this one. */ private static Class findAccessibleClass (Class clazz) { if (Modifier.isPublic (clazz.getModifiers ())) { return clazz; } else { Class sup = clazz.getSuperclass (); if (sup == null) return Object.class; // handle interfaces return findAccessibleClass (sup); } }
/** Create a support based on the property name. * The getter and setter methods are constructed by capitalizing the first * letter in the name of propety and prefixing it with <code>get</code> and * <code>set</code>, respectively. * * @param instance object to work on * @param valueType type of the property * @param property name of property * @exception NoSuchMethodException if the getter or setter methods cannot be found */ public Reflection(Object instance, Class<T> valueType, String property) throws NoSuchMethodException { this( instance, valueType, findGetter(instance, valueType, property), findAccessibleClass(instance.getClass()).getMethod( firstLetterToUpperCase(property, "set"), valueType ) ); }