>>();
publicvoidsetInterfaces(Class
>...interfaces){
Assert.notNull(interfaces,"Interfacesmustnotbenull");
this.interfaces.clear();
for(Class
>ifc:
interfaces){
addInterface(ifc);
}
}
publicvoidaddInterface(Class
>intf){
Assert.notNull(intf,"Interfacemustnotbenull");
if(!
intf.isInterface()){
thrownewIllegalArgumentException("["+intf.getName()+"]isnotaninterface");
}
if(!
this.interfaces.contains(intf)){
this.interfaces.add(intf);
//当代理接口有更改,也同时清空methodCache
adviceChanged();
}
}
protectedvoidadviceChanged(){
this.methodCache.clear();
}
publicbooleanremoveInterface(Class
>intf){
returnthis.interfaces.remove(intf);
}
@Override
publicClass
>[]getProxiedInterfaces(){
returnthis.interfaces.toArray(newClass
>[this.interfaces.size()]);
}
//判断所有的目标类是否是接口
@Override
publicbooleanisInterfaceProxied(Class
>intf){
for(Class
>proxyIntf:
this.interfaces){
if(intf.isAssignableFrom(proxyIntf)){
returntrue;
}
}
returnfalse;
}
设置代理目标的方法:
publicstaticfinalTargetSourceEMPTY_TARGET_SOURCE=EmptyTargetSource.INSTANCE;
TargetSourcetargetSource=EMPTY_TARGET_SOURCE;
//将根据给得的object创建SingletonTargetSource
publicvoidsetTarget(Objecttarget){
setTargetSource(newSingletonTargetSource(target));
}
//直接设置TargetSource相应的接口实现,若为空,返回EMPTY_TARGET_SOURCE
@Override
publicvoidsetTargetSource(TargetSourcetargetSource){
this.targetSource=(targetSource!
=null?
targetSource:
EMPTY_TARGET_SOURCE);
}
//该代理目标类能够转为targetClass,该方法是前面两个方法的替代:
当我们想要一个没有可达TargetSource的代理类
publicvoidsetTargetClass(Class
>targetClass){
this.targetSource=EmptyTargetSource.forClass(targetClass);
}
@Override
publicClass
>getTargetClass(){
returnthis.targetSource.getTargetClass();
}
设置是否预先过滤,默认为false:
privatebooleanpreFiltered=false;
@Override
publicvoidsetPreFiltered(booleanpreFiltered){
this.preFiltered=preFiltered;
}
@Override
publicbooleanisPreFiltered(){
returnthis.preFiltered;
}
设置AdvisorChainFactory:
AdvisorChainFactoryadvisorChainFactory=newDefaultAdvisorChainFactory();
publicvoidsetAdvisorChainFactory(AdvisorChainFactoryadvisorChainFactory){
Assert.notNull(advisorChainFactory,"AdvisorChainFactorymustnotbenull");
this.advisorChainFactory=advisorChainFactory;
}
publicAdvisorChainFactorygetAdvisorChainFactory(){
returnthis.advisorChainFactory;
}
对于Advisor的操作:
privateAdvisor[]advisorArray=newAdvisor[0];
//获取操作
protectedfinalListgetAdvisorsInternal(){
returnthis.advisors;
}
@Override
publicfinalAdvisor[]getAdvisors(){
returnthis.advisorArray;
}
//添加操作
@Override
publicvoidaddAdvisor(Advisoradvisor){
intpos=this.advisors.size();
addAdvisor(pos,advisor);
}
@Override
publicvoidaddAdvisor(intpos,Advisoradvisor)throwsAopConfigException{
if(advisorinstanceofIntroductionAdvisor){
validateIntroductionAdvisor((IntroductionAdvisor)advisor);
}
addAdvisorInternal(pos,advisor);
}
privatevoidvalidateIntroductionAdvisor(IntroductionAdvisoradvisor){
advisor.validateInterfaces();
//Iftheadvisorpassedvalidation,wecanmakethechange.
Class
>[]ifcs=advisor.getInterfaces();
for(Class
>ifc:
ifcs){
addInterface(ifc);
}
}
privatevoidaddAdvisorInternal(intpos,Advisoradvisor)throwsAopConfigException{
Assert.notNull(advisor,"Advisormustnotbenull");
if(isFrozen()){
thrownewAopConfigException("Cannotaddadvisor:
Configurationisfrozen.");
}
if(pos>this.advisors.size()){
thrownewIllegalArgumentException(
"Illegalposition"+pos+"inadvisorlistwithsize"+this.advisors.size());
}
this.advisors.add(pos,advisor);
updateAdvisorArray();
adviceChanged();
}
protectedfinalvoidupdateAdvisorArray(){
this.advisorArray=this.advisors.toArray(newAdvisor[this.advisors.size()]);
}
//对于多个Advisor的添加
publicvoidaddAdvisors(Advisor...advisors){
addAdvisors(Arrays.asList(advisors));
}
//对于Advisor集合的添加
publicvoidaddAdvisors(Collectionadvisors){
if(isFrozen()){
thrownewAopConfigException("Cannotaddadvisor:
Configurationisfrozen.");
}
if(!
CollectionUtils.isEmpty(advisors)){
for(Advisoradvisor:
advisors){
if(advisorinstanceofIntroductionAdvisor){
validateIntroductionAdvisor((IntroductionAdvisor)advisor);
}
Assert.notNull(advisor,"Advisormustnotbenull");
this.advisors.add(advisor);
}
updateAdvisorArray();
adviceChanged();
}
}
//删除操作
@Override
publicbooleanremoveAdvisor(Advisoradvisor){
intindex=indexOf(advisor);
if(index==-1){
returnfalse;
}
else{
removeAdvisor(index);
returntrue;
}
}
@Override
publicintindexOf(Advisoradvisor){
Assert.notNull(advisor,"Advisormustnotbenull");
returnthis.advisors.indexOf(advisor);
}
@Override
publicvoidremoveAdvisor(intindex)throwsAopConfigException{
if(isFrozen()){
thrownewAopConfigException("CannotremoveAdvisor:
Configurationisfrozen.");
}
if(index<0||index>this.advisors.size()-1){
thrownewAopConfigException("Advisorindex"+index+"isoutofbounds:
"+
"Thisconfigurationonlyhas"+this.advisors.size()+"advisors.");
}
Advisoradvisor=this.advisors.get(index);
if(advisorinstanceofIntroductionAdvisor){
IntroductionAdvisoria=(IntroductionAdvisor)advisor;
//Weneedtoremoveintroductioninterfaces.
for(intj=0;jremoveInterface(ia.getInterfaces()[j]);
}
}
this.advisors.remove(index);
updateAdvisorArray();
adviceChanged();
}
//替换操作
@Override
publicbooleanreplaceAdvisor(Advisora,Advisorb)throwsAopConfigException{
Assert.notNull(a,"Advisoramustnotbenull");
Assert.notNull(b,"Advisorbmustnotbenull");
intindex=indexOf(a);
if(index==-1){
returnfalse;
}
removeAdvisor(index);
addAdvisor(index,b);
returntrue;
}
对于Advice的操作:
@Override
publicvoidaddAdvice(Adviceadvice)throwsAopConfigException{
intpos=this.advisors.size();
addAdvice(pos,advice);
}
@Override
publicvoidaddAdvice(intpos,Adviceadvice)throwsAopConfigException{
Asse