From 8c3cab7eadcb07be11ed725bc82277f046a4fe7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=A6=D1=8B=D0=BF?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Mon, 2 Nov 2020 09:51:38 +0200 Subject: [PATCH] Improve usage of AdvisedSupport.getAdvisors() --- .../aop/framework/Advised.java | 10 +++++- .../aop/framework/AdvisedSupport.java | 31 +++++-------------- .../aop/framework/AopProxyUtils.java | 4 +-- .../aop/framework/CglibAopProxy.java | 8 ++--- .../aop/framework/JdkDynamicAopProxy.java | 2 +- 5 files changed, 24 insertions(+), 31 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/Advised.java b/spring-aop/src/main/java/org/springframework/aop/framework/Advised.java index 035ae8b30f..ba5b46d3ad 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/Advised.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/Advised.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -223,4 +223,12 @@ public interface Advised extends TargetClassAware { */ String toProxyConfigString(); + /** + * Equivalent to {@code getAdvisors().length} + * @return count of advisors of this advised + */ + default int getAdvisorCount() { + return getAdvisors().length; + } + } diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java index b4de26beed..e49d894396 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -95,12 +95,6 @@ public class AdvisedSupport extends ProxyConfig implements Advised { */ private List advisors = new ArrayList<>(); - /** - * Array updated on changes to the advisors list, which is easier - * to manipulate internally. - */ - private Advisor[] advisorArray = new Advisor[0]; - /** * No-arg constructor for use as a JavaBean. @@ -244,7 +238,7 @@ public class AdvisedSupport extends ProxyConfig implements Advised { @Override public final Advisor[] getAdvisors() { - return this.advisorArray; + return this.advisors.toArray(new Advisor[0]); } @Override @@ -292,7 +286,6 @@ public class AdvisedSupport extends ProxyConfig implements Advised { } } - updateAdvisorArray(); adviceChanged(); } @@ -339,7 +332,6 @@ public class AdvisedSupport extends ProxyConfig implements Advised { Assert.notNull(advisor, "Advisor must not be null"); this.advisors.add(advisor); } - updateAdvisorArray(); adviceChanged(); } } @@ -363,27 +355,18 @@ public class AdvisedSupport extends ProxyConfig implements Advised { "Illegal position " + pos + " in advisor list with size " + this.advisors.size()); } this.advisors.add(pos, advisor); - updateAdvisorArray(); adviceChanged(); } - /** - * Bring the array up to date with the list. - */ - protected final void updateAdvisorArray() { - this.advisorArray = this.advisors.toArray(new Advisor[0]); - } - /** * Allows uncontrolled access to the {@link List} of {@link Advisor Advisors}. - *

Use with care, and remember to {@link #updateAdvisorArray() refresh the advisor array} - * and {@link #adviceChanged() fire advice changed events} when making any modifications. + *

Use with care, and remember to {@link #adviceChanged() fire advice changed events} + * when making any modifications. */ protected final List getAdvisorsInternal() { return this.advisors; } - @Override public void addAdvice(Advice advice) throws AopConfigException { int pos = this.advisors.size(); @@ -521,7 +504,6 @@ public class AdvisedSupport extends ProxyConfig implements Advised { Assert.notNull(advisor, "Advisor must not be null"); this.advisors.add(advisor); } - updateAdvisorArray(); adviceChanged(); } @@ -536,7 +518,6 @@ public class AdvisedSupport extends ProxyConfig implements Advised { copy.advisorChainFactory = this.advisorChainFactory; copy.interfaces = this.interfaces; copy.advisors = this.advisors; - copy.updateAdvisorArray(); return copy; } @@ -553,6 +534,10 @@ public class AdvisedSupport extends ProxyConfig implements Advised { this.methodCache = new ConcurrentHashMap<>(32); } + @Override + public int getAdvisorCount() { + return advisors.size(); + } @Override public String toProxyConfigString() { diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyUtils.java b/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyUtils.java index e417f6ebed..5dd18747ef 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AopProxyUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -206,7 +206,7 @@ public abstract class AopProxyUtils { * Check equality of the advisors behind the given AdvisedSupport objects. */ public static boolean equalsAdvisors(AdvisedSupport a, AdvisedSupport b) { - return Arrays.equals(a.getAdvisors(), b.getAdvisors()); + return a.getAdvisorCount() == b.getAdvisorCount() && Arrays.equals(a.getAdvisors(), b.getAdvisors()); } diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java b/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java index 6786472896..69fe9a0487 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java @@ -125,7 +125,7 @@ class CglibAopProxy implements AopProxy, Serializable { */ public CglibAopProxy(AdvisedSupport config) throws AopConfigException { Assert.notNull(config, "AdvisedSupport must not be null"); - if (config.getAdvisors().length == 0 && config.getTargetSource() == AdvisedSupport.EMPTY_TARGET_SOURCE) { + if (config.getAdvisorCount() == 0 && config.getTargetSource() == AdvisedSupport.EMPTY_TARGET_SOURCE) { throw new AopConfigException("No advisors and no TargetSource specified"); } this.advised = config; @@ -942,11 +942,11 @@ class CglibAopProxy implements AopProxy, Serializable { } // Advice instance identity is unimportant to the proxy class: // All that matters is type and ordering. - Advisor[] thisAdvisors = this.advised.getAdvisors(); - Advisor[] thatAdvisors = otherAdvised.getAdvisors(); - if (thisAdvisors.length != thatAdvisors.length) { + if (this.advised.getAdvisorCount() != otherAdvised.getAdvisorCount()) { return false; } + Advisor[] thisAdvisors = this.advised.getAdvisors(); + Advisor[] thatAdvisors = otherAdvised.getAdvisors(); for (int i = 0; i < thisAdvisors.length; i++) { Advisor thisAdvisor = thisAdvisors[i]; Advisor thatAdvisor = thatAdvisors[i]; diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java b/spring-aop/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java index 156cee3221..fcfaf93dd8 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java @@ -104,7 +104,7 @@ final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializa */ public JdkDynamicAopProxy(AdvisedSupport config) throws AopConfigException { Assert.notNull(config, "AdvisedSupport must not be null"); - if (config.getAdvisors().length == 0 && config.getTargetSource() == AdvisedSupport.EMPTY_TARGET_SOURCE) { + if (config.getAdvisorCount() == 0 && config.getTargetSource() == AdvisedSupport.EMPTY_TARGET_SOURCE) { throw new AopConfigException("No advisors and no TargetSource specified"); } this.advised = config;