Skip to content

Commit 9decbf2

Browse files
committed
Polishing
1 parent 268b7a8 commit 9decbf2

File tree

6 files changed

+50
-39
lines changed

6 files changed

+50
-39
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -38,6 +38,14 @@
3838
* for an advice method from the pointcut expression, returning, and throwing clauses.
3939
* If an unambiguous interpretation is not available, it returns {@code null}.
4040
*
41+
* <h3>Algorithm Summary</h3>
42+
* <p>If an unambiguous binding can be deduced, then it is.
43+
* If the advice requirements cannot possibly be satisfied, then {@code null}
44+
* is returned. By setting the {@link #setRaiseExceptions(boolean) raiseExceptions}
45+
* property to {@code true}, descriptive exceptions will be thrown instead of
46+
* returning {@code null} in the case that the parameter names cannot be discovered.
47+
*
48+
* <h3>Algorithm Details</h3>
4149
* <p>This class interprets arguments in the following way:
4250
* <ol>
4351
* <li>If the first parameter of the method is of type {@link JoinPoint}
@@ -65,15 +73,15 @@
6573
* zero we proceed to the next stage. If {@code a} &gt; 1 then an
6674
* {@code AmbiguousBindingException} is raised. If {@code a} == 1,
6775
* and there are no unbound arguments of type {@code Annotation+},
68-
* then an {@code IllegalArgumentException} is raised. if there is
76+
* then an {@code IllegalArgumentException} is raised. If there is
6977
* exactly one such argument, then the corresponding parameter name is
7078
* assigned the value from the pointcut expression.</li>
71-
* <li>If a returningName has been set, and there are no unbound arguments
79+
* <li>If a {@code returningName} has been set, and there are no unbound arguments
7280
* then an {@code IllegalArgumentException} is raised. If there is
7381
* more than one unbound argument then an
7482
* {@code AmbiguousBindingException} is raised. If there is exactly
7583
* one unbound argument then the corresponding parameter name is assigned
76-
* the value &lt;returningName&gt;.</li>
84+
* the value of the {@code returningName}.</li>
7785
* <li>If there remain unbound arguments, then the pointcut expression is
7886
* examined once more for {@code this}, {@code target}, and
7987
* {@code args} pointcut expressions used in the binding form (binding
@@ -99,20 +107,12 @@
99107
* <p>The behavior on raising an {@code IllegalArgumentException} or
100108
* {@code AmbiguousBindingException} is configurable to allow this discoverer
101109
* to be used as part of a chain-of-responsibility. By default the condition will
102-
* be logged and the {@code getParameterNames(..)} method will simply return
110+
* be logged and the {@link #getParameterNames(Method)} method will simply return
103111
* {@code null}. If the {@link #setRaiseExceptions(boolean) raiseExceptions}
104112
* property is set to {@code true}, the conditions will be thrown as
105113
* {@code IllegalArgumentException} and {@code AmbiguousBindingException},
106114
* respectively.
107115
*
108-
* <p>Was that perfectly clear? ;)
109-
*
110-
* <p>Short version: If an unambiguous binding can be deduced, then it is.
111-
* If the advice requirements cannot possibly be satisfied, then {@code null}
112-
* is returned. By setting the {@link #setRaiseExceptions(boolean) raiseExceptions}
113-
* property to {@code true}, descriptive exceptions will be thrown instead of
114-
* returning {@code null} in the case that the parameter names cannot be discovered.
115-
*
116116
* @author Adrian Colyer
117117
* @author Juergen Hoeller
118118
* @since 2.0
@@ -158,7 +158,7 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov
158158

159159
/** The pointcut expression associated with the advice, as a simple String. */
160160
@Nullable
161-
private String pointcutExpression;
161+
private final String pointcutExpression;
162162

163163
private boolean raiseExceptions;
164164

@@ -197,7 +197,7 @@ public void setRaiseExceptions(boolean raiseExceptions) {
197197

198198
/**
199199
* If {@code afterReturning} advice binds the return value, the
200-
* returning variable name must be specified.
200+
* {@code returning} variable name must be specified.
201201
* @param returningName the name of the returning variable
202202
*/
203203
public void setReturningName(@Nullable String returningName) {
@@ -206,18 +206,17 @@ public void setReturningName(@Nullable String returningName) {
206206

207207
/**
208208
* If {@code afterThrowing} advice binds the thrown value, the
209-
* throwing variable name must be specified.
209+
* {@code throwing} variable name must be specified.
210210
* @param throwingName the name of the throwing variable
211211
*/
212212
public void setThrowingName(@Nullable String throwingName) {
213213
this.throwingName = throwingName;
214214
}
215215

216-
217216
/**
218217
* Deduce the parameter names for an advice method.
219-
* <p>See the {@link AspectJAdviceParameterNameDiscoverer class level javadoc}
220-
* for this class for details of the algorithm used.
218+
* <p>See the {@link AspectJAdviceParameterNameDiscoverer class-level javadoc}
219+
* for this class for details on the algorithm used.
221220
* @param method the target {@link Method}
222221
* @return the parameter names
223222
*/
@@ -316,13 +315,13 @@ public String[] getParameterNames(Constructor<?> ctor) {
316315
}
317316

318317

319-
private void bindParameterName(int index, String name) {
318+
private void bindParameterName(int index, @Nullable String name) {
320319
this.parameterNameBindings[index] = name;
321320
this.numberOfRemainingUnboundArguments--;
322321
}
323322

324323
/**
325-
* If the first parameter is of type JoinPoint or ProceedingJoinPoint,bind "thisJoinPoint" as
324+
* If the first parameter is of type JoinPoint or ProceedingJoinPoint, bind "thisJoinPoint" as
326325
* parameter name and return true, else return false.
327326
*/
328327
private boolean maybeBindThisJoinPoint() {
@@ -367,8 +366,8 @@ private void maybeBindThrowingVariable() {
367366
}
368367

369368
if (throwableIndex == -1) {
370-
throw new IllegalStateException("Binding of throwing parameter '" + this.throwingName
371-
+ "' could not be completed as no available arguments are a subtype of Throwable");
369+
throw new IllegalStateException("Binding of throwing parameter '" + this.throwingName +
370+
"' could not be completed as no available arguments are a subtype of Throwable");
372371
}
373372
else {
374373
bindParameterName(throwableIndex, this.throwingName);
@@ -400,7 +399,6 @@ private void maybeBindReturningVariable() {
400399
}
401400
}
402401

403-
404402
/**
405403
* Parse the string pointcut expression looking for:
406404
* &#64;this, &#64;target, &#64;args, &#64;within, &#64;withincode, &#64;annotation.
@@ -465,7 +463,7 @@ else if (numAnnotationSlots == 1) {
465463
}
466464
}
467465

468-
/*
466+
/**
469467
* If the token starts meets Java identifier conventions, it's in.
470468
*/
471469
@Nullable
@@ -533,7 +531,6 @@ else if (tokens[i].equals("args") || tokens[i].startsWith("args(")) {
533531
}
534532
}
535533

536-
537534
if (varNames.size() > 1) {
538535
throw new AmbiguousBindingException("Found " + varNames.size() +
539536
" candidate this(), target() or args() variables but only one unbound argument slot");
@@ -609,7 +606,7 @@ else if (varNames.size() == 1) {
609606
// else varNames.size must be 0 and we have nothing to bind.
610607
}
611608

612-
/*
609+
/**
613610
* We've found the start of a binding pointcut at the given index into the
614611
* token array. Now we need to extract the pointcut body and return it.
615612
*/
@@ -709,7 +706,7 @@ private boolean alreadyBound(String varName) {
709706
return false;
710707
}
711708

712-
/*
709+
/**
713710
* Return {@code true} if the given argument type is a subclass
714711
* of the given supertype.
715712
*/
@@ -737,7 +734,7 @@ private int countNumberOfUnboundPrimitiveArguments() {
737734
return count;
738735
}
739736

740-
/*
737+
/**
741738
* Find the argument index with the given type, and bind the given
742739
* {@code varName} in that position.
743740
*/

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -102,7 +102,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
102102
private boolean singletonsCurrentlyInDestruction = false;
103103

104104
/** Disposable bean instances: bean name to disposable instance. */
105-
private final Map<String, Object> disposableBeans = new LinkedHashMap<>();
105+
private final Map<String, DisposableBean> disposableBeans = new LinkedHashMap<>();
106106

107107
/** Map between containing bean names: bean name to Set of bean names that the bean contains. */
108108
private final Map<String, Set<String>> containedBeanMap = new ConcurrentHashMap<>(16);
@@ -340,7 +340,7 @@ protected boolean isActuallyInCreation(String beanName) {
340340
* (within the entire factory).
341341
* @param beanName the name of the bean
342342
*/
343-
public boolean isSingletonCurrentlyInCreation(String beanName) {
343+
public boolean isSingletonCurrentlyInCreation(@Nullable String beanName) {
344344
return this.singletonsCurrentlyInCreation.contains(beanName);
345345
}
346346

@@ -554,7 +554,7 @@ public void destroySingleton(String beanName) {
554554
// Destroy the corresponding DisposableBean instance.
555555
DisposableBean disposableBean;
556556
synchronized (this.disposableBeans) {
557-
disposableBean = (DisposableBean) this.disposableBeans.remove(beanName);
557+
disposableBean = this.disposableBeans.remove(beanName);
558558
}
559559
destroyBean(beanName, disposableBean);
560560
}

spring-beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
6868

6969
private static final String SHUTDOWN_METHOD_NAME = "shutdown";
7070

71-
private static final Log logger = LogFactory.getLog(DisposableBeanAdapter.class);
7271

72+
private static final Log logger = LogFactory.getLog(DisposableBeanAdapter.class);
7373

7474
private final Object bean;
7575

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ ApplicationEventMulticaster getApplicationEventMulticaster() throws IllegalState
447447

448448
@Override
449449
public void setApplicationStartup(ApplicationStartup applicationStartup) {
450-
Assert.notNull(applicationStartup, "applicationStartup should not be null");
450+
Assert.notNull(applicationStartup, "ApplicationStartup must not be null");
451451
this.applicationStartup = applicationStartup;
452452
}
453453

spring-context/src/test/java/org/springframework/context/annotation/configuration/ImportTests.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -41,6 +41,7 @@ class ImportTests {
4141

4242
private DefaultListableBeanFactory processConfigurationClasses(Class<?>... classes) {
4343
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
44+
beanFactory.setAllowBeanDefinitionOverriding(false);
4445
for (Class<?> clazz : classes) {
4546
beanFactory.registerBeanDefinition(clazz.getSimpleName(), new RootBeanDefinition(clazz));
4647
}
@@ -56,9 +57,10 @@ private void assertBeanDefinitionCount(int expectedCount, Class<?>... classes) {
5657
for (Class<?> clazz : classes) {
5758
beanFactory.getBean(clazz);
5859
}
59-
6060
}
6161

62+
// ------------------------------------------------------------------------
63+
6264
@Test
6365
void testProcessImportsWithAsm() {
6466
int configClasses = 2;
@@ -158,6 +160,13 @@ void testImportAnnotationWithThreeLevelRecursion() {
158160
assertBeanDefinitionCount(configClasses + beansInClasses, FirstLevel.class);
159161
}
160162

163+
@Test
164+
void testImportAnnotationWithThreeLevelRecursionAndDoubleImport() {
165+
int configClasses = 5;
166+
int beansInClasses = 5;
167+
assertBeanDefinitionCount(configClasses + beansInClasses, FirstLevel.class, FirstLevelPlus.class);
168+
}
169+
161170
// ------------------------------------------------------------------------
162171

163172
@Test
@@ -167,7 +176,6 @@ void testImportAnnotationWithMultipleArguments() {
167176
assertBeanDefinitionCount((configClasses + beansInClasses), WithMultipleArgumentsToImportAnnotation.class);
168177
}
169178

170-
171179
@Test
172180
void testImportAnnotationWithMultipleArgumentsResultingInOverriddenBeanDefinition() {
173181
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
@@ -245,6 +253,11 @@ TestBean m() {
245253
}
246254
}
247255

256+
@Configuration
257+
@Import(ThirdLevel.class)
258+
static class FirstLevelPlus {
259+
}
260+
248261
@Configuration
249262
@Import({ThirdLevel.class, InitBean.class})
250263
static class SecondLevel {

spring-core/src/main/java/org/springframework/core/MethodParameter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -781,6 +781,7 @@ public MethodParameter clone() {
781781
return new MethodParameter(this);
782782
}
783783

784+
784785
/**
785786
* Create a new MethodParameter for the given method or constructor.
786787
* <p>This is a convenience factory method for scenarios where a

0 commit comments

Comments
 (0)