Author: niallp
Date: Tue Jul 10 22:18:17 2007
New Revision: 555172

URL: http://svn.apache.org/viewvc?view=rev&rev=555172
Log:
BEANUTILS-273 - Public methods overriden in anonymous or private subclasses are 
not recognized by PropertyUtils - reported by Marcelo Liberato

Added:
    
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira273TestCase.java
   (with props)
    
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira273BeanFactory.java
   (with props)
Modified:
    
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java

Modified: 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java?view=diff&rev=555172&r1=555171&r2=555172
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java
 (original)
+++ 
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java
 Tue Jul 10 22:18:17 2007
@@ -709,17 +709,54 @@
             return (method);
         }
 
+        String methodName      = method.getName();
+        Class[] parameterTypes = method.getParameterTypes();
+
         // Check the implemented interfaces and subinterfaces
         method =
                 getAccessibleMethodFromInterfaceNest(clazz,
-                        method.getName(),
-                        method.getParameterTypes());
+                        methodName,
+                        parameterTypes);
+
+        // Check the superclass chain
+        if (method == null) {
+            method = getAccessibleMethodFromSuperclass(clazz,
+                        methodName,
+                        parameterTypes);
+        }
+
         return (method);
 
     }
 
 
     // -------------------------------------------------------- Private Methods
+
+    /**
+     * <p>Return an accessible method (that is, one that can be invoked via
+     * reflection) by scanning through the superclasses. If no such method
+     * can be found, return <code>null</code>.</p>
+     *
+     * @param clazz Class to be checked
+     * @param methodName Method name of the method we wish to call
+     * @param parameterTypes The parameter type signatures
+     */
+    private static Method getAccessibleMethodFromSuperclass
+            (Class clazz, String methodName, Class parameterTypes[]) {
+
+        Class parentClazz = clazz.getSuperclass();
+        while (parentClazz != null) {
+            if (Modifier.isPublic(parentClazz.getModifiers())) {
+                try {
+                    return parentClazz.getMethod(methodName, parameterTypes);
+                } catch (NoSuchMethodException e) {
+                    return null;
+                }
+            }
+            parentClazz = parentClazz.getSuperclass();
+        }
+        return null;
+    }
 
     /**
      * <p>Return an accessible method (that is, one that can be invoked via

Added: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira273TestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira273TestCase.java?view=auto&rev=555172
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira273TestCase.java
 (added)
+++ 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira273TestCase.java
 Tue Jul 10 22:18:17 2007
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.beanutils.bugs;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.beanutils.bugs.other.Jira273BeanFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Public methods overriden in anonymous or private subclasses
+ * are not recognized by PropertyUtils - see issue# BEANUTILS-273.
+ * <p />
+ * See https://issues.apache.org/jira/browse/BEANUTILS-273
+ * <p />
+ *
+ * @version $Revision$ $Date$
+ */
+public class Jira273TestCase extends TestCase {
+
+    private Log log = LogFactory.getLog(Jira273TestCase.class);
+
+    /**
+     * Create a test case with the specified name.
+     *
+     * @param name The name of the test
+     */
+    public Jira273TestCase(String name) {
+        super(name);
+    }
+
+    /**
+     * Run the Test.
+     *
+     * @param args Arguments
+     */
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run(suite());
+    }
+
+    /**
+     * Create a test suite for this test.
+     *
+     * @return a test suite
+     */
+    public static Test suite() {
+        return (new TestSuite(Jira273TestCase.class));
+    }
+
+    /**
+     * Set up.
+     *
+     * @throws java.lang.Exception
+     */
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    /**
+     * Tear Down.
+     *
+     * @throws java.lang.Exception
+     */
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    /**
+     * Test with an annonymous class that overrides a public method
+     * of a public class.
+     */
+    public void testIssue_BEANUTILS_273_AnnonymousOverriden() {
+        Object bean = Jira273BeanFactory.createAnnonymousOverriden();
+        Object result = null;
+        try {
+            result = PropertyUtils.getProperty(bean, "beanValue");
+        } catch (Throwable t) {
+            log.error("AnnonymousOverriden: " + t.getMessage(), t);
+            fail("AnnonymousOverriden Threw exception: " + t);
+        }
+        assertEquals("AnnonymousOverriden", result);
+    }
+
+    /**
+     * Test with an annonymous class that inherits a public method
+     * of a public class.
+     */
+    public void testIssue_BEANUTILS_273_AnnonymousNotOverriden() {
+        Object bean = Jira273BeanFactory.createAnnonymousNotOverriden();
+        Object result = null;
+        try {
+            result = PropertyUtils.getProperty(bean, "beanValue");
+        } catch (Throwable t) {
+            log.error("AnnonymousNotOverriden: " + t.getMessage(), t);
+            fail("AnnonymousNotOverriden Threw exception: " + t);
+        }
+        assertEquals("PublicBeanWithMethod", result);
+    }
+
+    /**
+     * Test with an private class that inherits a public method
+     * of a public class.
+     */
+    public void testIssue_BEANUTILS_273_PrivatePublicNotOverriden() {
+        Object bean = Jira273BeanFactory.createPrivatePublicNotOverriden();
+        Object result = null;
+        try {
+            result = PropertyUtils.getProperty(bean, "beanValue");
+        } catch (Throwable t) {
+            log.error("PrivatePublicNotOverriden: " + t.getMessage(), t);
+            fail("PrivatePublicNotOverriden Threw exception: " + t);
+        }
+        assertEquals("PublicBeanWithMethod", result);
+    }
+
+    /**
+     * Test with an private class that overrides a public method
+     * of a public class.
+     */
+    public void testIssue_BEANUTILS_273_PrivatePublicOverriden() {
+        Object bean = Jira273BeanFactory.createPrivatePublicOverriden();
+        Object result = null;
+        try {
+            result = PropertyUtils.getProperty(bean, "beanValue");
+        } catch (Throwable t) {
+            log.error("PrivatePublicOverriden: " + t.getMessage(), t);
+            fail("PrivatePublicOverriden Threw exception: " + t);
+        }
+        assertEquals("PrivatePublicOverriden", result);
+    }
+
+    /**
+     * Test with an private class that inherits a public method
+     * of a "grand parent" public class.
+     */
+    public void testIssue_BEANUTILS_273_PrivatePrivatePublicNotOverriden() {
+        Object bean = 
Jira273BeanFactory.createPrivatePrivatePublicNotOverriden();
+        Object result = null;
+        try {
+            result = PropertyUtils.getProperty(bean, "beanValue");
+        } catch (Throwable t) {
+            log.error("PrivatePrivatePublicNotOverriden: " + t.getMessage(), 
t);
+            fail("PrivatePrivatePublicNotOverriden Threw exception: " + t);
+        }
+        assertEquals("PublicBeanWithMethod", result);
+    }
+
+    /**
+     * Test with an private class that overrides a public method
+     * of a "grand parent" public class.
+     */
+    public void testIssue_BEANUTILS_273_PrivatePrivatePublicOverriden() {
+        Object bean = Jira273BeanFactory.createPrivatePrivatePublicOverriden();
+        Object result = null;
+        try {
+            result = PropertyUtils.getProperty(bean, "beanValue");
+        } catch (Throwable t) {
+            log.error("PrivatePrivatePublicOverriden: " + t.getMessage(), t);
+            fail("PrivatePrivatePublicOverriden Threw exception: " + t);
+        }
+        assertEquals("PrivatePrivatePublicOverriden", result);
+    }
+}

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira273TestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira273TestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira273BeanFactory.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira273BeanFactory.java?view=auto&rev=555172
==============================================================================
--- 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira273BeanFactory.java
 (added)
