1/* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2013 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 36implements AnnotationVisitor 37{ 38 private final StringMatcher regularExpressionMatcher; 39 private final AnnotationVisitor annotationVisitor; 40 41 42 /** 43 * Creates a new ClassNameFilter. 44 * @param regularExpression the regular expression against which annotation 45 * type names will be matched. 46 * @param annotationVisitor the <code>annotationVisitor</code> to which 47 * visits will be delegated. 48 */ 49 public AnnotationTypeFilter(String regularExpression, 50 AnnotationVisitor annotationVisitor) 51 { 52 this.regularExpressionMatcher = new ListParser(new ClassNameParser()).parse(regularExpression); 53 this.annotationVisitor = annotationVisitor; 54 } 55 56 57 // Implementations for AnnotationVisitor. 58 59 public void visitAnnotation(Clazz clazz, Annotation annotation) 60 { 61 if (accepted(annotation.getType(clazz))) 62 { 63 annotationVisitor.visitAnnotation(clazz, annotation); 64 } 65 } 66 67 68 public void visitAnnotation(Clazz clazz, Field field, Annotation annotation) 69 { 70 if (accepted(annotation.getType(clazz))) 71 { 72 annotationVisitor.visitAnnotation(clazz, field, annotation); 73 } 74 } 75 76 77 public void visitAnnotation(Clazz clazz, Method method, Annotation annotation) 78 { 79 if (accepted(annotation.getType(clazz))) 80 { 81 annotationVisitor.visitAnnotation(clazz, method, annotation); 82 } 83 } 84 85 86 public void visitAnnotation(Clazz clazz, Method method, int parameterIndex, Annotation annotation) 87 { 88 if (accepted(annotation.getType(clazz))) 89 { 90 annotationVisitor.visitAnnotation(clazz, method, parameterIndex, annotation); 91 } 92 } 93 94 95 // Small utility methods. 96 97 private boolean accepted(String name) 98 { 99 return regularExpressionMatcher.matches(name); 100 } 101} 102