001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.aop.aspectj; 018 019import org.aopalliance.aop.Advice; 020 021import org.springframework.aop.Pointcut; 022import org.springframework.aop.PointcutAdvisor; 023import org.springframework.core.Ordered; 024import org.springframework.lang.Nullable; 025import org.springframework.util.Assert; 026 027/** 028 * AspectJPointcutAdvisor that adapts an {@link AbstractAspectJAdvice} 029 * to the {@link org.springframework.aop.PointcutAdvisor} interface. 030 * 031 * @author Adrian Colyer 032 * @author Juergen Hoeller 033 * @since 2.0 034 */ 035public class AspectJPointcutAdvisor implements PointcutAdvisor, Ordered { 036 037 private final AbstractAspectJAdvice advice; 038 039 private final Pointcut pointcut; 040 041 @Nullable 042 private Integer order; 043 044 045 /** 046 * Create a new AspectJPointcutAdvisor for the given advice. 047 * @param advice the AbstractAspectJAdvice to wrap 048 */ 049 public AspectJPointcutAdvisor(AbstractAspectJAdvice advice) { 050 Assert.notNull(advice, "Advice must not be null"); 051 this.advice = advice; 052 this.pointcut = advice.buildSafePointcut(); 053 } 054 055 056 public void setOrder(int order) { 057 this.order = order; 058 } 059 060 @Override 061 public int getOrder() { 062 if (this.order != null) { 063 return this.order; 064 } 065 else { 066 return this.advice.getOrder(); 067 } 068 } 069 070 @Override 071 public boolean isPerInstance() { 072 return true; 073 } 074 075 @Override 076 public Advice getAdvice() { 077 return this.advice; 078 } 079 080 @Override 081 public Pointcut getPointcut() { 082 return this.pointcut; 083 } 084 085 /** 086 * Return the name of the aspect (bean) in which the advice was declared. 087 * @since 4.3.15 088 * @see AbstractAspectJAdvice#getAspectName() 089 */ 090 public String getAspectName() { 091 return this.advice.getAspectName(); 092 } 093 094 095 @Override 096 public boolean equals(@Nullable Object other) { 097 if (this == other) { 098 return true; 099 } 100 if (!(other instanceof AspectJPointcutAdvisor)) { 101 return false; 102 } 103 AspectJPointcutAdvisor otherAdvisor = (AspectJPointcutAdvisor) other; 104 return this.advice.equals(otherAdvisor.advice); 105 } 106 107 @Override 108 public int hashCode() { 109 return AspectJPointcutAdvisor.class.hashCode() * 29 + this.advice.hashCode(); 110 } 111 112}