Skip to content

Commit 45598ca

Browse files
committed
Polishing
1 parent 4b30fe2 commit 45598ca

File tree

2 files changed

+61
-59
lines changed

2 files changed

+61
-59
lines changed

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

Lines changed: 46 additions & 45 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.
@@ -109,29 +109,22 @@ public InstantiationModelAwarePointcutAdvisorImpl(AspectJExpressionPointcut decl
109109

110110

111111
/**
112-
* The pointcut for Spring AOP to use. Actual behaviour of the pointcut will change
113-
* depending on the state of the advice.
112+
* The pointcut for Spring AOP to use.
113+
* Actual behaviour of the pointcut will change depending on the state of the advice.
114114
*/
115115
@Override
116116
public Pointcut getPointcut() {
117117
return this.pointcut;
118118
}
119119

120-
/**
121-
* This is only of interest for Spring AOP: AspectJ instantiation semantics
122-
* are much richer. In AspectJ terminology, all a return of {@code true}
123-
* means here is that the aspect is not a SINGLETON.
124-
*/
125120
@Override
126-
public boolean isPerInstance() {
127-
return (getAspectMetadata().getAjType().getPerClause().getKind() != PerClauseKind.SINGLETON);
121+
public boolean isLazy() {
122+
return this.lazy;
128123
}
129124

130-
/**
131-
* Return the AspectJ AspectMetadata for this advisor.
132-
*/
133-
public AspectMetadata getAspectMetadata() {
134-
return this.aspectInstanceFactory.getAspectMetadata();
125+
@Override
126+
public synchronized boolean isAdviceInstantiated() {
127+
return (this.instantiatedAdvice != null);
135128
}
136129

137130
/**
@@ -145,20 +138,26 @@ public synchronized Advice getAdvice() {
145138
return this.instantiatedAdvice;
146139
}
147140

148-
@Override
149-
public boolean isLazy() {
150-
return this.lazy;
141+
private Advice instantiateAdvice(AspectJExpressionPointcut pcut) {
142+
return this.aspectJAdvisorFactory.getAdvice(this.aspectJAdviceMethod, pcut,
143+
this.aspectInstanceFactory, this.declarationOrder, this.aspectName);
151144
}
152145

146+
/**
147+
* This is only of interest for Spring AOP: AspectJ instantiation semantics
148+
* are much richer. In AspectJ terminology, all a return of {@code true}
149+
* means here is that the aspect is not a SINGLETON.
150+
*/
153151
@Override
154-
public synchronized boolean isAdviceInstantiated() {
155-
return (this.instantiatedAdvice != null);
152+
public boolean isPerInstance() {
153+
return (getAspectMetadata().getAjType().getPerClause().getKind() != PerClauseKind.SINGLETON);
156154
}
157155

158-
159-
private Advice instantiateAdvice(AspectJExpressionPointcut pcut) {
160-
return this.aspectJAdvisorFactory.getAdvice(this.aspectJAdviceMethod, pcut,
161-
this.aspectInstanceFactory, this.declarationOrder, this.aspectName);
156+
/**
157+
* Return the AspectJ AspectMetadata for this advisor.
158+
*/
159+
public AspectMetadata getAspectMetadata() {
160+
return this.aspectInstanceFactory.getAspectMetadata();
162161
}
163162

164163
public MetadataAwareAspectInstanceFactory getAspectInstanceFactory() {
@@ -213,33 +212,26 @@ private void determineAdviceType() {
213212
}
214213
else {
215214
switch (aspectJAnnotation.getAnnotationType()) {
216-
case AtAfter:
217-
case AtAfterReturning:
218-
case AtAfterThrowing:
219-
this.isAfterAdvice = true;
220-
this.isBeforeAdvice = false;
221-
break;
222-
case AtAround:
223215
case AtPointcut:
224-
this.isAfterAdvice = false;
216+
case AtAround:
225217
this.isBeforeAdvice = false;
218+
this.isAfterAdvice = false;
226219
break;
227220
case AtBefore:
228-
this.isAfterAdvice = false;
229221
this.isBeforeAdvice = true;
222+
this.isAfterAdvice = false;
223+
break;
224+
case AtAfter:
225+
case AtAfterReturning:
226+
case AtAfterThrowing:
227+
this.isBeforeAdvice = false;
228+
this.isAfterAdvice = true;
229+
break;
230230
}
231231
}
232232
}
233233

234234

235-
@Override
236-
public String toString() {
237-
return "InstantiationModelAwarePointcutAdvisor: expression [" + getDeclaredPointcut().getExpression() +
238-
"]; advice method [" + this.aspectJAdviceMethod + "]; perClauseKind=" +
239-
this.aspectInstanceFactory.getAspectMetadata().getAjType().getPerClause().getKind();
240-
241-
}
242-
243235
private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
244236
inputStream.defaultReadObject();
245237
try {
@@ -250,11 +242,18 @@ private void readObject(ObjectInputStream inputStream) throws IOException, Class
250242
}
251243
}
252244

245+
@Override
246+
public String toString() {
247+
return "InstantiationModelAwarePointcutAdvisor: expression [" + getDeclaredPointcut().getExpression() +
248+
"]; advice method [" + this.aspectJAdviceMethod + "]; perClauseKind=" +
249+
this.aspectInstanceFactory.getAspectMetadata().getAjType().getPerClause().getKind();
250+
}
251+
253252

254253
/**
255254
* Pointcut implementation that changes its behaviour when the advice is instantiated.
256-
* Note that this is a <i>dynamic</i> pointcut. Otherwise it might
257-
* be optimized out if it does not at first match statically.
255+
* Note that this is a <i>dynamic</i> pointcut; otherwise it might be optimized out
256+
* if it does not at first match statically.
258257
*/
259258
private class PerTargetInstantiationModelPointcut extends DynamicMethodMatcherPointcut {
260259

@@ -264,8 +263,9 @@ private class PerTargetInstantiationModelPointcut extends DynamicMethodMatcherPo
264263

265264
private LazySingletonAspectInstanceFactoryDecorator aspectInstanceFactory;
266265

267-
private PerTargetInstantiationModelPointcut(AspectJExpressionPointcut declaredPointcut,
266+
public PerTargetInstantiationModelPointcut(AspectJExpressionPointcut declaredPointcut,
268267
Pointcut preInstantiationPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory) {
268+
269269
this.declaredPointcut = declaredPointcut;
270270
this.preInstantiationPointcut = preInstantiationPointcut;
271271
if (aspectInstanceFactory instanceof LazySingletonAspectInstanceFactoryDecorator) {
@@ -275,7 +275,8 @@ private PerTargetInstantiationModelPointcut(AspectJExpressionPointcut declaredPo
275275

276276
@Override
277277
public boolean matches(Method method, Class<?> targetClass) {
278-
// We're either instantiated and matching on declared pointcut, or uninstantiated matching on either pointcut
278+
// We're either instantiated and matching on declared pointcut,
279+
// or uninstantiated matching on either pointcut...
279280
return (isAspectMaterialized() && this.declaredPointcut.matches(method, targetClass)) ||
280281
this.preInstantiationPointcut.getMethodMatcher().matches(method, targetClass);
281282
}

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

Lines changed: 15 additions & 14 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.
@@ -20,9 +20,9 @@
2020
import java.lang.annotation.Annotation;
2121
import java.lang.reflect.Field;
2222
import java.lang.reflect.Method;
23+
import java.util.ArrayList;
2324
import java.util.Collections;
2425
import java.util.Comparator;
25-
import java.util.LinkedList;
2626
import java.util.List;
2727

2828
import org.aopalliance.aop.Advice;
@@ -131,7 +131,7 @@ public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory aspectInstan
131131
MetadataAwareAspectInstanceFactory lazySingletonAspectInstanceFactory =
132132
new LazySingletonAspectInstanceFactoryDecorator(aspectInstanceFactory);
133133

134-
List<Advisor> advisors = new LinkedList<Advisor>();
134+
List<Advisor> advisors = new ArrayList<Advisor>();
135135
for (Method method : getAdvisorMethods(aspectClass)) {
136136
Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, advisors.size(), aspectName);
137137
if (advisor != null) {
@@ -157,7 +157,7 @@ public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory aspectInstan
157157
}
158158

159159
private List<Method> getAdvisorMethods(Class<?> aspectClass) {
160-
final List<Method> methods = new LinkedList<Method>();
160+
final List<Method> methods = new ArrayList<Method>();
161161
ReflectionUtils.doWithMethods(aspectClass, new ReflectionUtils.MethodCallback() {
162162
@Override
163163
public void doWith(Method method) throws IllegalArgumentException {
@@ -176,7 +176,7 @@ public void doWith(Method method) throws IllegalArgumentException {
176176
* for the given introduction field.
177177
* <p>Resulting Advisors will need to be evaluated for targets.
178178
* @param introductionField the field to introspect
179-
* @return {@code null} if not an Advisor
179+
* @return the Advisor instance, or {@code null} if not an Advisor
180180
*/
181181
private Advisor getDeclareParentsAdvisor(Field introductionField) {
182182
DeclareParents declareParents = introductionField.getAnnotation(DeclareParents.class);
@@ -253,6 +253,15 @@ public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut
253253
AbstractAspectJAdvice springAdvice;
254254

255255
switch (aspectJAnnotation.getAnnotationType()) {
256+
case AtPointcut:
257+
if (logger.isDebugEnabled()) {
258+
logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'");
259+
}
260+
return null;
261+
case AtAround:
262+
springAdvice = new AspectJAroundAdvice(
263+
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
264+
break;
256265
case AtBefore:
257266
springAdvice = new AspectJMethodBeforeAdvice(
258267
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
@@ -277,15 +286,6 @@ public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut
277286
springAdvice.setThrowingName(afterThrowingAnnotation.throwing());
278287
}
279288
break;
280-
case AtAround:
281-
springAdvice = new AspectJAroundAdvice(
282-
candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
283-
break;
284-
case AtPointcut:
285-
if (logger.isDebugEnabled()) {
286-
logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'");
287-
}
288-
return null;
289289
default:
290290
throw new UnsupportedOperationException(
291291
"Unsupported advice type on method: " + candidateAdviceMethod);
@@ -299,6 +299,7 @@ public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut
299299
springAdvice.setArgumentNamesFromStringArray(argNames);
300300
}
301301
springAdvice.calculateArgumentBindings();
302+
302303
return springAdvice;
303304
}
304305

0 commit comments

Comments
 (0)