Skip to content

Commit c69fdfa

Browse files
committed
Polishing
1 parent bd060a1 commit c69fdfa

File tree

6 files changed

+63
-61
lines changed

6 files changed

+63
-61
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,6 +59,9 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac
5959

6060
private static final String AJC_MAGIC = "ajc$";
6161

62+
private static final Class<?>[] ASPECTJ_ANNOTATION_CLASSES = new Class<?>[] {
63+
Pointcut.class, Before.class, Around.class, After.class, AfterReturning.class, AfterThrowing.class};
64+
6265

6366
/** Logger available to subclasses */
6467
protected final Log logger = LogFactory.getLog(getClass());
@@ -122,14 +125,12 @@ public void validate(Class<?> aspectClass) throws AopConfigException {
122125

123126
/**
124127
* Find and return the first AspectJ annotation on the given method
125-
* (there <i>should</i> only be one anyway...)
128+
* (there <i>should</i> only be one anyway...).
126129
*/
127130
@SuppressWarnings("unchecked")
128131
protected static AspectJAnnotation<?> findAspectJAnnotationOnMethod(Method method) {
129-
Class<?>[] classesToLookFor = new Class<?>[] {
130-
Before.class, Around.class, After.class, AfterReturning.class, AfterThrowing.class, Pointcut.class};
131-
for (Class<?> c : classesToLookFor) {
132-
AspectJAnnotation<?> foundAnnotation = findAnnotation(method, (Class<Annotation>) c);
132+
for (Class<?> clazz : ASPECTJ_ANNOTATION_CLASSES) {
133+
AspectJAnnotation<?> foundAnnotation = findAnnotation(method, (Class<Annotation>) clazz);
133134
if (foundAnnotation != null) {
134135
return foundAnnotation;
135136
}
@@ -148,6 +149,9 @@ private static <A extends Annotation> AspectJAnnotation<A> findAnnotation(Method
148149
}
149150

150151

152+
/**
153+
* AspectJ annotation types.
154+
*/
151155
protected enum AspectJAnnotationType {
152156

153157
AtPointcut,
@@ -167,16 +171,16 @@ protected static class AspectJAnnotation<A extends Annotation> {
167171

168172
private static final String[] EXPRESSION_PROPERTIES = new String[] {"value", "pointcut"};
169173

170-
private static Map<Class<?>, AspectJAnnotationType> annotationTypes =
171-
new HashMap<Class<?>, AspectJAnnotationType>();
174+
private static Map<Class<?>, AspectJAnnotationType> annotationTypeMap =
175+
new HashMap<Class<?>, AspectJAnnotationType>(8);
172176

173177
static {
174-
annotationTypes.put(Pointcut.class,AspectJAnnotationType.AtPointcut);
175-
annotationTypes.put(After.class,AspectJAnnotationType.AtAfter);
176-
annotationTypes.put(AfterReturning.class,AspectJAnnotationType.AtAfterReturning);
177-
annotationTypes.put(AfterThrowing.class,AspectJAnnotationType.AtAfterThrowing);
178-
annotationTypes.put(Around.class,AspectJAnnotationType.AtAround);
179-
annotationTypes.put(Before.class,AspectJAnnotationType.AtBefore);
178+
annotationTypeMap.put(Pointcut.class, AspectJAnnotationType.AtPointcut);
179+
annotationTypeMap.put(Before.class, AspectJAnnotationType.AtBefore);
180+
annotationTypeMap.put(After.class, AspectJAnnotationType.AtAfter);
181+
annotationTypeMap.put(AfterReturning.class, AspectJAnnotationType.AtAfterReturning);
182+
annotationTypeMap.put(AfterThrowing.class, AspectJAnnotationType.AtAfterThrowing);
183+
annotationTypeMap.put(Around.class, AspectJAnnotationType.AtAround);
180184
}
181185

182186
private final A annotation;
@@ -202,9 +206,9 @@ public AspectJAnnotation(A annotation) {
202206
}
203207

204208
private AspectJAnnotationType determineAnnotationType(A annotation) {
205-
for (Class<?> type : annotationTypes.keySet()) {
209+
for (Class<?> type : annotationTypeMap.keySet()) {
206210
if (type.isInstance(annotation)) {
207-
return annotationTypes.get(type);
211+
return annotationTypeMap.get(type);
208212
}
209213
}
210214
throw new IllegalStateException("Unknown annotation type: " + annotation.toString());
@@ -213,19 +217,15 @@ private AspectJAnnotationType determineAnnotationType(A annotation) {
213217
private String resolveExpression(A annotation) throws Exception {
214218
String expression = null;
215219
for (String methodName : EXPRESSION_PROPERTIES) {
216-
Method method;
217220
try {
218-
method = annotation.getClass().getDeclaredMethod(methodName);
219-
}
220-
catch (NoSuchMethodException ex) {
221-
method = null;
222-
}
223-
if (method != null) {
221+
Method method = annotation.getClass().getDeclaredMethod(methodName);
224222
String candidate = (String) method.invoke(annotation);
225223
if (StringUtils.hasText(candidate)) {
226224
expression = candidate;
227225
}
228226
}
227+
catch (NoSuchMethodException ex) {
228+
}
229229
}
230230
return expression;
231231
}

spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
* @author Stephane Nicoll
5454
* @since 2.5
5555
* @see ContextAnnotationAutowireCandidateResolver
56-
* @see CommonAnnotationBeanPostProcessor
5756
* @see ConfigurationClassPostProcessor
57+
* @see CommonAnnotationBeanPostProcessor
5858
* @see org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
5959
* @see org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
6060
* @see org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor
@@ -102,7 +102,6 @@ public class AnnotationConfigUtils {
102102
public static final String PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME =
103103
"org.springframework.context.annotation.internalPersistenceAnnotationProcessor";
104104

105-
106105
private static final String PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME =
107106
"org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor";
108107

@@ -155,7 +154,7 @@ public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
155154
}
156155
}
157156

158-
Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<BeanDefinitionHolder>(4);
157+
Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<BeanDefinitionHolder>(8);
159158

160159
if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
161160
RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
@@ -202,6 +201,7 @@ public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
202201
def.setSource(source);
203202
beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
204203
}
204+
205205
if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
206206
RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
207207
def.setSource(source);

spring-context/src/main/java/org/springframework/context/event/DefaultEventListenerFactory.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
/**
2525
* Default {@link EventListenerFactory} implementation that supports the
2626
* regular {@link EventListener} annotation.
27+
*
2728
* <p>Used as "catch-all" implementation by default.
2829
*
2930
* @author Stephane Nicoll
@@ -33,15 +34,17 @@ public class DefaultEventListenerFactory implements EventListenerFactory, Ordere
3334

3435
private int order = LOWEST_PRECEDENCE;
3536

36-
@Override
37-
public int getOrder() {
38-
return order;
39-
}
4037

4138
public void setOrder(int order) {
4239
this.order = order;
4340
}
4441

42+
@Override
43+
public int getOrder() {
44+
return this.order;
45+
}
46+
47+
4548
public boolean supportsMethod(Method method) {
4649
return true;
4750
}

spring-context/src/main/java/org/springframework/context/event/EventListenerMethodProcessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,8 +44,7 @@
4444
import org.springframework.util.CollectionUtils;
4545

4646
/**
47-
* Register {@link EventListener} annotated method as individual {@link ApplicationListener}
48-
* instances.
47+
* Registers {@link EventListener} methods as individual {@link ApplicationListener} instances.
4948
*
5049
* @author Stephane Nicoll
5150
* @author Juergen Hoeller

spring-tx/src/main/java/org/springframework/transaction/event/TransactionalEventListenerFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,11 +21,11 @@
2121
import org.springframework.context.ApplicationListener;
2222
import org.springframework.context.event.EventListenerFactory;
2323
import org.springframework.core.Ordered;
24-
import org.springframework.core.annotation.AnnotationUtils;
24+
import org.springframework.core.annotation.AnnotatedElementUtils;
2525

2626
/**
2727
* {@link EventListenerFactory} implementation that handles {@link TransactionalEventListener}
28-
* annotated method.
28+
* annotated methods.
2929
*
3030
* @author Stephane Nicoll
3131
* @since 4.2
@@ -47,7 +47,7 @@ public int getOrder() {
4747

4848
@Override
4949
public boolean supportsMethod(Method method) {
50-
return (AnnotationUtils.findAnnotation(method, TransactionalEventListener.class) != null);
50+
return AnnotatedElementUtils.hasAnnotation(method, TransactionalEventListener.class);
5151
}
5252

5353
@Override

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -116,6 +116,28 @@
116116
public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
117117
implements BeanFactoryAware, InitializingBean {
118118

119+
/**
120+
* MethodFilter that matches {@link InitBinder @InitBinder} methods.
121+
*/
122+
public static final MethodFilter INIT_BINDER_METHODS = new MethodFilter() {
123+
@Override
124+
public boolean matches(Method method) {
125+
return (AnnotationUtils.findAnnotation(method, InitBinder.class) != null);
126+
}
127+
};
128+
129+
/**
130+
* MethodFilter that matches {@link ModelAttribute @ModelAttribute} methods.
131+
*/
132+
public static final MethodFilter MODEL_ATTRIBUTE_METHODS = new MethodFilter() {
133+
@Override
134+
public boolean matches(Method method) {
135+
return (AnnotationUtils.findAnnotation(method, RequestMapping.class) == null &&
136+
AnnotationUtils.findAnnotation(method, ModelAttribute.class) != null);
137+
}
138+
};
139+
140+
119141
private List<HandlerMethodArgumentResolver> customArgumentResolvers;
120142

121143
private HandlerMethodArgumentResolverComposite argumentResolvers;
@@ -945,26 +967,4 @@ private ModelAndView getModelAndView(ModelAndViewContainer mavContainer,
945967
return mav;
946968
}
947969

948-
949-
/**
950-
* MethodFilter that matches {@link InitBinder @InitBinder} methods.
951-
*/
952-
public static final MethodFilter INIT_BINDER_METHODS = new MethodFilter() {
953-
@Override
954-
public boolean matches(Method method) {
955-
return AnnotationUtils.findAnnotation(method, InitBinder.class) != null;
956-
}
957-
};
958-
959-
/**
960-
* MethodFilter that matches {@link ModelAttribute @ModelAttribute} methods.
961-
*/
962-
public static final MethodFilter MODEL_ATTRIBUTE_METHODS = new MethodFilter() {
963-
@Override
964-
public boolean matches(Method method) {
965-
return ((AnnotationUtils.findAnnotation(method, RequestMapping.class) == null) &&
966-
(AnnotationUtils.findAnnotation(method, ModelAttribute.class) != null));
967-
}
968-
};
969-
970970
}

0 commit comments

Comments
 (0)