Announcement Announcement Module
Collapse
No announcement yet.
Missing method in ReflectionUtils? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Missing method in ReflectionUtils?

    Was there a reason why a method wasn't included in the ReflectionUtils class to get all of the declared fields for a particular class and all super classes? There is a method to get all of the declared methods but not fields.

    The reason I ask is that I've been playing around with the new BeanPropertyRowMapper that's included in 2.1 M1 and it only pulls the fields that are declared by the the mapped class. A problem I see with that is what if I have a class that extends a base class that holds additional fields that need to be mapped to. By just pulling the declared fields for the mapped class alone and not it's super classes, all of the fields that I'm querying won't be populated.

    All it would take is to include the following method in the ReflectionUtils. The helper methods already exist to get the job done.

    Code:
    	/**
    	 * Get all declared fields on the leaf class and all superclasses. Leaf
    	 * class fields are included first.
    	 */
    	public static Field[] getAllDeclaredFields(Class leafClass) throws llegalArgumentException {
    		final List l = new LinkedList();
    		doWithFields(leafClass, new FieldCallback() {
    			public void doWith(Field f) {
    				l.add(f);
    			}
    		});
    		return (Field[]) l.toArray(new Field[l.size()]);
    	}
    And also to fix the BeanPropertyRowMapper it would just take fixing one line. The line in the initialize method:

    Code:
    Field[] f = mappedClass.getDeclaredFields();
    Would now be:

    Code:
    Field[] f = ReflectionUtils.getAllDeclaredFields(mappedClass);
    Any thoughts?

    -Ben

  • #2
    ReflectionUtils allows you to collect the fields using doWithFields and FieldCallback, so the extra convenience method might not be necessary. But I agree that this might be a good choice for the row mapper. Raise an issue in JIRA?

    Comment

    Working...
    X