001/* 002 * Copyright 2002-2014 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.core.type.filter; 018 019import java.io.IOException; 020 021import org.aspectj.bridge.IMessageHandler; 022import org.aspectj.weaver.ResolvedType; 023import org.aspectj.weaver.World; 024import org.aspectj.weaver.bcel.BcelWorld; 025import org.aspectj.weaver.patterns.Bindings; 026import org.aspectj.weaver.patterns.FormalBinding; 027import org.aspectj.weaver.patterns.IScope; 028import org.aspectj.weaver.patterns.PatternParser; 029import org.aspectj.weaver.patterns.SimpleScope; 030import org.aspectj.weaver.patterns.TypePattern; 031 032import org.springframework.core.type.classreading.MetadataReader; 033import org.springframework.core.type.classreading.MetadataReaderFactory; 034 035/** 036 * Type filter that uses AspectJ type pattern for matching. 037 * 038 * <p>A critical implementation details of this type filter is that it does not 039 * load the class being examined to match with a type pattern. 040 * 041 * @author Ramnivas Laddad 042 * @author Juergen Hoeller 043 * @since 2.5 044 */ 045public class AspectJTypeFilter implements TypeFilter { 046 047 private final World world; 048 049 private final TypePattern typePattern; 050 051 052 public AspectJTypeFilter(String typePatternExpression, ClassLoader classLoader) { 053 this.world = new BcelWorld(classLoader, IMessageHandler.THROW, null); 054 this.world.setBehaveInJava5Way(true); 055 PatternParser patternParser = new PatternParser(typePatternExpression); 056 TypePattern typePattern = patternParser.parseTypePattern(); 057 typePattern.resolve(this.world); 058 IScope scope = new SimpleScope(this.world, new FormalBinding[0]); 059 this.typePattern = typePattern.resolveBindings(scope, Bindings.NONE, false, false); 060 } 061 062 063 @Override 064 public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) 065 throws IOException { 066 067 String className = metadataReader.getClassMetadata().getClassName(); 068 ResolvedType resolvedType = this.world.resolve(className); 069 return this.typePattern.matchesStatically(resolvedType); 070 } 071 072}