001/* 002 * Copyright 2002-2019 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.aspectj.weaver.tools.PointcutParser; 020import org.aspectj.weaver.tools.TypePatternMatcher; 021 022import org.springframework.aop.ClassFilter; 023import org.springframework.lang.Nullable; 024import org.springframework.util.Assert; 025import org.springframework.util.ObjectUtils; 026import org.springframework.util.StringUtils; 027 028/** 029 * Spring AOP {@link ClassFilter} implementation using AspectJ type matching. 030 * 031 * @author Rod Johnson 032 * @author Juergen Hoeller 033 * @author Sam Brannen 034 * @since 2.0 035 */ 036public class TypePatternClassFilter implements ClassFilter { 037 038 private String typePattern = ""; 039 040 @Nullable 041 private TypePatternMatcher aspectJTypePatternMatcher; 042 043 044 /** 045 * Creates a new instance of the {@link TypePatternClassFilter} class. 046 * <p>This is the JavaBean constructor; be sure to set the 047 * {@link #setTypePattern(String) typePattern} property, else a 048 * no doubt fatal {@link IllegalStateException} will be thrown 049 * when the {@link #matches(Class)} method is first invoked. 050 */ 051 public TypePatternClassFilter() { 052 } 053 054 /** 055 * Create a fully configured {@link TypePatternClassFilter} using the 056 * given type pattern. 057 * @param typePattern the type pattern that AspectJ weaver should parse 058 */ 059 public TypePatternClassFilter(String typePattern) { 060 setTypePattern(typePattern); 061 } 062 063 064 /** 065 * Set the AspectJ type pattern to match. 066 * <p>Examples include: 067 * <code class="code"> 068 * org.springframework.beans.* 069 * </code> 070 * This will match any class or interface in the given package. 071 * <code class="code"> 072 * org.springframework.beans.ITestBean+ 073 * </code> 074 * This will match the {@code ITestBean} interface and any class 075 * that implements it. 076 * <p>These conventions are established by AspectJ, not Spring AOP. 077 * @param typePattern the type pattern that AspectJ weaver should parse 078 */ 079 public void setTypePattern(String typePattern) { 080 Assert.notNull(typePattern, "Type pattern must not be null"); 081 this.typePattern = typePattern; 082 this.aspectJTypePatternMatcher = 083 PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingContextClassloaderForResolution(). 084 parseTypePattern(replaceBooleanOperators(typePattern)); 085 } 086 087 /** 088 * Return the AspectJ type pattern to match. 089 */ 090 public String getTypePattern() { 091 return this.typePattern; 092 } 093 094 095 /** 096 * Should the pointcut apply to the given interface or target class? 097 * @param clazz candidate target class 098 * @return whether the advice should apply to this candidate target class 099 * @throws IllegalStateException if no {@link #setTypePattern(String)} has been set 100 */ 101 @Override 102 public boolean matches(Class<?> clazz) { 103 Assert.state(this.aspectJTypePatternMatcher != null, "No type pattern has been set"); 104 return this.aspectJTypePatternMatcher.matches(clazz); 105 } 106 107 /** 108 * If a type pattern has been specified in XML, the user cannot 109 * write {@code and} as "&&" (though && will work). 110 * We also allow {@code and} between two sub-expressions. 111 * <p>This method converts back to {@code &&} for the AspectJ pointcut parser. 112 */ 113 private String replaceBooleanOperators(String pcExpr) { 114 String result = StringUtils.replace(pcExpr," and "," && "); 115 result = StringUtils.replace(result, " or ", " || "); 116 return StringUtils.replace(result, " not ", " ! "); 117 } 118 119 @Override 120 public boolean equals(Object other) { 121 return (this == other || (other instanceof TypePatternClassFilter && 122 ObjectUtils.nullSafeEquals(this.typePattern, ((TypePatternClassFilter) other).typePattern))); 123 } 124 125 @Override 126 public int hashCode() { 127 return ObjectUtils.nullSafeHashCode(this.typePattern); 128 } 129 130 @Override 131 public String toString() { 132 return getClass().getName() + ": " + this.typePattern; 133 } 134 135}