diff --git a/src/main/java/org/springframework/hateoas/core/MethodParameters.java b/src/main/java/org/springframework/hateoas/core/MethodParameters.java index adfbb546..87f92784 100644 --- a/src/main/java/org/springframework/hateoas/core/MethodParameters.java +++ b/src/main/java/org/springframework/hateoas/core/MethodParameters.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-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. @@ -20,10 +20,12 @@ import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; +import org.springframework.beans.BeanUtils; import org.springframework.core.LocalVariableTableParameterNameDiscoverer; import org.springframework.core.MethodParameter; import org.springframework.core.ParameterNameDiscoverer; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; /** * Value object to represent {@link MethodParameters} to allow to easily find the ones with a given annotation. @@ -32,7 +34,20 @@ import org.springframework.util.Assert; */ public class MethodParameters { - private static final ParameterNameDiscoverer DISCOVERER = new LocalVariableTableParameterNameDiscoverer(); + private static final String SPRING_4_DISCOVERER_NAME = "org.springframework.core.DefaultParameterNameDiscoverer"; + private static ParameterNameDiscoverer DISCOVERER; + + static { + + ClassLoader classLoader = MethodParameters.class.getClassLoader(); + + try { + Class discovererType = ClassUtils.forName(SPRING_4_DISCOVERER_NAME, classLoader); + DISCOVERER = (ParameterNameDiscoverer) BeanUtils.instantiate(discovererType); + } catch (ClassNotFoundException o_O) { + DISCOVERER = new LocalVariableTableParameterNameDiscoverer(); + } + } private final List parameters; @@ -93,6 +108,27 @@ public class MethodParameters { return null; } + /** + * Returns all parameters of the given type. + * + * @param type must not be {@literal null}. + * @return + * @since 0.9 + */ + public List getParametersOfType(Class type) { + + Assert.notNull(type, "Type must not be null!"); + List result = new ArrayList(); + + for (MethodParameter parameter : getParameters()) { + if (parameter.getParameterType().equals(type)) { + result.add(parameter); + } + } + + return result; + } + /** * Returns all {@link MethodParameter}s annotated with the given annotation type. * diff --git a/src/test/java/org/springframework/hateoas/core/MethodParametersUnitTests.java b/src/test/java/org/springframework/hateoas/core/MethodParametersUnitTests.java index e528b319..e7d1664c 100644 --- a/src/test/java/org/springframework/hateoas/core/MethodParametersUnitTests.java +++ b/src/test/java/org/springframework/hateoas/core/MethodParametersUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-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. @@ -15,13 +15,15 @@ */ package org.springframework.hateoas.core; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import java.lang.reflect.Method; +import java.util.List; import org.junit.Test; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.MethodParameter; /** * Unit tests for {@link MethodParameters}. @@ -33,7 +35,7 @@ public class MethodParametersUnitTests { @Test public void prefersAnnotatedParameterOverDiscovered() throws Exception { - Method method = Sample.class.getMethod("method", String.class, String.class); + Method method = Sample.class.getMethod("method", String.class, String.class, Object.class); MethodParameters parameters = new MethodParameters(method, new AnnotationAttribute(Qualifier.class)); assertThat(parameters.getParameter("param"), is(notNullValue())); @@ -41,9 +43,23 @@ public class MethodParametersUnitTests { assertThat(parameters.getParameter("another"), is(nullValue())); } + /** + * @see #138 + */ + @Test + public void returnsParametersOfAGivenType() throws Exception { + + Method method = Sample.class.getMethod("method", String.class, String.class, Object.class); + MethodParameters methodParameters = new MethodParameters(method); + + List objectParameters = methodParameters.getParametersOfType(Object.class); + assertThat(objectParameters, hasSize(1)); + assertThat(objectParameters.get(0).getParameterIndex(), is(2)); + } + static class Sample { - public void method(String param, @Qualifier("foo") String another) {} + public void method(String param, @Qualifier("foo") String another, Object object) {} } }