From 700c3b257f4e2b021f5ac7d59ea5d7b15f9c8671 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 22 Apr 2014 20:48:26 +0200 Subject: [PATCH] Polishing --- .../annotation/AnnotatedElementUtils.java | 14 +++-------- .../core/annotation/AnnotationUtils.java | 25 ++++++++----------- .../AnnotationMetadataReadingVisitor.java | 4 +-- .../type/filter/AssignableTypeFilter.java | 6 ++--- .../org/springframework/util/MimeType.java | 7 +++--- 5 files changed, 23 insertions(+), 33 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java index e67cb62fd6..aa9bdff028 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java @@ -58,7 +58,6 @@ public class AnnotatedElementUtils { public static boolean hasMetaAnnotationTypes(AnnotatedElement element, String annotationType) { return Boolean.TRUE.equals(process(element, annotationType, false, new Processor() { - @Override public Boolean process(Annotation annotation, int metaDepth) { if (metaDepth > 0) { @@ -66,7 +65,6 @@ public class AnnotatedElementUtils { } return null; } - @Override public void postProcess(Annotation annotation, Boolean result) { } @@ -75,12 +73,10 @@ public class AnnotatedElementUtils { public static boolean isAnnotated(AnnotatedElement element, String annotationType) { return Boolean.TRUE.equals(process(element, annotationType, false, new Processor() { - @Override public Boolean process(Annotation annotation, int metaDepth) { return Boolean.TRUE; } - @Override public void postProcess(Annotation annotation, Boolean result) { } @@ -95,12 +91,10 @@ public class AnnotatedElementUtils { final boolean classValuesAsString, final boolean nestedAnnotationsAsMap) { return process(element, annotationType, false, new Processor() { - @Override public AnnotationAttributes process(Annotation annotation, int metaDepth) { return AnnotationUtils.getAnnotationAttributes(annotation, classValuesAsString, nestedAnnotationsAsMap); } - @Override public void postProcess(Annotation annotation, AnnotationAttributes result) { for (String key : result.keySet()) { @@ -125,7 +119,6 @@ public class AnnotatedElementUtils { final MultiValueMap attributes = new LinkedMultiValueMap(); process(element, annotationType, false, new Processor() { - @Override public Void process(Annotation annotation, int metaDepth) { if (annotation.annotationType().getName().equals(annotationType)) { @@ -136,7 +129,6 @@ public class AnnotatedElementUtils { } return null; } - @Override public void postProcess(Annotation annotation, Void result) { for (String key : attributes.keySet()) { @@ -199,8 +191,8 @@ public class AnnotatedElementUtils { Processor processor, Set visited, int metaDepth) { if (visited.add(element)) { - Annotation[] annotations = (traverseClassHierarchy ? element.getDeclaredAnnotations() - : element.getAnnotations()); + Annotation[] annotations = + (traverseClassHierarchy ? element.getDeclaredAnnotations() : element.getAnnotations()); for (Annotation annotation : annotations) { if (annotation.annotationType().getName().equals(annotationType) || metaDepth > 0) { T result = processor.process(annotation, metaDepth); @@ -228,7 +220,7 @@ public class AnnotatedElementUtils { if (traverseClassHierarchy && element instanceof Class) { Class superclass = ((Class) element).getSuperclass(); if (superclass != null && !superclass.equals(Object.class)) { - T result = doProcess(superclass, annotationType, traverseClassHierarchy, processor, visited, + T result = doProcess(superclass, annotationType, true, processor, visited, metaDepth); if (result != null) { return result; diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index 735f5fe616..42e6e8caad 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -137,14 +137,14 @@ public abstract class AnnotationUtils { * @param containerAnnotationType the class of the container that holds the annotations * @param annotationType the annotation class to look for * @return the annotations found - * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method) * @since 4.0 + * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method) */ public static Set getRepeatableAnnotation(Method method, Class containerAnnotationType, Class annotationType) { + Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method); - return getRepeatableAnnotation((AnnotatedElement) resolvedMethod, - containerAnnotationType, annotationType); + return getRepeatableAnnotation((AnnotatedElement) resolvedMethod, containerAnnotationType, annotationType); } /** @@ -156,11 +156,12 @@ public abstract class AnnotationUtils { * @param containerAnnotationType the class of the container that holds the annotations * @param annotationType the annotation class to look for * @return the annotations found - * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method) * @since 4.0 + * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method) */ public static Set getRepeatableAnnotation(AnnotatedElement annotatedElement, Class containerAnnotationType, Class annotationType) { + if (annotatedElement.getAnnotations().length == 0) { return Collections.emptySet(); } @@ -507,14 +508,14 @@ public abstract class AnnotationUtils { } if (nestedAnnotationsAsMap && value instanceof Annotation) { attrs.put(method.getName(), - getAnnotationAttributes((Annotation) value, classValuesAsString, nestedAnnotationsAsMap)); + getAnnotationAttributes((Annotation) value, classValuesAsString, true)); } else if (nestedAnnotationsAsMap && value instanceof Annotation[]) { Annotation[] realAnnotations = (Annotation[]) value; AnnotationAttributes[] mappedAnnotations = new AnnotationAttributes[realAnnotations.length]; for (int i = 0; i < realAnnotations.length; i++) { mappedAnnotations[i] = getAnnotationAttributes( - realAnnotations[i], classValuesAsString, nestedAnnotationsAsMap); + realAnnotations[i], classValuesAsString, true); } attrs.put(method.getName(), mappedAnnotations); } @@ -550,7 +551,7 @@ public abstract class AnnotationUtils { */ public static Object getValue(Annotation annotation, String attributeName) { try { - Method method = annotation.annotationType().getDeclaredMethod(attributeName, new Class[0]); + Method method = annotation.annotationType().getDeclaredMethod(attributeName); ReflectionUtils.makeAccessible(method); return method.invoke(annotation); } @@ -601,8 +602,7 @@ public abstract class AnnotationUtils { */ public static Object getDefaultValue(Class annotationType, String attributeName) { try { - Method method = annotationType.getDeclaredMethod(attributeName, new Class[0]); - return method.getDefaultValue(); + return annotationType.getDeclaredMethod(attributeName).getDefaultValue(); } catch (Exception ex) { return null; @@ -620,14 +620,11 @@ public abstract class AnnotationUtils { private final Set result = new LinkedHashSet(); - - public AnnotationCollector(Class containerAnnotationType, - Class annotationType) { + public AnnotationCollector(Class containerAnnotationType, Class annotationType) { this.containerAnnotationType = containerAnnotationType; this.annotationType = annotationType; } - public Set getResult(AnnotatedElement element) { process(element); return Collections.unmodifiableSet(this.result); @@ -662,6 +659,6 @@ public abstract class AnnotationUtils { + this.containerAnnotationType.getName(), ex); } } - } + } diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java index 46946f5543..ca0c71ebbc 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java @@ -119,8 +119,8 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito @Override public AnnotationAttributes getAnnotationAttributes(String annotationType, boolean classValuesAsString) { - AnnotationAttributes raw = AnnotationReadingVisitorUtils.getMergedAnnotationAttributes(this.attributesMap, - annotationType); + AnnotationAttributes raw = AnnotationReadingVisitorUtils.getMergedAnnotationAttributes( + this.attributesMap, annotationType); return AnnotationReadingVisitorUtils.convertClassValues(this.classLoader, raw, classValuesAsString); } diff --git a/spring-core/src/main/java/org/springframework/core/type/filter/AssignableTypeFilter.java b/spring-core/src/main/java/org/springframework/core/type/filter/AssignableTypeFilter.java index e55eca50e9..ac037d03a6 100644 --- a/spring-core/src/main/java/org/springframework/core/type/filter/AssignableTypeFilter.java +++ b/spring-core/src/main/java/org/springframework/core/type/filter/AssignableTypeFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-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. @@ -59,12 +59,12 @@ public class AssignableTypeFilter extends AbstractTypeHierarchyTraversingFilter return true; } else if (Object.class.getName().equals(typeName)) { - return Boolean.FALSE; + return false; } else if (typeName.startsWith("java.")) { try { Class clazz = getClass().getClassLoader().loadClass(typeName); - return Boolean.valueOf(this.targetType.isAssignableFrom(clazz)); + return this.targetType.isAssignableFrom(clazz); } catch (ClassNotFoundException ex) { // Class not found - can't determine a match that way. diff --git a/spring-core/src/main/java/org/springframework/util/MimeType.java b/spring-core/src/main/java/org/springframework/util/MimeType.java index 05bbb8d324..91c182a4c0 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeType.java +++ b/spring-core/src/main/java/org/springframework/util/MimeType.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-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. @@ -287,7 +287,7 @@ public class MimeType implements Comparable, Serializable { * method is not symmetric. * @param other the reference media type with which to compare * @return {@code true} if this media type includes the given media type; - * {@code false} otherwise + * {@code false} otherwise */ public boolean includes(MimeType other) { if (other == null) { @@ -424,7 +424,8 @@ public class MimeType implements Comparable, Serializable { return false; } MimeType otherType = (MimeType) other; - return (this.type.equalsIgnoreCase(otherType.type) && this.subtype.equalsIgnoreCase(otherType.subtype) && + return (this.type.equalsIgnoreCase(otherType.type) && + this.subtype.equalsIgnoreCase(otherType.subtype) && this.parameters.equals(otherType.parameters)); }