DATACMNS-506 - PreferredConstructorDiscoverer now uses Spring 4's ParameterNameDiscoverer if present.

We now also reflectively check for the presence of DefaultParameterNameDiscoverer. This allows the usage of the new reflection methods introduced in Java 8 if the user code is running on Spring 4.

Changed the name of the constant for the ParameterNameDiscoverer in Parameters to meet conventions for constants.
This commit is contained in:
Oliver Gierke
2014-05-19 08:47:32 +02:00
parent a1c67a5265
commit 8164426981
2 changed files with 11 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2012 by the original author(s).
* Copyright 2011-2014 by the original author(s).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@ package org.springframework.data.mapping.model;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.List;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
@@ -26,7 +27,9 @@ import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PreferredConstructor;
import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.CollectionUtils;
/**
* Helper class to find a {@link PreferredConstructor}.
@@ -35,7 +38,8 @@ import org.springframework.data.util.TypeInformation;
*/
public class PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>> {
private final ParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
private static final ParameterNameDiscoverer PARAMETER_DISCOVERER = ReflectionUtils.createInstanceIfPresent(
"org.springframework.core.DefaultParameterNameDiscoverer", new LocalVariableTableParameterNameDiscoverer());
private PreferredConstructor<T, P> constructor;
@@ -106,7 +110,8 @@ public class PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>>
return new PreferredConstructor<T, P>((Constructor<T>) constructor);
}
String[] parameterNames = nameDiscoverer.getParameterNames(constructor);
String[] parameterNames = PARAMETER_DISCOVERER.getParameterNames(constructor);
Parameter<Object, P>[] parameters = new Parameter[parameterTypes.size()];
Annotation[][] parameterAnnotations = constructor.getParameterAnnotations();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2013 the original author or authors.
* Copyright 2008-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@ public abstract class Parameters<S extends Parameters<S, T>, T extends Parameter
private static final String ALL_OR_NOTHING = String.format("Either use @%s "
+ "on all parameters except %s and %s typed once, or none at all!", Param.class.getSimpleName(),
Pageable.class.getSimpleName(), Sort.class.getSimpleName());
private static final ParameterNameDiscoverer discoverer = ReflectionUtils.createInstanceIfPresent(
private static final ParameterNameDiscoverer PARAMETER_DISCOVERER = ReflectionUtils.createInstanceIfPresent(
"org.springframework.core.DefaultParameterNameDiscoverer", new LocalVariableTableParameterNameDiscoverer());
private final int pageableIndex;
@@ -68,7 +68,7 @@ public abstract class Parameters<S extends Parameters<S, T>, T extends Parameter
for (int i = 0; i < types.size(); i++) {
MethodParameter methodParameter = new MethodParameter(method, i);
methodParameter.initParameterNameDiscovery(discoverer);
methodParameter.initParameterNameDiscovery(PARAMETER_DISCOVERER);
T parameter = createParameter(methodParameter);