AnnotationTypeFilter.java revision b72c5c2e5482cf10117b2b25f642f7616b2326c3
1/* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu) 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the Free 9 * Software Foundation; either version 2 of the License, or (at your option) 10 * any later version. 11 * 12 * This program is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 * more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21package proguard.classfile.attribute.annotation.visitor; 22 23import proguard.classfile.*; 24import proguard.classfile.attribute.annotation.Annotation; 25import proguard.classfile.util.SimplifiedVisitor; 26import proguard.util.*; 27 28/** 29 * This <code>AnnotationVisitor</code> delegates its visits to another given 30 * <code>AnnotationVisitor</code>, but only when the visited annotation has 31 * a type that matches a given regular expression. 32 * 33 * @author Eric Lafortune 34 */ 35public class AnnotationTypeFilter 36extends SimplifiedVisitor 37implements AnnotationVisitor 38{ 39 private final StringMatcher regularExpressionMatcher; 40 private final AnnotationVisitor annotationVisitor; 41 42 43 /** 44 * Creates a new ClassNameFilter. 45 * @param regularExpression the regular expression against which annotation 46 * type names will be matched. 47 * @param annotationVisitor the <code>annotationVisitor</code> to which 48 * visits will be delegated. 49 */ 50 public AnnotationTypeFilter(String regularExpression, 51 AnnotationVisitor annotationVisitor) 52 { 53 this.regularExpressionMatcher = new ListParser(new ClassNameParser()).parse(regularExpression); 54 this.annotationVisitor = annotationVisitor; 55 } 56 57 58 // Implementations for AnnotationVisitor. 59 60 public void visitAnnotation(Clazz clazz, Annotation annotation) 61 { 62 if (accepted(annotation.getType(clazz))) 63 { 64 annotationVisitor.visitAnnotation(clazz, annotation); 65 } 66 } 67 68 69 public void visitAnnotation(Clazz clazz, Field field, Annotation annotation) 70 { 71 if (accepted(annotation.getType(clazz))) 72 { 73 annotationVisitor.visitAnnotation(clazz, field, annotation); 74 } 75 } 76 77 78 public void visitAnnotation(Clazz clazz, Method method, Annotation annotation) 79 { 80 if (accepted(annotation.getType(clazz))) 81 { 82 annotationVisitor.visitAnnotation(clazz, method, annotation); 83 } 84 } 85 86 87 public void visitAnnotation(Clazz clazz, Method method, int parameterIndex, Annotation annotation) 88 { 89 if (accepted(annotation.getType(clazz))) 90 { 91 annotationVisitor.visitAnnotation(clazz, method, parameterIndex, annotation); 92 } 93 } 94 95 96 // Small utility methods. 97 98 private boolean accepted(String name) 99 { 100 return regularExpressionMatcher.matches(name); 101 } 102} 103