Skip to content

Commit b71db12

Browse files
committed
Apply "instanceof pattern matching" in spring-aop
1 parent ab571e3 commit b71db12

38 files changed

+160
-161
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,10 @@ protected JoinPoint getJoinPoint() {
655655
@Nullable
656656
protected JoinPointMatch getJoinPointMatch() {
657657
MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
658-
if (!(mi instanceof ProxyMethodInvocation)) {
658+
if (!(mi instanceof ProxyMethodInvocation pmi)) {
659659
throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
660660
}
661-
return getJoinPointMatch((ProxyMethodInvocation) mi);
661+
return getJoinPointMatch(pmi);
662662
}
663663

664664
// Note: We can't use JoinPointMatch.getClass().getName() as the key, since

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -61,12 +61,12 @@ public static boolean isAfterAdvice(Advisor anAdvisor) {
6161
*/
6262
@Nullable
6363
public static AspectJPrecedenceInformation getAspectJPrecedenceInformationFor(Advisor anAdvisor) {
64-
if (anAdvisor instanceof AspectJPrecedenceInformation) {
65-
return (AspectJPrecedenceInformation) anAdvisor;
64+
if (anAdvisor instanceof AspectJPrecedenceInformation ajpi) {
65+
return ajpi;
6666
}
6767
Advice advice = anAdvisor.getAdvice();
68-
if (advice instanceof AspectJPrecedenceInformation) {
69-
return (AspectJPrecedenceInformation) advice;
68+
if (advice instanceof AspectJPrecedenceInformation ajpi) {
69+
return ajpi;
7070
}
7171
return null;
7272
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ private PointcutExpression obtainPointcutExpression() {
200200
*/
201201
@Nullable
202202
private ClassLoader determinePointcutClassLoader() {
203-
if (this.beanFactory instanceof ConfigurableBeanFactory) {
204-
return ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader();
203+
if (this.beanFactory instanceof ConfigurableBeanFactory cbf) {
204+
return cbf.getBeanClassLoader();
205205
}
206206
if (this.pointcutDeclarationScope != null) {
207207
return this.pointcutDeclarationScope.getClassLoader();
@@ -335,10 +335,10 @@ public boolean matches(Method method, Class<?> targetClass, Object... args) {
335335
try {
336336
MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
337337
targetObject = mi.getThis();
338-
if (!(mi instanceof ProxyMethodInvocation)) {
338+
if (!(mi instanceof ProxyMethodInvocation _pmi)) {
339339
throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
340340
}
341-
pmi = (ProxyMethodInvocation) mi;
341+
pmi = _pmi;
342342
thisObject = pmi.getProxy();
343343
}
344344
catch (IllegalStateException ex) {
@@ -404,8 +404,8 @@ private PointcutExpression getFallbackPointcutExpression(Class<?> targetClass) {
404404
}
405405

406406
private RuntimeTestWalker getRuntimeTestWalker(ShadowMatch shadowMatch) {
407-
if (shadowMatch instanceof DefensiveShadowMatch) {
408-
return new RuntimeTestWalker(((DefensiveShadowMatch) shadowMatch).primary);
407+
if (shadowMatch instanceof DefensiveShadowMatch defensiveShadowMatch) {
408+
return new RuntimeTestWalker(defensiveShadowMatch.primary);
409409
}
410410
return new RuntimeTestWalker(shadowMatch);
411411
}

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

Lines changed: 3 additions & 3 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-2022 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.
@@ -71,8 +71,8 @@ public static boolean makeAdvisorChainAspectJCapableIfNecessary(List<Advisor> ad
7171
private static boolean isAspectJAdvice(Advisor advisor) {
7272
return (advisor instanceof InstantiationModelAwarePointcutAdvisor ||
7373
advisor.getAdvice() instanceof AbstractAspectJAdvice ||
74-
(advisor instanceof PointcutAdvisor &&
75-
((PointcutAdvisor) advisor).getPointcut() instanceof AspectJExpressionPointcut));
74+
(advisor instanceof PointcutAdvisor pointcutAdvisor &&
75+
pointcutAdvisor.getPointcut() instanceof AspectJExpressionPointcut));
7676
}
7777

7878
static boolean isVariableName(@Nullable String name) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -202,8 +202,8 @@ public void visit(Instanceof i) {
202202
}
203203
Class<?> typeClass = null;
204204
ResolvedType type = (ResolvedType) i.getType();
205-
if (type instanceof ReferenceType) {
206-
ReferenceTypeDelegate delegate = ((ReferenceType) type).getDelegate();
205+
if (type instanceof ReferenceType referenceType) {
206+
ReferenceTypeDelegate delegate = referenceType.getDelegate();
207207
if (delegate instanceof ReflectionBasedReferenceTypeDelegate) {
208208
try {
209209
ReflectionUtils.makeAccessible(myClassField);

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

Lines changed: 3 additions & 3 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-2022 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.
@@ -69,8 +69,8 @@ public ClassLoader getAspectClassLoader() {
6969
*/
7070
@Override
7171
public int getOrder() {
72-
if (this.aspectInstance instanceof Ordered) {
73-
return ((Ordered) this.aspectInstance).getOrder();
72+
if (this.aspectInstance instanceof Ordered ordered) {
73+
return ordered.getOrder();
7474
}
7575
return getOrderForAspectClass(this.aspectInstance.getClass());
7676
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -117,9 +117,9 @@ private String replaceBooleanOperators(String pcExpr) {
117117
}
118118

119119
@Override
120-
public boolean equals(Object other) {
121-
return (this == other || (other instanceof TypePatternClassFilter &&
122-
ObjectUtils.nullSafeEquals(this.typePattern, ((TypePatternClassFilter) other).typePattern)));
120+
public boolean equals(Object obj) {
121+
return (this == obj || (obj instanceof TypePatternClassFilter that &&
122+
ObjectUtils.nullSafeEquals(this.typePattern, that.typePattern)));
123123
}
124124

125125
@Override

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -93,9 +93,8 @@ public Object getAspectInstance() {
9393
@Override
9494
@Nullable
9595
public ClassLoader getAspectClassLoader() {
96-
return (this.beanFactory instanceof ConfigurableBeanFactory ?
97-
((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() :
98-
ClassUtils.getDefaultClassLoader());
96+
return (this.beanFactory instanceof ConfigurableBeanFactory cbf ?
97+
cbf.getBeanClassLoader() : ClassUtils.getDefaultClassLoader());
9998
}
10099

101100
@Override
@@ -110,11 +109,11 @@ public Object getAspectCreationMutex() {
110109
// Rely on singleton semantics provided by the factory -> no local lock.
111110
return null;
112111
}
113-
else if (this.beanFactory instanceof ConfigurableBeanFactory) {
112+
else if (this.beanFactory instanceof ConfigurableBeanFactory cbf) {
114113
// No singleton guarantees from the factory -> let's lock locally but
115114
// reuse the factory's singleton lock, just in case a lazy dependency
116115
// of our advice bean happens to trigger the singleton lock implicitly...
117-
return ((ConfigurableBeanFactory) this.beanFactory).getSingletonMutex();
116+
return cbf.getSingletonMutex();
118117
}
119118
else {
120119
return this;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ public PerTargetInstantiationModelPointcut(AspectJExpressionPointcut declaredPoi
274274

275275
this.declaredPointcut = declaredPointcut;
276276
this.preInstantiationPointcut = preInstantiationPointcut;
277-
if (aspectInstanceFactory instanceof LazySingletonAspectInstanceFactoryDecorator) {
278-
this.aspectInstanceFactory = (LazySingletonAspectInstanceFactoryDecorator) aspectInstanceFactory;
277+
if (aspectInstanceFactory instanceof LazySingletonAspectInstanceFactoryDecorator lazyFactory) {
278+
this.aspectInstanceFactory = lazyFactory;
279279
}
280280
}
281281

spring-aop/src/main/java/org/springframework/aop/config/AbstractInterceptorDrivenBeanDefinitionDecorator.java

Lines changed: 3 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-2022 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.
@@ -93,8 +93,8 @@ public final BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder defin
9393
// copy autowire settings from original bean definition.
9494
proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
9595
proxyDefinition.setPrimary(targetDefinition.isPrimary());
96-
if (targetDefinition instanceof AbstractBeanDefinition) {
97-
proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
96+
if (targetDefinition instanceof AbstractBeanDefinition abd) {
97+
proxyDefinition.copyQualifiersFrom(abd);
9898
}
9999
// wrap it in a BeanDefinitionHolder with bean name
100100
result = new BeanDefinitionHolder(proxyDefinition, existingBeanName);

0 commit comments

Comments
 (0)