Browse Source

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.
1.7.x
Oliver Gierke 12 years ago
parent
commit
6df464b31c
  1. 11
      src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java
  2. 6
      src/main/java/org/springframework/data/repository/query/Parameters.java

11
src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java

@ -1,5 +1,5 @@ @@ -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; @@ -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; @@ -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; @@ -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>> @@ -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();

6
src/main/java/org/springframework/data/repository/query/Parameters.java

@ -1,5 +1,5 @@ @@ -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 @@ -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 @@ -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);

Loading…
Cancel
Save