From 8fdc7d4e8f730b37544ab3560c0f43716ac7a379 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 15 Oct 2013 15:21:47 +0200 Subject: [PATCH] Polish type prediction for generic factory methods - Now avoiding NullPointerExceptions in GenericTypeResolver's resolveReturnTypeForGenericMethod() in case the supplied ClassLoader is null. - AutowireUtils.resolveReturnTypeForFactoryMethod() now properly asserts that the supplied ClassLoader is not null. - Fixed copy-n-paste errors in Javadoc for AutowireUtils.resolveReturnTypeForFactoryMethod(). Issue: SPR-10411 --- .../beans/factory/support/AutowireUtils.java | 10 ++++++---- .../org/springframework/core/GenericTypeResolver.java | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java index d2d7577640..7357b4fded 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AutowireUtils.java @@ -36,8 +36,8 @@ import org.springframework.util.Assert; import org.springframework.util.ClassUtils; /** - * Utility class that contains various methods useful for - * the implementation of autowire-capable bean factories. + * Utility class that contains various methods useful for the implementation of + * autowire-capable bean factories. * * @author Juergen Hoeller * @author Mark Fisher @@ -161,9 +161,9 @@ abstract class AutowireUtils { * generic factory method, where formal type variables are declared * on the given method itself. *

For example, given a factory method with the following signature, - * if {@code resolveReturnTypeForGenericMethod()} is invoked with the reflected + * if {@code resolveReturnTypeForFactoryMethod()} is invoked with the reflected * method for {@code creatProxy()} and an {@code Object[]} array containing - * {@code MyService.class}, {@code resolveReturnTypeForGenericMethod()} will + * {@code MyService.class}, {@code resolveReturnTypeForFactoryMethod()} will * infer that the target return type is {@code MyService}. *

{@code public static  T createProxy(Class clazz)}
*

Possible Return Values

@@ -185,10 +185,12 @@ abstract class AutowireUtils { * @param classLoader the ClassLoader to resolve class names against, if necessary * (never {@code null}) * @return the resolved target return type, the standard return type, or {@code null} + * @since 3.2.5 */ public static Class resolveReturnTypeForFactoryMethod(Method method, Object[] args, ClassLoader classLoader) { Assert.notNull(method, "Method must not be null"); Assert.notNull(args, "Argument array must not be null"); + Assert.notNull(classLoader, "ClassLoader must not be null"); TypeVariable[] declaredTypeVariables = method.getTypeParameters(); Type genericReturnType = method.getGenericReturnType(); diff --git a/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java b/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java index 0a35b2bdba..865ac36036 100644 --- a/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java +++ b/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java @@ -164,7 +164,7 @@ public abstract class GenericTypeResolver { if (arg instanceof Class) { return (Class) arg; } - else if (arg instanceof String) { + else if (arg instanceof String && classLoader != null) { try { return classLoader.loadClass((String) arg); }