You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2008/04/03 05:00:35 UTC
svn commit: r644143 - in /commons/proper/beanutils/trunk/src:
java/org/apache/commons/beanutils/ test/org/apache/commons/beanutils/bugs/
test/org/apache/commons/beanutils/bugs/other/
Author: niallp
Date: Wed Apr 2 20:00:34 2008
New Revision: 644143
URL: http://svn.apache.org/viewvc?rev=644143&view=rev
Log:
BEANUTILS-298 - MethodUtils.getAccessibleMethod(Method method) could not find right public method - reported by Roman Mukhin
Added:
commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira298TestCase.java (with props)
commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira298BeanFactory.java (with props)
Modified:
commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java
commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java
Modified: commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java?rev=644143&r1=644142&r2=644143&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java (original)
+++ commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/BeanUtilsBean.java Wed Apr 2 20:00:34 2008
@@ -507,9 +507,10 @@
} else {
PropertyDescriptor[] descriptors =
getPropertyUtils().getPropertyDescriptors(bean);
+ Class clazz = bean.getClass();
for (int i = 0; i < descriptors.length; i++) {
String name = descriptors[i].getName();
- if (getPropertyUtils().getReadMethod(descriptors[i]) != null) {
+ if (getPropertyUtils().getReadMethod(clazz, descriptors[i]) != null) {
description.put(name, getProperty(bean, name));
}
}
Modified: commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java?rev=644143&r1=644142&r2=644143&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java (original)
+++ commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/MethodUtils.java Wed Apr 2 20:00:34 2008
@@ -717,7 +717,7 @@
}
method = getAccessibleMethod
- (clazz.getMethod(methodName, parameterTypes));
+ (clazz, clazz.getMethod(methodName, parameterTypes));
cacheMethod(md, method);
return method;
} catch (NoSuchMethodException e) {
@@ -742,13 +742,38 @@
return (null);
}
+ return getAccessibleMethod(method.getDeclaringClass(), method);
+
+ }
+
+
+
+ /**
+ * <p>Return an accessible method (that is, one that can be invoked via
+ * reflection) that implements the specified Method. If no such method
+ * can be found, return <code>null</code>.</p>
+ *
+ * @param clazz The class of the object
+ * @param method The method that we wish to call
+ * @return The accessible method
+ */
+ public static Method getAccessibleMethod(Class clazz, Method method) {
+
+ // Make sure we have a method to check
+ if (method == null) {
+ return (null);
+ }
+
// If the requested method is not public we cannot call it
if (!Modifier.isPublic(method.getModifiers())) {
return (null);
}
- // If the declaring class is public, we are done
- Class clazz = method.getDeclaringClass();
+ if (clazz == null) {
+ clazz = method.getDeclaringClass();
+ }
+
+ // If the class is public, we are done
if (Modifier.isPublic(clazz.getModifiers())) {
return (method);
}
@@ -843,7 +868,7 @@
*/
}
if (method != null) {
- break;
+ return method;
}
// Recursively check our parent interfaces
@@ -852,7 +877,7 @@
methodName,
parameterTypes);
if (method != null) {
- break;
+ return method;
}
}
@@ -1007,7 +1032,7 @@
if (match) {
// get accessible version of method
- Method method = getAccessibleMethod(methods[i]);
+ Method method = getAccessibleMethod(clazz, methods[i]);
if (method != null) {
if (log.isTraceEnabled()) {
log.trace(method + " accessible version of "
Modified: commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java?rev=644143&r1=644142&r2=644143&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java (original)
+++ commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java Wed Apr 2 20:00:34 2008
@@ -475,7 +475,7 @@
if (descriptor instanceof IndexedPropertyDescriptor) {
Method readMethod = ((IndexedPropertyDescriptor) descriptor).
getIndexedReadMethod();
- readMethod = MethodUtils.getAccessibleMethod(readMethod);
+ readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod);
if (readMethod != null) {
Object[] subscript = new Object[1];
subscript[0] = new Integer(index);
@@ -494,7 +494,7 @@
}
// Otherwise, the underlying property must be an array
- Method readMethod = getReadMethod(descriptor);
+ Method readMethod = getReadMethod(bean.getClass(), descriptor);
if (readMethod == null) {
throw new NoSuchMethodException("Property '" + name + "' has no " +
"getter method on bean class '" + bean.getClass() + "'");
@@ -629,7 +629,7 @@
// Call the keyed getter method if there is one
Method readMethod = ((MappedPropertyDescriptor) descriptor).
getMappedReadMethod();
- readMethod = MethodUtils.getAccessibleMethod(readMethod);
+ readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod);
if (readMethod != null) {
Object[] keyArray = new Object[1];
keyArray[0] = key;
@@ -641,7 +641,7 @@
}
} else {
/* means that the result has to be retrieved from a map */
- Method readMethod = getReadMethod(descriptor);
+ Method readMethod = getReadMethod(bean.getClass(), descriptor);
if (readMethod != null) {
Object invokeResult = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY);
/* test and fetch from the map */
@@ -1227,6 +1227,21 @@
/**
+ * <p>Return an accessible property getter method for this property,
+ * if there is one; otherwise return <code>null</code>.</p>
+ *
+ * <p><strong>FIXME</strong> - Does not work with DynaBeans.</p>
+ *
+ * @param clazz The class of the read method will be invoked on
+ * @param descriptor Property descriptor to return a getter for
+ * @return The read method
+ */
+ Method getReadMethod(Class clazz, PropertyDescriptor descriptor) {
+ return (MethodUtils.getAccessibleMethod(clazz, descriptor.getReadMethod()));
+ }
+
+
+ /**
* Return the value of the specified simple property of the specified
* bean, with no type conversions.
*
@@ -1291,7 +1306,7 @@
throw new NoSuchMethodException("Unknown property '" +
name + "' on class '" + bean.getClass() + "'" );
}
- Method readMethod = getReadMethod(descriptor);
+ Method readMethod = getReadMethod(bean.getClass(), descriptor);
if (readMethod == null) {
throw new NoSuchMethodException("Property '" + name +
"' has no getter method in class '" + bean.getClass() + "'");
@@ -1321,6 +1336,21 @@
/**
+ * <p>Return an accessible property setter method for this property,
+ * if there is one; otherwise return <code>null</code>.</p>
+ *
+ * <p><strong>FIXME</strong> - Does not work with DynaBeans.</p>
+ *
+ * @param clazz The class of the read method will be invoked on
+ * @param descriptor Property descriptor to return a setter for
+ * @return The write method
+ */
+ Method getWriteMethod(Class clazz, PropertyDescriptor descriptor) {
+ return (MethodUtils.getAccessibleMethod(clazz, descriptor.getWriteMethod()));
+ }
+
+
+ /**
* <p>Return <code>true</code> if the specified property name identifies
* a readable property on the specified bean; otherwise, return
* <code>false</code>.
@@ -1361,14 +1391,14 @@
PropertyDescriptor desc =
getPropertyDescriptor(bean, name);
if (desc != null) {
- Method readMethod = getReadMethod(desc);
+ Method readMethod = getReadMethod(bean.getClass(), desc);
if (readMethod == null) {
if (desc instanceof IndexedPropertyDescriptor) {
readMethod = ((IndexedPropertyDescriptor) desc).getIndexedReadMethod();
} else if (desc instanceof MappedPropertyDescriptor) {
readMethod = ((MappedPropertyDescriptor) desc).getMappedReadMethod();
}
- readMethod = MethodUtils.getAccessibleMethod(readMethod);
+ readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod);
}
return (readMethod != null);
} else {
@@ -1427,14 +1457,14 @@
PropertyDescriptor desc =
getPropertyDescriptor(bean, name);
if (desc != null) {
- Method writeMethod = getWriteMethod(desc);
+ Method writeMethod = getWriteMethod(bean.getClass(), desc);
if (writeMethod == null) {
if (desc instanceof IndexedPropertyDescriptor) {
writeMethod = ((IndexedPropertyDescriptor) desc).getIndexedWriteMethod();
} else if (desc instanceof MappedPropertyDescriptor) {
writeMethod = ((MappedPropertyDescriptor) desc).getMappedWriteMethod();
}
- writeMethod = MethodUtils.getAccessibleMethod(writeMethod);
+ writeMethod = MethodUtils.getAccessibleMethod(bean.getClass(), writeMethod);
}
return (writeMethod != null);
} else {
@@ -1580,7 +1610,7 @@
if (descriptor instanceof IndexedPropertyDescriptor) {
Method writeMethod = ((IndexedPropertyDescriptor) descriptor).
getIndexedWriteMethod();
- writeMethod = MethodUtils.getAccessibleMethod(writeMethod);
+ writeMethod = MethodUtils.getAccessibleMethod(bean.getClass(), writeMethod);
if (writeMethod != null) {
Object[] subscript = new Object[2];
subscript[0] = new Integer(index);
@@ -1610,7 +1640,7 @@
}
// Otherwise, the underlying property must be an array or a list
- Method readMethod = getReadMethod(descriptor);
+ Method readMethod = getReadMethod(bean.getClass(), descriptor);
if (readMethod == null) {
throw new NoSuchMethodException("Property '" + name +
"' has no getter method on bean class '" + bean.getClass() + "'");
@@ -1748,7 +1778,7 @@
Method mappedWriteMethod =
((MappedPropertyDescriptor) descriptor).
getMappedWriteMethod();
- mappedWriteMethod = MethodUtils.getAccessibleMethod(mappedWriteMethod);
+ mappedWriteMethod = MethodUtils.getAccessibleMethod(bean.getClass(), mappedWriteMethod);
if (mappedWriteMethod != null) {
Object[] params = new Object[2];
params[0] = key;
@@ -1769,7 +1799,7 @@
}
} else {
/* means that the result has to be retrieved from a map */
- Method readMethod = getReadMethod(descriptor);
+ Method readMethod = getReadMethod(bean.getClass(), descriptor);
if (readMethod != null) {
Object invokeResult = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY);
/* test and fetch from the map */
@@ -2036,7 +2066,7 @@
throw new NoSuchMethodException("Unknown property '" +
name + "' on class '" + bean.getClass() + "'" );
}
- Method writeMethod = getWriteMethod(descriptor);
+ Method writeMethod = getWriteMethod(bean.getClass(), descriptor);
if (writeMethod == null) {
throw new NoSuchMethodException("Property '" + name +
"' has no setter method in class '" + bean.getClass() + "'");
Added: commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira298TestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira298TestCase.java?rev=644143&view=auto
==============================================================================
--- commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira298TestCase.java (added)
+++ commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira298TestCase.java Wed Apr 2 20:00:34 2008
@@ -0,0 +1,132 @@
+/*
+ * 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 java.lang.reflect.Method;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.commons.beanutils.MethodUtils;
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.beanutils.bugs.other.Jira298BeanFactory;
+import org.apache.commons.beanutils.bugs.other.Jira298BeanFactory.IX;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * See https://issues.apache.org/jira/browse/BEANUTILS-298
+ * <p />
+ *
+ * @version $Revision$ $Date$
+ */
+public class Jira298TestCase extends TestCase {
+
+ private Log log = LogFactory.getLog(Jira298TestCase.class);
+
+ /**
+ * Create a test case with the specified name.
+ *
+ * @param name The name of the test
+ */
+ public Jira298TestCase(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(Jira298TestCase.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 {@link PropertyUtils#getProperty(Object, String)}
+ */
+ public void testIssue_BEANUTILS_298_PropertyUtils_getProperty() {
+ Object bean = Jira298BeanFactory.createImplX();
+ Object result = null;
+ try {
+ result = PropertyUtils.getProperty(bean, "name");
+ } catch (Throwable t) {
+ log.error("Failed: " + t.getMessage(), t);
+ fail("Threw exception: " + t);
+ }
+ assertEquals("BaseX name value", result);
+ }
+
+ /**
+ * Test {@link PropertyUtils#setProperty(Object, String, Object)}
+ */
+ public void testIssue_BEANUTILS_298_PropertyUtils_setProperty() {
+ Object bean = Jira298BeanFactory.createImplX();
+ assertEquals("BaseX name value", ((IX)bean).getName());
+ try {
+ PropertyUtils.setProperty(bean, "name", "new name");
+ } catch (Throwable t) {
+ log.error("Failed: " + t.getMessage(), t);
+ fail("Threw exception: " + t);
+ }
+ assertEquals("new name", ((IX)bean).getName());
+ }
+
+ /**
+ * Test {@link MethodUtils#getAccessibleMethod(Class, Method)}
+ */
+ public void testIssue_BEANUTILS_298_MethodUtils_getAccessibleMethod() {
+ Object bean = Jira298BeanFactory.createImplX();
+ Object result = null;
+ try {
+ Method m2 = MethodUtils.getAccessibleMethod(bean.getClass(), "getName", new Class[0]);
+ result = m2.invoke(bean, null);
+ } catch (Throwable t) {
+ log.error("Failed: " + t.getMessage(), t);
+ fail("Threw exception: " + t);
+ }
+ assertEquals("BaseX name value", result);
+ }
+}
Propchange: commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira298TestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/Jira298TestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added: commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira298BeanFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira298BeanFactory.java?rev=644143&view=auto
==============================================================================
--- commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira298BeanFactory.java (added)
+++ commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira298BeanFactory.java Wed Apr 2 20:00:34 2008
@@ -0,0 +1,54 @@
+/*
+ * 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.Jira298TestCase;
+
+/**
+ * Factory which creates beans for {@link Jira298TestCase}.
+ *
+ * @version $Revision$ $Date$
+ */
+public class Jira298BeanFactory {
+
+ /**
+ * Factory method which creates ImplX.
+ *
+ * @return a new ImplX.
+ */
+ public static IX createImplX() {
+ return new ImplX();
+ }
+
+ public interface IX {
+ public String getName();
+ public void setName(String name);
+ }
+
+ static class BaseX {
+ private String name = "BaseX name value";
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ }
+
+ static class ImplX extends BaseX implements IX {
+ }
+}
Propchange: commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira298BeanFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/bugs/other/Jira298BeanFactory.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL