Improve usage of AdvisedSupport.getAdvisors()

This commit is contained in:
Сергей Цыпанов 2020-11-02 09:51:38 +02:00 committed by Juergen Hoeller
parent 0015fd6734
commit 8c3cab7ead
5 changed files with 24 additions and 31 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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(); String toProxyConfigString();
/**
* Equivalent to {@code getAdvisors().length}
* @return count of advisors of this advised
*/
default int getAdvisorCount() {
return getAdvisors().length;
}
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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<Advisor> advisors = new ArrayList<>(); private List<Advisor> 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. * No-arg constructor for use as a JavaBean.
@ -244,7 +238,7 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
@Override @Override
public final Advisor[] getAdvisors() { public final Advisor[] getAdvisors() {
return this.advisorArray; return this.advisors.toArray(new Advisor[0]);
} }
@Override @Override
@ -292,7 +286,6 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
} }
} }
updateAdvisorArray();
adviceChanged(); adviceChanged();
} }
@ -339,7 +332,6 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
Assert.notNull(advisor, "Advisor must not be null"); Assert.notNull(advisor, "Advisor must not be null");
this.advisors.add(advisor); this.advisors.add(advisor);
} }
updateAdvisorArray();
adviceChanged(); adviceChanged();
} }
} }
@ -363,27 +355,18 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
"Illegal position " + pos + " in advisor list with size " + this.advisors.size()); "Illegal position " + pos + " in advisor list with size " + this.advisors.size());
} }
this.advisors.add(pos, advisor); this.advisors.add(pos, advisor);
updateAdvisorArray();
adviceChanged(); 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}. * Allows uncontrolled access to the {@link List} of {@link Advisor Advisors}.
* <p>Use with care, and remember to {@link #updateAdvisorArray() refresh the advisor array} * <p>Use with care, and remember to {@link #adviceChanged() fire advice changed events}
* and {@link #adviceChanged() fire advice changed events} when making any modifications. * when making any modifications.
*/ */
protected final List<Advisor> getAdvisorsInternal() { protected final List<Advisor> getAdvisorsInternal() {
return this.advisors; return this.advisors;
} }
@Override @Override
public void addAdvice(Advice advice) throws AopConfigException { public void addAdvice(Advice advice) throws AopConfigException {
int pos = this.advisors.size(); int pos = this.advisors.size();
@ -521,7 +504,6 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
Assert.notNull(advisor, "Advisor must not be null"); Assert.notNull(advisor, "Advisor must not be null");
this.advisors.add(advisor); this.advisors.add(advisor);
} }
updateAdvisorArray();
adviceChanged(); adviceChanged();
} }
@ -536,7 +518,6 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
copy.advisorChainFactory = this.advisorChainFactory; copy.advisorChainFactory = this.advisorChainFactory;
copy.interfaces = this.interfaces; copy.interfaces = this.interfaces;
copy.advisors = this.advisors; copy.advisors = this.advisors;
copy.updateAdvisorArray();
return copy; return copy;
} }
@ -553,6 +534,10 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
this.methodCache = new ConcurrentHashMap<>(32); this.methodCache = new ConcurrentHashMap<>(32);
} }
@Override
public int getAdvisorCount() {
return advisors.size();
}
@Override @Override
public String toProxyConfigString() { public String toProxyConfigString() {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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. * Check equality of the advisors behind the given AdvisedSupport objects.
*/ */
public static boolean equalsAdvisors(AdvisedSupport a, AdvisedSupport b) { 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());
} }

View File

@ -125,7 +125,7 @@ class CglibAopProxy implements AopProxy, Serializable {
*/ */
public CglibAopProxy(AdvisedSupport config) throws AopConfigException { public CglibAopProxy(AdvisedSupport config) throws AopConfigException {
Assert.notNull(config, "AdvisedSupport must not be null"); 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"); throw new AopConfigException("No advisors and no TargetSource specified");
} }
this.advised = config; this.advised = config;
@ -942,11 +942,11 @@ class CglibAopProxy implements AopProxy, Serializable {
} }
// Advice instance identity is unimportant to the proxy class: // Advice instance identity is unimportant to the proxy class:
// All that matters is type and ordering. // All that matters is type and ordering.
Advisor[] thisAdvisors = this.advised.getAdvisors(); if (this.advised.getAdvisorCount() != otherAdvised.getAdvisorCount()) {
Advisor[] thatAdvisors = otherAdvised.getAdvisors();
if (thisAdvisors.length != thatAdvisors.length) {
return false; return false;
} }
Advisor[] thisAdvisors = this.advised.getAdvisors();
Advisor[] thatAdvisors = otherAdvised.getAdvisors();
for (int i = 0; i < thisAdvisors.length; i++) { for (int i = 0; i < thisAdvisors.length; i++) {
Advisor thisAdvisor = thisAdvisors[i]; Advisor thisAdvisor = thisAdvisors[i];
Advisor thatAdvisor = thatAdvisors[i]; Advisor thatAdvisor = thatAdvisors[i];

View File

@ -104,7 +104,7 @@ final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializa
*/ */
public JdkDynamicAopProxy(AdvisedSupport config) throws AopConfigException { public JdkDynamicAopProxy(AdvisedSupport config) throws AopConfigException {
Assert.notNull(config, "AdvisedSupport must not be null"); 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"); throw new AopConfigException("No advisors and no TargetSource specified");
} }
this.advised = config; this.advised = config;