DATACMNS-356 - Parameters now uses DefaultParameterNameDiscoverer if present.

We now leniently try to lookup DefaultParameterNameDiscoverer and create an instance of it to make use of Spring 4's improved parameter name capabilities on Java 8. We fall back on a LocalVariableTableParameterNameDiscoverer if on Spring versions prior to 4.
This commit is contained in:
Oliver Gierke
2013-08-07 14:39:37 +02:00
parent 04d6859354
commit 541ef648f1
2 changed files with 25 additions and 3 deletions

View File

@@ -28,6 +28,7 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.util.Assert;
/**
@@ -44,12 +45,12 @@ 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(
"org.springframework.core.DefaultParameterNameDiscoverer", new LocalVariableTableParameterNameDiscoverer());
private final int pageableIndex;
private final int sortIndex;
private final List<T> parameters;
private final ParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
/**
* Creates a new instance of {@link Parameters}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-2013 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.
@@ -18,8 +18,10 @@ package org.springframework.data.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils.FieldFilter;
/**
@@ -30,6 +32,25 @@ import org.springframework.util.ReflectionUtils.FieldFilter;
*/
public class ReflectionUtils {
/**
* Creates an instance of the class with the given fully qualified name or returns the given default instance if the
* class cannot be loaded or instantiated.
*
* @param classname the fully qualified class name to create an instance for.
* @param defaultInstance the instance to fall back to in case the given class cannot be loaded or instantiated.
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T createInstanceIfPresent(String classname, T defaultInstance) {
try {
Class<?> type = ClassUtils.getDefaultClassLoader().loadClass(classname);
return (T) BeanUtils.instantiateClass(type);
} catch (Exception e) {
return defaultInstance;
}
}
/**
* A {@link FieldFilter} that has a description.
*