+++ 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira273BeanFactory.java
 Tue Jul 10 22:18:17 2007
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.beanutils.bugs.other;
+
+import org.apache.commons.beanutils.bugs.Jira273TestCase;
+
+/**
+ * Factory which creates beans for [EMAIL PROTECTED] Jira273TestCase}.
+ *
+ * @version $Revision$ $Date$
+ */
+public class Jira273BeanFactory {
+
+    /**
+     * Factory method which creates annonymous
+     * [EMAIL PROTECTED] PublicBeanWithMethod} with method NOT overriden.
+     *
+     * @return a new annonymous [EMAIL PROTECTED] PublicBeanWithMethod}.
+     */
+    public static Object createAnnonymousOverriden() {
+        return new PublicBeanWithMethod() {
+            public String getBeanValue() {
+                return "AnnonymousOverriden";
+            }
+        };
+    }
+
+    /**
+     * Factory method which creates annonymous
+     * [EMAIL PROTECTED] PublicBeanWithMethod} with method overriden.
+     *
+     * @return a new annonymous [EMAIL PROTECTED] PublicBeanWithMethod}.
+     */
+    public static Object createAnnonymousNotOverriden() {
+        return new PublicBeanWithMethod() {
+        };
+    }
+
+    /**
+     * Factory method which creates [EMAIL PROTECTED] PrivatePublicOverriden}.
+     *
+     * @return a new [EMAIL PROTECTED] PrivatePublicOverriden}.
+     */
+    public static Object createPrivatePublicOverriden() {
+        return new PrivatePublicOverriden();
+    }
+
+    /**
+     * Factory method which creates [EMAIL PROTECTED] 
PrivatePrivatePublicOverriden}.
+     *
+     * @return a new [EMAIL PROTECTED] PrivatePrivatePublicOverriden}.
+     */
+    public static Object createPrivatePrivatePublicOverriden() {
+        return new PrivatePrivatePublicOverriden();
+    }
+
+    /**
+     * Factory method which creates [EMAIL PROTECTED] 
PrivatePrivatePublicNotOverriden}.
+     *
+     * @return a new [EMAIL PROTECTED] PrivatePrivatePublicNotOverriden}.
+     */
+    public static Object createPrivatePrivatePublicNotOverriden() {
+        return new PrivatePrivatePublicNotOverriden();
+    }
+
+    /**
+     * Factory method which creates [EMAIL PROTECTED] 
PrivatePublicNotOverriden}.
+     *
+     * @return a new [EMAIL PROTECTED] PrivatePublicNotOverriden}.
+     */
+    public static Object createPrivatePublicNotOverriden() {
+        return new PrivatePublicNotOverriden();
+    }
+
+    private static class PrivateBeanWithMethod {
+        public String getBeanValue() {
+            return "PrivateBeanWithMethod";
+        }
+    }
+
+    public static class PublicBeanWithMethod {
+        public String getBeanValue() {
+            return "PublicBeanWithMethod";
+        }
+    }
+    private static class PrivatePublicOverriden extends PublicBeanWithMethod {
+        public String getBeanValue() {
+            return "PrivatePublicOverriden";
+        }
+    }
+    private static class PrivatePublicNotOverriden extends 
PublicBeanWithMethod {
+    }
+    private static class PrivatePrivatePublicOverriden extends 
PrivatePublicNotOverriden {
+        public String getBeanValue() {
+            return "PrivatePrivatePublicOverriden";
+        }
+    }
+    private static class PrivatePrivatePublicNotOverriden extends 
PrivatePublicNotOverriden {
+    }
+}

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira273BeanFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira273BeanFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to