From 6df464b31c8711dd3c317ca2be0e2e89c59dc22d Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 19 May 2014 08:47:32 +0200 Subject: [PATCH] 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. --- .../mapping/model/PreferredConstructorDiscoverer.java | 11 ++++++++--- .../data/repository/query/Parameters.java | 6 +++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java b/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java index 4c242d03d..4648733b3 100644 --- a/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java +++ b/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java @@ -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> { - private final ParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); + private static final ParameterNameDiscoverer PARAMETER_DISCOVERER = ReflectionUtils.createInstanceIfPresent( + "org.springframework.core.DefaultParameterNameDiscoverer", new LocalVariableTableParameterNameDiscoverer()); private PreferredConstructor constructor; @@ -106,7 +110,8 @@ public class PreferredConstructorDiscoverer> return new PreferredConstructor((Constructor) constructor); } - String[] parameterNames = nameDiscoverer.getParameterNames(constructor); + String[] parameterNames = PARAMETER_DISCOVERER.getParameterNames(constructor); + Parameter[] parameters = new Parameter[parameterTypes.size()]; Annotation[][] parameterAnnotations = constructor.getParameterAnnotations(); diff --git a/src/main/java/org/springframework/data/repository/query/Parameters.java b/src/main/java/org/springframework/data/repository/query/Parameters.java index 3c8172e65..24340cd55 100644 --- a/src/main/java/org/springframework/data/repository/query/Parameters.java +++ b/src/main/java/org/springframework/data/repository/query/Parameters.java @@ -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, 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, 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);