From 5c7c56a6b3ce5405750f30d9f205425e2352274e Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Tue, 15 Dec 2009 15:53:11 +0000 Subject: [PATCH] ObjectToObject converter now only matches public methods/constructors; private class method invocations also supported now through a makeAccessible call --- .../core/convert/support/ObjectToObjectConverter.java | 1 + .../main/java/org/springframework/util/ClassUtils.java | 10 +++++----- .../core/convert/support/DefaultConversionTests.java | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java index 762b03daae..df540db8e6 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java @@ -60,6 +60,7 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter { Object target; Method method = getValueOfMethodOn(targetClass, sourceClass); if (method != null) { + ReflectionUtils.makeAccessible(method); target = ReflectionUtils.invokeMethod(method, null, source); } else { diff --git a/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java b/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java index b0525af65f..cd2f26732a 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java @@ -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. *

Essentially translates NoSuchMethodException to "false". * @param clazz the clazz to analyze * @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 null). *

Essentially translates NoSuchMethodException to null. * @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 clazz the class which defines 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(methodName, "Method name must not be null"); try { - Method method = clazz.getDeclaredMethod(methodName, args); - return ((method.getModifiers() & Modifier.STATIC) != 0 ? method : null); + Method method = clazz.getMethod(methodName, args); + return Modifier.isStatic(method.getModifiers()) ? method : null; } catch (NoSuchMethodException ex) { return null; diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java index 3b0f197f92..283143d2b7 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java @@ -597,8 +597,7 @@ public class DefaultConversionTests { } @Test - @Ignore - public void convertObjectToObjectValueOFMethod() { + public void convertObjectToObjectValueOfMethod() { assertEquals(ISBN.valueOf("123456789"), conversionService.convert("123456789", ISBN.class)); } @@ -614,6 +613,7 @@ public class DefaultConversionTests { } private static class SSN { + private String value; public SSN(String value) {