You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Seba <lo...@gmail.com> on 2008/07/04 17:18:09 UTC

[BeanUtils] Possible bug or strange behavior

Hi
This is the first time I'm writing to a list, so I'm not sure whether this
should be sent to DevList or UserList. Sorry if I'm sending this to the
wrong list.
I'm using 1.7.0 version of commons-beanutils. The possible bug (or issue)
I've found is in lines 416 to 418 of class MethodUtils when I'm evaluating
an Enum's constant-specific method, which overrides an abstract one.
Those lines are the following:

415:        // If the declaring class is public, we are done
416:        Class clazz = method.getDeclaringClass();
417:        if (Modifier.isPublic(clazz.getModifiers())) {
418:            return (method);
419:        }

Since every constant of the Enum is compiled as a separate anonymous class
extending the Enum, evaluating its modifiers doesn't return public, which
causes the above return to be skipped.
My question is: should the above code be modified to consider that
particular case? Because the method is actually public and it's declaring
class (the constant, actually) too.

Here I show the code for testing this behavior:

=========================
The enum:

public enum SomeEnum {
   CONSTANT1 {
        public String getLabel() {
            return "a";
        }
    },
   CONSTANT2 {
        public String getLabel() {
            return "b";
        }
    };
    public abstract String getLabel();
}

=========================
The test class:

public class TestEnum {

    public static void main(String[] args) {
        SomeEnum a = SomeEnum.CONSTANT1;
        Method[] methods = a.getClass().getMethods();
        System.out.println(a.getClass());
        for (int i = 0; i < methods.length; i++) {
            Method method     = methods[i];
            System.out.println(method.getName() + " - " +
                    evaluateMethod(method));
        }
    }

    private static boolean evaluateMethod(Method method) {
        return Modifier.isPublic(method.getDeclaringClass().getModifiers());
    }