#269 - Polishing.

Simplified slash removal and made sure slashes occuring multiple times are removed properly.

Original pull request: #423.
This commit is contained in:
Oliver Gierke
2016-01-12 09:49:02 +01:00
parent 720ff797de
commit e1afab94de
2 changed files with 39 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2016 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.
@@ -19,6 +19,7 @@ import static org.springframework.core.annotation.AnnotationUtils.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.regex.Pattern;
import org.springframework.util.Assert;
@@ -29,6 +30,8 @@ import org.springframework.util.Assert;
*/
public class AnnotationMappingDiscoverer implements MappingDiscoverer {
private static final Pattern MULTIPLE_SLASHES = Pattern.compile("\\/{2,}");
private final Class<? extends Annotation> annotationType;
private final String mappingAttributeName;
@@ -68,8 +71,8 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
String[] mapping = getMappingFrom(findAnnotation(type, annotationType));
if (mapping.length > 1) {
throw new IllegalStateException(String.format("Multiple class level mappings defined on class %s!",
type.getName()));
throw new IllegalStateException(
String.format("Multiple class level mappings defined on class %s!", type.getName()));
}
return mapping.length == 0 ? null : mapping[0];
@@ -99,8 +102,8 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
String[] mapping = getMappingFrom(findAnnotation(method, annotationType));
if (mapping.length > 1) {
throw new IllegalStateException(String.format("Multiple method level mappings defined on method %s!",
method.toString()));
throw new IllegalStateException(
String.format("Multiple method level mappings defined on method %s!", method.toString()));
}
String typeMapping = getMapping(type);
@@ -112,16 +115,6 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
return typeMapping == null || "/".equals(typeMapping) ? mapping[0] : join(typeMapping, mapping[0]);
}
private String join(String typeMapping, String mapping) {
StringBuilder builder = new StringBuilder();
builder.append(typeMapping.endsWith("/") ? typeMapping.substring(0, typeMapping.length() - 1) : typeMapping);
builder.append('/');
builder.append(mapping.startsWith("/") ? mapping.substring(1) : mapping);
return builder.toString();
}
private String[] getMappingFrom(Annotation annotation) {
Object value = mappingAttributeName == null ? getValue(annotation) : getValue(annotation, mappingAttributeName);
@@ -137,4 +130,15 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
throw new IllegalStateException(String.format(
"Unsupported type for the mapping attribute! Support String and String[] but got %s!", value.getClass()));
}
/**
* Joins the given mappings making sure exactly one slash.
*
* @param typeMapping must not be {@literal null} or empty.
* @param mapping must not be {@literal null} or empty.
* @return
*/
private static String join(String typeMapping, String mapping) {
return MULTIPLE_SLASHES.matcher(typeMapping.concat("/").concat(mapping)).replaceAll("/");
}
}