#114 - Support for type mappings on sub-types in ControllerLinkBuilder.
When building links for method invocations we previously only inspected the type that declared the method for type level mappings. We now use the type the method was invoked on explicitly.
This commit is contained in:
@@ -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.
|
||||
@@ -63,6 +63,8 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
|
||||
@Override
|
||||
public String getMapping(Class<?> type) {
|
||||
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
|
||||
String[] mapping = getMappingFrom(findAnnotation(type, annotationType));
|
||||
|
||||
if (mapping.length > 1) {
|
||||
@@ -80,6 +82,20 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
|
||||
@Override
|
||||
public String getMapping(Method method) {
|
||||
|
||||
Assert.notNull(method, "Method must not be null!");
|
||||
return getMapping(method.getDeclaringClass(), method);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.Class, java.lang.reflect.Method)
|
||||
*/
|
||||
@Override
|
||||
public String getMapping(Class<?> type, Method method) {
|
||||
|
||||
Assert.notNull(type, "Type must not be null!");
|
||||
Assert.notNull(method, "Method must not be null!");
|
||||
|
||||
String[] mapping = getMappingFrom(findAnnotation(method, annotationType));
|
||||
|
||||
if (mapping.length > 1) {
|
||||
@@ -87,7 +103,7 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
|
||||
method.toString()));
|
||||
}
|
||||
|
||||
String typeMapping = getMapping(method.getDeclaringClass());
|
||||
String typeMapping = getMapping(type);
|
||||
|
||||
if (mapping == null || mapping.length == 0) {
|
||||
return typeMapping;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
@@ -58,6 +58,7 @@ public class DummyInvocationUtils {
|
||||
private static final Method GET_INVOCATIONS;
|
||||
private static final Method GET_OBJECT_PARAMETERS;
|
||||
|
||||
private final Class<?> targetType;
|
||||
private final Object[] objectParameters;
|
||||
private MethodInvocation invocation;
|
||||
|
||||
@@ -72,7 +73,9 @@ public class DummyInvocationUtils {
|
||||
*
|
||||
* @param parameters
|
||||
*/
|
||||
public InvocationRecordingMethodInterceptor(Object... parameters) {
|
||||
public InvocationRecordingMethodInterceptor(Class<?> targetType, Object... parameters) {
|
||||
|
||||
this.targetType = targetType;
|
||||
this.objectParameters = parameters.clone();
|
||||
}
|
||||
|
||||
@@ -90,7 +93,7 @@ public class DummyInvocationUtils {
|
||||
return ReflectionUtils.invokeMethod(method, obj, args);
|
||||
}
|
||||
|
||||
this.invocation = new SimpleMethodInvocation(method, args);
|
||||
this.invocation = new SimpleMethodInvocation(targetType, method, args);
|
||||
|
||||
Class<?> returnType = method.getReturnType();
|
||||
return returnType.cast(getProxyWithInterceptor(returnType, this));
|
||||
@@ -140,7 +143,7 @@ public class DummyInvocationUtils {
|
||||
|
||||
Assert.notNull(type, "Given type must not be null!");
|
||||
|
||||
InvocationRecordingMethodInterceptor interceptor = new InvocationRecordingMethodInterceptor(parameters);
|
||||
InvocationRecordingMethodInterceptor interceptor = new InvocationRecordingMethodInterceptor(type, parameters);
|
||||
return getProxyWithInterceptor(type, interceptor);
|
||||
}
|
||||
|
||||
@@ -172,10 +175,13 @@ public class DummyInvocationUtils {
|
||||
Object[] getArguments();
|
||||
|
||||
Method getMethod();
|
||||
|
||||
Class<?> getTargetType();
|
||||
}
|
||||
|
||||
static class SimpleMethodInvocation implements MethodInvocation {
|
||||
|
||||
private final Class<?> targetType;
|
||||
private final Method method;
|
||||
private final Object[] arguments;
|
||||
|
||||
@@ -185,11 +191,22 @@ public class DummyInvocationUtils {
|
||||
* @param method
|
||||
* @param arguments
|
||||
*/
|
||||
private SimpleMethodInvocation(Method method, Object[] arguments) {
|
||||
private SimpleMethodInvocation(Class<?> targetType, Method method, Object[] arguments) {
|
||||
|
||||
this.targetType = targetType;
|
||||
this.arguments = arguments;
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.core.DummyInvocationUtils.MethodInvocation#getTargetType()
|
||||
*/
|
||||
@Override
|
||||
public Class<?> getTargetType() {
|
||||
return targetType;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.core.DummyInvocationUtils.MethodInvocation#getArguments()
|
||||
|
||||
@@ -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.
|
||||
@@ -39,4 +39,14 @@ public interface MappingDiscoverer {
|
||||
* @return the method mapping including the type-level one or {@literal null} if neither of them present.
|
||||
*/
|
||||
String getMapping(Method method);
|
||||
|
||||
/**
|
||||
* Returns the mapping for the given {@link Method} invoked on the given type. This can be used to calculate the
|
||||
* mapping for a super type method being invoked on a sub-type with a type mapping.
|
||||
*
|
||||
* @param type must not be {@literal null}.
|
||||
* @param method must not be {@literal null}.
|
||||
* @return the method mapping including the type-level one or {@literal null} if neither of them present.
|
||||
*/
|
||||
String getMapping(Class<?> type, Method method);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.
|
||||
@@ -103,7 +103,7 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
|
||||
Iterator<Object> classMappingParameters = invocations.getObjectParameters();
|
||||
Method method = invocation.getMethod();
|
||||
|
||||
String mapping = DISCOVERER.getMapping(method);
|
||||
String mapping = DISCOVERER.getMapping(invocation.getTargetType(), method);
|
||||
UriComponentsBuilder builder = ControllerLinkBuilder.getBuilder().path(mapping);
|
||||
|
||||
UriTemplate template = new UriTemplate(mapping);
|
||||
|
||||
Reference in New Issue
Block a user