ObjectToObject converter now only matches public methods/constructors; private class method invocations also supported now through a makeAccessible call

This commit is contained in:
Keith Donald 2009-12-15 15:53:11 +00:00
parent b64945988b
commit 5c7c56a6b3
3 changed files with 8 additions and 7 deletions

View File

@ -60,6 +60,7 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter {
Object target; Object target;
Method method = getValueOfMethodOn(targetClass, sourceClass); Method method = getValueOfMethodOn(targetClass, sourceClass);
if (method != null) { if (method != null) {
ReflectionUtils.makeAccessible(method);
target = ReflectionUtils.invokeMethod(method, null, source); target = ReflectionUtils.invokeMethod(method, null, source);
} }
else { else {

View File

@ -558,7 +558,7 @@ public abstract class ClassUtils {
/** /**
* Determine whether the given class has a constructor with the given signature. * Determine whether the given class has a public constructor with the given signature.
* <p>Essentially translates <code>NoSuchMethodException</code> to "false". * <p>Essentially translates <code>NoSuchMethodException</code> to "false".
* @param clazz the clazz to analyze * @param clazz the clazz to analyze
* @param paramTypes the parameter types of the method * @param paramTypes the parameter types of the method
@ -570,7 +570,7 @@ public abstract class ClassUtils {
} }
/** /**
* Determine whether the given class has a constructor with the given signature, * Determine whether the given class has a public constructor with the given signature,
* and return it if available (else return <code>null</code>). * and return it if available (else return <code>null</code>).
* <p>Essentially translates <code>NoSuchMethodException</code> to <code>null</code>. * <p>Essentially translates <code>NoSuchMethodException</code> to <code>null</code>.
* @param clazz the clazz to analyze * @param clazz the clazz to analyze
@ -717,7 +717,7 @@ public abstract class ClassUtils {
} }
/** /**
* Return a static method of a class. * Return a public static method of a class.
* @param methodName the static method name * @param methodName the static method name
* @param clazz the class which defines the method * @param clazz the class which defines the method
* @param args the parameter types to the method * @param args the parameter types to the method
@ -728,8 +728,8 @@ public abstract class ClassUtils {
Assert.notNull(clazz, "Class must not be null"); Assert.notNull(clazz, "Class must not be null");
Assert.notNull(methodName, "Method name must not be null"); Assert.notNull(methodName, "Method name must not be null");
try { try {
Method method = clazz.getDeclaredMethod(methodName, args); Method method = clazz.getMethod(methodName, args);
return ((method.getModifiers() & Modifier.STATIC) != 0 ? method : null); return Modifier.isStatic(method.getModifiers()) ? method : null;
} }
catch (NoSuchMethodException ex) { catch (NoSuchMethodException ex) {
return null; return null;

View File

@ -597,8 +597,7 @@ public class DefaultConversionTests {
} }
@Test @Test
@Ignore public void convertObjectToObjectValueOfMethod() {
public void convertObjectToObjectValueOFMethod() {
assertEquals(ISBN.valueOf("123456789"), conversionService.convert("123456789", ISBN.class)); assertEquals(ISBN.valueOf("123456789"), conversionService.convert("123456789", ISBN.class));
} }
@ -614,6 +613,7 @@ public class DefaultConversionTests {
} }
private static class SSN { private static class SSN {
private String value; private String value;
public SSN(String value) { public SSN(String value) {