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