Skip to content

Commit 5ea48dd

Browse files
committed
#361 - Rearrange PropertyResolvingMappingDiscoverer.
Moved PropertyResolvingMappingDiscoverer into server.core package. Unified MappingDiscoverer arrangement in WebHandler and use it from there. Deprecated AnnotationBasedMappingDiscoverer to be able to make it package protected in 1.3.
1 parent ffa1dc3 commit 5ea48dd

File tree

5 files changed

+46
-54
lines changed

5 files changed

+46
-54
lines changed

src/main/java/org/springframework/hateoas/server/core/AnnotationMappingDiscoverer.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333

3434
/**
3535
* {@link MappingDiscoverer} implementation that inspects mappings from a particular annotation.
36-
*
36+
*
3737
* @author Oliver Gierke
3838
* @author Mark Paluch
3939
* @author Greg Turnquist
40+
* @deprecated since 1.2, not for removal but for hiding within the package in 1.3
4041
*/
42+
@Deprecated
4143
public class AnnotationMappingDiscoverer implements MappingDiscoverer {
4244

4345
private static final Pattern MULTIPLE_SLASHES = Pattern.compile("/{2,}");
@@ -48,7 +50,7 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
4850
/**
4951
* Creates an {@link AnnotationMappingDiscoverer} for the given annotation type. Will lookup the {@code value}
5052
* attribute by default.
51-
*
53+
*
5254
* @param annotation must not be {@literal null}.
5355
*/
5456
public AnnotationMappingDiscoverer(Class<? extends Annotation> annotation) {
@@ -57,7 +59,7 @@ public AnnotationMappingDiscoverer(Class<? extends Annotation> annotation) {
5759

5860
/**
5961
* Creates an {@link AnnotationMappingDiscoverer} for the given annotation type and attribute name.
60-
*
62+
*
6163
* @param annotation must not be {@literal null}.
6264
* @param mappingAttributeName if {@literal null}, it defaults to {@code value}.
6365
*/
@@ -69,7 +71,7 @@ public AnnotationMappingDiscoverer(Class<? extends Annotation> annotation, @Null
6971
this.mappingAttributeName = mappingAttributeName;
7072
}
7173

72-
/*
74+
/*
7375
* (non-Javadoc)
7476
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.Class)
7577
*/
@@ -84,7 +86,7 @@ public String getMapping(Class<?> type) {
8486
return mapping.length == 0 ? null : mapping[0];
8587
}
8688

87-
/*
89+
/*
8890
* (non-Javadoc)
8991
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.reflect.Method)
9092
*/
@@ -96,7 +98,7 @@ public String getMapping(Method method) {
9698
return getMapping(method.getDeclaringClass(), method);
9799
}
98100

99-
/*
101+
/*
100102
* (non-Javadoc)
101103
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.Class, java.lang.reflect.Method)
102104
*/
@@ -120,7 +122,7 @@ public String getMapping(Class<?> type, Method method) {
120122
/**
121123
* Extract {@link org.springframework.web.bind.annotation.RequestMapping}'s list of {@link RequestMethod}s into an
122124
* array of {@link String}s.
123-
*
125+
*
124126
* @param type
125127
* @param method
126128
* @return
@@ -171,7 +173,7 @@ private String[] getMappingFrom(@Nullable Annotation annotation) {
171173

172174
/**
173175
* Joins the given mappings making sure exactly one slash.
174-
*
176+
*
175177
* @param typeMapping must not be {@literal null} or empty.
176178
* @param mapping must not be {@literal null} or empty.
177179
* @return

src/main/java/org/springframework/hateoas/server/mvc/PropertyResolvingMappingDiscoverer.java renamed to src/main/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscoverer.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.hateoas.server.mvc;
16+
package org.springframework.hateoas.server.core;
1717

1818
import java.lang.reflect.Method;
1919
import java.util.Collection;
2020

21-
import org.springframework.hateoas.server.core.MappingDiscoverer;
2221
import org.springframework.http.HttpMethod;
2322
import org.springframework.lang.Nullable;
2423
import org.springframework.util.Assert;

src/main/java/org/springframework/hateoas/server/core/WebHandler.java

+29-34
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,7 @@
2222

2323
import java.lang.annotation.Annotation;
2424
import java.lang.reflect.Method;
25-
import java.util.ArrayList;
26-
import java.util.Arrays;
27-
import java.util.Collection;
28-
import java.util.Collections;
29-
import java.util.HashMap;
30-
import java.util.Iterator;
31-
import java.util.List;
32-
import java.util.Map;
33-
import java.util.Objects;
34-
import java.util.Optional;
25+
import java.util.*;
3526
import java.util.concurrent.ConcurrentHashMap;
3627
import java.util.function.BiFunction;
3728
import java.util.function.Function;
@@ -68,8 +59,9 @@
6859
*/
6960
public class WebHandler {
7061

71-
private static final MappingDiscoverer DISCOVERER = CachingMappingDiscoverer
72-
.of(new AnnotationMappingDiscoverer(RequestMapping.class));
62+
@SuppressWarnings("deprecation") //
63+
public static final MappingDiscoverer DISCOVERER = CachingMappingDiscoverer
64+
.of(new PropertyResolvingMappingDiscoverer(new AnnotationMappingDiscoverer(RequestMapping.class)));
7365

7466
private static final Map<AffordanceKey, List<Affordance>> AFFORDANCES_CACHE = new ConcurrentReferenceHashMap<>();
7567

@@ -169,7 +161,7 @@ private static <T extends LinkBuilder> PreparedWebHandler<T> linkTo(Object invoc
169161
}
170162

171163
List<Affordance> affordances = AFFORDANCES_CACHE.computeIfAbsent(
172-
AffordanceKey.of(invocation.getTargetType(), invocation.getMethod(), components),
164+
new AffordanceKey(invocation.getTargetType(), invocation.getMethod(), components),
173165
key -> SpringAffordanceBuilder.create(key.type, key.method, key.href.toUriString(), DISCOVERER));
174166

175167
return creator.createBuilder(components, variables, affordances);
@@ -242,46 +234,49 @@ private static final class AffordanceKey {
242234
private final Method method;
243235
private final UriComponents href;
244236

245-
private AffordanceKey(Class<?> type, Method method, UriComponents href) {
237+
AffordanceKey(Class<?> type, Method method, UriComponents href) {
246238

247239
this.type = type;
248240
this.method = method;
249241
this.href = href;
250242
}
251243

252-
public static AffordanceKey of(Class<?> type, Method method, UriComponents href) {
253-
return new AffordanceKey(type, method, href);
254-
}
255-
256-
public Class<?> getType() {
257-
return this.type;
258-
}
259-
260-
public Method getMethod() {
261-
return this.method;
262-
}
263-
264-
public UriComponents getHref() {
265-
return this.href;
266-
}
267-
244+
/*
245+
* (non-Javadoc)
246+
* @see java.lang.Object#equals(java.lang.Object)
247+
*/
268248
@Override
269-
public boolean equals(Object o) {
249+
public boolean equals(@Nullable Object o) {
270250

271-
if (this == o)
251+
if (this == o) {
272252
return true;
273-
if (!(o instanceof AffordanceKey))
253+
}
254+
255+
if (!(o instanceof AffordanceKey)) {
274256
return false;
257+
}
258+
275259
AffordanceKey that = (AffordanceKey) o;
276-
return Objects.equals(this.type, that.type) && Objects.equals(this.method, that.method)
260+
261+
return Objects.equals(this.type, that.type) //
262+
&& Objects.equals(this.method, that.method) //
277263
&& Objects.equals(this.href, that.href);
278264
}
279265

266+
/*
267+
* (non-Javadoc)
268+
* @see java.lang.Object#hashCode()
269+
*/
280270
@Override
281271
public int hashCode() {
282272
return Objects.hash(this.type, this.method, this.href);
283273
}
284274

275+
/*
276+
* (non-Javadoc)
277+
* @see java.lang.Object#toString()
278+
*/
279+
@Override
285280
public String toString() {
286281
return "WebHandler.AffordanceKey(type=" + this.type + ", method=" + this.method + ", href=" + this.href + ")";
287282
}

src/main/java/org/springframework/hateoas/server/mvc/WebMvcLinkBuilder.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@
2424
import org.springframework.hateoas.Affordance;
2525
import org.springframework.hateoas.Link;
2626
import org.springframework.hateoas.TemplateVariables;
27-
import org.springframework.hateoas.server.core.AnnotationMappingDiscoverer;
28-
import org.springframework.hateoas.server.core.CachingMappingDiscoverer;
2927
import org.springframework.hateoas.server.core.DummyInvocationUtils;
30-
import org.springframework.hateoas.server.core.MappingDiscoverer;
3128
import org.springframework.hateoas.server.core.TemplateVariableAwareLinkBuilderSupport;
3229
import org.springframework.hateoas.server.core.UriTemplateFactory;
30+
import org.springframework.hateoas.server.core.WebHandler;
3331
import org.springframework.util.Assert;
34-
import org.springframework.web.bind.annotation.RequestMapping;
3532
import org.springframework.web.util.DefaultUriTemplateHandler;
3633
import org.springframework.web.util.UriComponents;
3734
import org.springframework.web.util.UriComponentsBuilder;
@@ -52,8 +49,6 @@
5249
@SuppressWarnings("deprecation")
5350
public class WebMvcLinkBuilder extends TemplateVariableAwareLinkBuilderSupport<WebMvcLinkBuilder> {
5451

55-
private static final MappingDiscoverer DISCOVERER = CachingMappingDiscoverer
56-
.of(new PropertyResolvingMappingDiscoverer(new AnnotationMappingDiscoverer(RequestMapping.class)));
5752
private static final WebMvcLinkBuilderFactory FACTORY = new WebMvcLinkBuilderFactory();
5853
private static final CustomUriTemplateHandler HANDLER = new CustomUriTemplateHandler();
5954

@@ -94,7 +89,7 @@ public static WebMvcLinkBuilder linkTo(Class<?> controller, Object... parameters
9489
Assert.notNull(controller, "Controller must not be null!");
9590
Assert.notNull(parameters, "Parameters must not be null!");
9691

97-
String mapping = DISCOVERER.getMapping(controller);
92+
String mapping = WebHandler.DISCOVERER.getMapping(controller);
9893

9994
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
10095
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
@@ -116,7 +111,7 @@ public static WebMvcLinkBuilder linkTo(Class<?> controller, Map<String, ?> param
116111
Assert.notNull(controller, "Controller must not be null!");
117112
Assert.notNull(parameters, "Parameters must not be null!");
118113

119-
String mapping = DISCOVERER.getMapping(controller);
114+
String mapping = WebHandler.DISCOVERER.getMapping(controller);
120115

121116
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
122117
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
@@ -139,7 +134,7 @@ public static WebMvcLinkBuilder linkTo(Class<?> controller, Method method, Objec
139134
Assert.notNull(controller, "Controller type must not be null!");
140135
Assert.notNull(method, "Method must not be null!");
141136

142-
String mapping = DISCOVERER.getMapping(controller, method);
137+
String mapping = WebHandler.DISCOVERER.getMapping(controller, method);
143138
UriTemplate template = UriTemplateFactory.templateFor(mapping);
144139
URI uri = template.expand(parameters);
145140

src/test/java/org/springframework/hateoas/server/mvc/PropertyResolvingMappingDiscovererUnitTest.java renamed to src/test/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscovererUnitTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.hateoas.server.mvc;
16+
package org.springframework.hateoas.server.core;
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

@@ -25,6 +25,7 @@
2525
import org.springframework.context.annotation.Configuration;
2626
import org.springframework.hateoas.TestUtils;
2727
import org.springframework.hateoas.server.core.AnnotationMappingDiscoverer;
28+
import org.springframework.hateoas.server.core.PropertyResolvingMappingDiscoverer;
2829
import org.springframework.mock.web.MockServletContext;
2930
import org.springframework.test.context.TestPropertySource;
3031
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;

0 commit comments

Comments
 (0)