1/* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2014 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.*; 25import proguard.classfile.attribute.annotation.*; 26import proguard.classfile.attribute.visitor.AttributeVisitor; 27import proguard.classfile.util.SimplifiedVisitor; 28 29/** 30 * This AttributeVisitor and AnnotationVisitor lets a given ElementValueVisitor 31 * visit all ElementValue objects of the attributes or annotations that it 32 * visits. 33 * 34 * @author Eric Lafortune 35 */ 36public class AllElementValueVisitor 37extends SimplifiedVisitor 38implements AttributeVisitor, 39 AnnotationVisitor, 40 ElementValueVisitor 41{ 42 private final boolean deep; 43 private final ElementValueVisitor elementValueVisitor; 44 45 46 /** 47 * Creates a new AllElementValueVisitor. 48 * @param elementValueVisitor the AllElementValueVisitor to which visits 49 * will be delegated. 50 */ 51 public AllElementValueVisitor(ElementValueVisitor elementValueVisitor) 52 { 53 this(false, elementValueVisitor); 54 } 55 56 57 /** 58 * Creates a new AllElementValueVisitor. 59 * @param deep specifies whether the element values 60 * further down the hierarchy should be 61 * visited too. 62 * @param elementValueVisitor the AllElementValueVisitor to which visits 63 * will be delegated. 64 */ 65 public AllElementValueVisitor(boolean deep, 66 ElementValueVisitor elementValueVisitor) 67 { 68 this.deep = deep; 69 this.elementValueVisitor = elementValueVisitor; 70 } 71 72 73 // Implementations for AttributeVisitor. 74 75 public void visitAnyAttribute(Clazz clazz, Attribute attribute) {} 76 77 78 public void visitRuntimeVisibleAnnotationsAttribute(Clazz clazz, RuntimeVisibleAnnotationsAttribute runtimeVisibleAnnotationsAttribute) 79 { 80 // Visit the annotations. 81 runtimeVisibleAnnotationsAttribute.annotationsAccept(clazz, this); 82 } 83 84 85 public void visitRuntimeVisibleAnnotationsAttribute(Clazz clazz, Field field, RuntimeVisibleAnnotationsAttribute runtimeVisibleAnnotationsAttribute) 86 { 87 // Visit the annotations. 88 runtimeVisibleAnnotationsAttribute.annotationsAccept(clazz, field, this); 89 } 90 91 92 public void visitRuntimeVisibleAnnotationsAttribute(Clazz clazz, Method method, RuntimeVisibleAnnotationsAttribute runtimeVisibleAnnotationsAttribute) 93 { 94 // Visit the annotations. 95 runtimeVisibleAnnotationsAttribute.annotationsAccept(clazz, method, this); 96 } 97 98 99 public void visitRuntimeInvisibleAnnotationsAttribute(Clazz clazz, RuntimeInvisibleAnnotationsAttribute runtimeInvisibleAnnotationsAttribute) 100 { 101 // Visit the annotations. 102 runtimeInvisibleAnnotationsAttribute.annotationsAccept(clazz, this); 103 } 104 105 106 public void visitRuntimeInvisibleAnnotationsAttribute(Clazz clazz, Field field, RuntimeInvisibleAnnotationsAttribute runtimeInvisibleAnnotationsAttribute) 107 { 108 // Visit the annotations. 109 runtimeInvisibleAnnotationsAttribute.annotationsAccept(clazz, field, this); 110 } 111 112 113 public void visitRuntimeInvisibleAnnotationsAttribute(Clazz clazz, Method method, RuntimeInvisibleAnnotationsAttribute runtimeInvisibleAnnotationsAttribute) 114 { 115 // Visit the annotations. 116 runtimeInvisibleAnnotationsAttribute.annotationsAccept(clazz, method, this); 117 } 118 119 120 public void visitAnyParameterAnnotationsAttribute(Clazz clazz, Method method, ParameterAnnotationsAttribute parameterAnnotationsAttribute) 121 { 122 // Visit the annotations. 123 parameterAnnotationsAttribute.annotationsAccept(clazz, method, this); 124 } 125 126 127 public void visitAnnotationDefaultAttribute(Clazz clazz, Method method, AnnotationDefaultAttribute annotationDefaultAttribute) 128 { 129 // Visit the default element value. 130 annotationDefaultAttribute.defaultValueAccept(clazz, this); 131 } 132 133 134 // Implementations for AnnotationVisitor. 135 136 public void visitAnnotation(Clazz clazz, Annotation annotation) 137 { 138 annotation.elementValuesAccept(clazz, this); 139 } 140 141 142 public void visitAnnotation(Clazz clazz, Field field, Annotation annotation) 143 { 144 annotation.elementValuesAccept(clazz, this); 145 } 146 147 148 public void visitAnnotation(Clazz clazz, Method method, Annotation annotation) 149 { 150 annotation.elementValuesAccept(clazz, this); 151 } 152 153 154 public void visitAnnotation(Clazz clazz, Method method, int parameterIndex, Annotation annotation) 155 { 156 annotation.elementValuesAccept(clazz, this); 157 } 158 159 160 public void visitAnnotation(Clazz clazz, Method method, CodeAttribute codeAttribute, Annotation annotation) 161 { 162 annotation.elementValuesAccept(clazz, this); 163 } 164 165 166 // Implementations for ElementValueVisitor. 167 168 public void visitConstantElementValue(Clazz clazz, Annotation annotation, ConstantElementValue constantElementValue) 169 { 170 elementValueVisitor.visitConstantElementValue(clazz, annotation, constantElementValue); 171 } 172 173 174 public void visitEnumConstantElementValue(Clazz clazz, Annotation annotation, EnumConstantElementValue enumConstantElementValue) 175 { 176 elementValueVisitor.visitEnumConstantElementValue(clazz, annotation, enumConstantElementValue); 177 } 178 179 180 public void visitClassElementValue(Clazz clazz, Annotation annotation, ClassElementValue classElementValue) 181 { 182 elementValueVisitor.visitClassElementValue(clazz, annotation, classElementValue); 183 } 184 185 186 public void visitAnnotationElementValue(Clazz clazz, Annotation annotation, AnnotationElementValue annotationElementValue) 187 { 188 elementValueVisitor.visitAnnotationElementValue(clazz, annotation, annotationElementValue); 189 190 if (deep) 191 { 192 annotationElementValue.annotationAccept(clazz, this); 193 } 194 } 195 196 197 public void visitArrayElementValue(Clazz clazz, Annotation annotation, ArrayElementValue arrayElementValue) 198 { 199 elementValueVisitor.visitArrayElementValue(clazz, annotation, arrayElementValue); 200 201 if (deep) 202 { 203 arrayElementValue.elementValuesAccept(clazz, annotation, elementValueVisitor); 204 } 205 } 206} 207