001// ASM: a very small and fast Java bytecode manipulation framework 002// Copyright (c) 2000-2011 INRIA, France Telecom 003// All rights reserved. 004// 005// Redistribution and use in source and binary forms, with or without 006// modification, are permitted provided that the following conditions 007// are met: 008// 1. Redistributions of source code must retain the above copyright 009// notice, this list of conditions and the following disclaimer. 010// 2. Redistributions in binary form must reproduce the above copyright 011// notice, this list of conditions and the following disclaimer in the 012// documentation and/or other materials provided with the distribution. 013// 3. Neither the name of the copyright holders nor the names of its 014// contributors may be used to endorse or promote products derived from 015// this software without specific prior written permission. 016// 017// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 018// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 019// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 020// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 021// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 022// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 023// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 024// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 025// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 026// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 027// THE POSSIBILITY OF SUCH DAMAGE. 028package io.ebean.enhance.asm; 029 030/** 031 * A visitor to visit a Java class. The methods of this class must be called in the following order: 032 * {@code visit} [ {@code visitSource} ] [ {@code visitModule} ][ {@code visitNestHost} ][ {@code 033 * visitOuterClass} ] ( {@code visitAnnotation} | {@code visitTypeAnnotation} | {@code 034 * visitAttribute} )* ( {@code visitNestMember} | {@code visitInnerClass} | {@code visitField} | 035 * {@code visitMethod} )* {@code visitEnd}. 036 * 037 * @author Eric Bruneton 038 */ 039public abstract class ClassVisitor { 040 041 /** 042 * The ASM API version implemented by this visitor. The value of this field must be one of {@link 043 * Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}. 044 */ 045 protected final int api; 046 047 /** The class visitor to which this visitor must delegate method calls. May be null. */ 048 protected ClassVisitor cv; 049 050 /** 051 * Constructs a new {@link ClassVisitor}. 052 * 053 * @param api the ASM API version implemented by this visitor. Must be one of {@link 054 * Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}. 055 */ 056 public ClassVisitor(final int api) { 057 this(api, null); 058 } 059 060 /** 061 * Constructs a new {@link ClassVisitor}. 062 * 063 * @param api the ASM API version implemented by this visitor. Must be one of {@link 064 * Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6} or {@link Opcodes#ASM7}. 065 * @param classVisitor the class visitor to which this visitor must delegate method calls. May be 066 * null. 067 */ 068 public ClassVisitor(final int api, final ClassVisitor classVisitor) { 069 if (api != Opcodes.ASM7 && api != Opcodes.ASM6 && api != Opcodes.ASM5 && api != Opcodes.ASM4) { 070 throw new IllegalArgumentException("Unsupported api " + api); 071 } 072 this.api = api; 073 this.cv = classVisitor; 074 } 075 076 /** 077 * Visits the header of the class. 078 * 079 * @param version the class version. The minor version is stored in the 16 most significant bits, 080 * and the major version in the 16 least significant bits. 081 * @param access the class's access flags (see {@link Opcodes}). This parameter also indicates if 082 * the class is deprecated. 083 * @param name the internal name of the class (see {@link Type#getInternalName()}). 084 * @param signature the signature of this class. May be {@literal null} if the class is not a 085 * generic one, and does not extend or implement generic classes or interfaces. 086 * @param superName the internal of name of the super class (see {@link Type#getInternalName()}). 087 * For interfaces, the super class is {@link Object}. May be {@literal null}, but only for the 088 * {@link Object} class. 089 * @param interfaces the internal names of the class's interfaces (see {@link 090 * Type#getInternalName()}). May be {@literal null}. 091 */ 092 public void visit( 093 final int version, 094 final int access, 095 final String name, 096 final String signature, 097 final String superName, 098 final String[] interfaces) { 099 if (cv != null) { 100 cv.visit(version, access, name, signature, superName, interfaces); 101 } 102 } 103 104 /** 105 * Visits the source of the class. 106 * 107 * @param source the name of the source file from which the class was compiled. May be {@literal 108 * null}. 109 * @param debug additional debug information to compute the correspondence between source and 110 * compiled elements of the class. May be {@literal null}. 111 */ 112 public void visitSource(final String source, final String debug) { 113 if (cv != null) { 114 cv.visitSource(source, debug); 115 } 116 } 117 118 /** 119 * Visit the module corresponding to the class. 120 * 121 * @param name the fully qualified name (using dots) of the module. 122 * @param access the module access flags, among {@code ACC_OPEN}, {@code ACC_SYNTHETIC} and {@code 123 * ACC_MANDATED}. 124 * @param version the module version, or {@literal null}. 125 * @return a visitor to visit the module values, or {@literal null} if this visitor is not 126 * interested in visiting this module. 127 */ 128 public ModuleVisitor visitModule(final String name, final int access, final String version) { 129 if (api < Opcodes.ASM6) { 130 throw new UnsupportedOperationException("This feature requires ASM6"); 131 } 132 if (cv != null) { 133 return cv.visitModule(name, access, version); 134 } 135 return null; 136 } 137 138 /** 139 * Visits the nest host class of the class. A nest is a set of classes of the same package that 140 * share access to their private members. One of these classes, called the host, lists the other 141 * members of the nest, which in turn should link to the host of their nest. This method must be 142 * called only once and only if the visited class is a non-host member of a nest. A class is 143 * implicitly its own nest, so it's invalid to call this method with the visited class name as 144 * argument. 145 * 146 * @param nestHost the internal name of the host class of the nest. 147 */ 148 public void visitNestHost(final String nestHost) { 149 if (api < Opcodes.ASM7) { 150 throw new UnsupportedOperationException("This feature requires ASM7"); 151 } 152 if (cv != null) { 153 cv.visitNestHost(nestHost); 154 } 155 } 156 157 /** 158 * Visits the enclosing class of the class. This method must be called only if the class has an 159 * enclosing class. 160 * 161 * @param owner internal name of the enclosing class of the class. 162 * @param name the name of the method that contains the class, or {@literal null} if the class is 163 * not enclosed in a method of its enclosing class. 164 * @param descriptor the descriptor of the method that contains the class, or {@literal null} if 165 * the class is not enclosed in a method of its enclosing class. 166 */ 167 public void visitOuterClass(final String owner, final String name, final String descriptor) { 168 if (cv != null) { 169 cv.visitOuterClass(owner, name, descriptor); 170 } 171 } 172 173 /** 174 * Visits an annotation of the class. 175 * 176 * @param descriptor the class descriptor of the annotation class. 177 * @param visible {@literal true} if the annotation is visible at runtime. 178 * @return a visitor to visit the annotation values, or {@literal null} if this visitor is not 179 * interested in visiting this annotation. 180 */ 181 public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) { 182 if (cv != null) { 183 return cv.visitAnnotation(descriptor, visible); 184 } 185 return null; 186 } 187 188 /** 189 * Visits an annotation on a type in the class signature. 190 * 191 * @param typeRef a reference to the annotated type. The sort of this type reference must be 192 * {@link TypeReference#CLASS_TYPE_PARAMETER}, {@link 193 * TypeReference#CLASS_TYPE_PARAMETER_BOUND} or {@link TypeReference#CLASS_EXTENDS}. See 194 * {@link TypeReference}. 195 * @param typePath the path to the annotated type argument, wildcard bound, array element type, or 196 * static inner type within 'typeRef'. May be {@literal null} if the annotation targets 197 * 'typeRef' as a whole. 198 * @param descriptor the class descriptor of the annotation class. 199 * @param visible {@literal true} if the annotation is visible at runtime. 200 * @return a visitor to visit the annotation values, or {@literal null} if this visitor is not 201 * interested in visiting this annotation. 202 */ 203 public AnnotationVisitor visitTypeAnnotation( 204 final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) { 205 if (api < Opcodes.ASM5) { 206 throw new UnsupportedOperationException("This feature requires ASM5"); 207 } 208 if (cv != null) { 209 return cv.visitTypeAnnotation(typeRef, typePath, descriptor, visible); 210 } 211 return null; 212 } 213 214 /** 215 * Visits a non standard attribute of the class. 216 * 217 * @param attribute an attribute. 218 */ 219 public void visitAttribute(final Attribute attribute) { 220 if (cv != null) { 221 cv.visitAttribute(attribute); 222 } 223 } 224 225 /** 226 * Visits a member of the nest. A nest is a set of classes of the same package that share access 227 * to their private members. One of these classes, called the host, lists the other members of the 228 * nest, which in turn should link to the host of their nest. This method must be called only if 229 * the visited class is the host of a nest. A nest host is implicitly a member of its own nest, so 230 * it's invalid to call this method with the visited class name as argument. 231 * 232 * @param nestMember the internal name of a nest member. 233 */ 234 public void visitNestMember(final String nestMember) { 235 if (api < Opcodes.ASM7) { 236 throw new UnsupportedOperationException("This feature requires ASM7"); 237 } 238 if (cv != null) { 239 cv.visitNestMember(nestMember); 240 } 241 } 242 243 /** 244 * Visits information about an inner class. This inner class is not necessarily a member of the 245 * class being visited. 246 * 247 * @param name the internal name of an inner class (see {@link Type#getInternalName()}). 248 * @param outerName the internal name of the class to which the inner class belongs (see {@link 249 * Type#getInternalName()}). May be {@literal null} for not member classes. 250 * @param innerName the (simple) name of the inner class inside its enclosing class. May be 251 * {@literal null} for anonymous inner classes. 252 * @param access the access flags of the inner class as originally declared in the enclosing 253 * class. 254 */ 255 public void visitInnerClass( 256 final String name, final String outerName, final String innerName, final int access) { 257 if (cv != null) { 258 cv.visitInnerClass(name, outerName, innerName, access); 259 } 260 } 261 262 /** 263 * Visits a field of the class. 264 * 265 * @param access the field's access flags (see {@link Opcodes}). This parameter also indicates if 266 * the field is synthetic and/or deprecated. 267 * @param name the field's name. 268 * @param descriptor the field's descriptor (see {@link Type}). 269 * @param signature the field's signature. May be {@literal null} if the field's type does not use 270 * generic types. 271 * @param value the field's initial value. This parameter, which may be {@literal null} if the 272 * field does not have an initial value, must be an {@link Integer}, a {@link Float}, a {@link 273 * Long}, a {@link Double} or a {@link String} (for {@code int}, {@code float}, {@code long} 274 * or {@code String} fields respectively). <i>This parameter is only used for static 275 * fields</i>. Its value is ignored for non static fields, which must be initialized through 276 * bytecode instructions in constructors or methods. 277 * @return a visitor to visit field annotations and attributes, or {@literal null} if this class 278 * visitor is not interested in visiting these annotations and attributes. 279 */ 280 public FieldVisitor visitField( 281 final int access, 282 final String name, 283 final String descriptor, 284 final String signature, 285 final Object value) { 286 if (cv != null) { 287 return cv.visitField(access, name, descriptor, signature, value); 288 } 289 return null; 290 } 291 292 /** 293 * Visits a method of the class. This method <i>must</i> return a new {@link MethodVisitor} 294 * instance (or {@literal null}) each time it is called, i.e., it should not return a previously 295 * returned visitor. 296 * 297 * @param access the method's access flags (see {@link Opcodes}). This parameter also indicates if 298 * the method is synthetic and/or deprecated. 299 * @param name the method's name. 300 * @param descriptor the method's descriptor (see {@link Type}). 301 * @param signature the method's signature. May be {@literal null} if the method parameters, 302 * return type and exceptions do not use generic types. 303 * @param exceptions the internal names of the method's exception classes (see {@link 304 * Type#getInternalName()}). May be {@literal null}. 305 * @return an object to visit the byte code of the method, or {@literal null} if this class 306 * visitor is not interested in visiting the code of this method. 307 */ 308 public MethodVisitor visitMethod( 309 final int access, 310 final String name, 311 final String descriptor, 312 final String signature, 313 final String[] exceptions) { 314 if (cv != null) { 315 return cv.visitMethod(access, name, descriptor, signature, exceptions); 316 } 317 return null; 318 } 319 320 /** 321 * Visits the end of the class. This method, which is the last one to be called, is used to inform 322 * the visitor that all the fields and methods of the class have been visited. 323 */ 324 public void visitEnd() { 325 if (cv != null) { 326 cv.visitEnd(); 327 } 328 } 329}