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 * visitPermittedSubclass} ] | {@code 035 * visitInnerClass} | {@code visitRecordComponent} | {@code visitField} | {@code visitMethod} )* 036 * {@code visitEnd}. 037 * 038 * @author Eric Bruneton 039 */ 040public abstract class ClassVisitor { 041 042 /** 043 * The ASM API version implemented by this visitor. The value of this field must be one of the 044 * {@code ASM}<i>x</i> values in {@link Opcodes}. 045 */ 046 protected final int api; 047 048 /** The class visitor to which this visitor must delegate method calls. May be {@literal null}. */ 049 protected ClassVisitor cv; 050 051 /** 052 * Constructs a new {@link ClassVisitor}. 053 * 054 * @param api the ASM API version implemented by this visitor. Must be one of the {@code 055 * ASM}<i>x</i> values in {@link Opcodes}. 056 */ 057 protected ClassVisitor(final int api) { 058 this(api, null); 059 } 060 061 /** 062 * Constructs a new {@link ClassVisitor}. 063 * 064 * @param api the ASM API version implemented by this visitor. Must be one of the {@code 065 * ASM}<i>x</i> values in {@link Opcodes}. 066 * @param classVisitor the class visitor to which this visitor must delegate method calls. May be 067 * null. 068 */ 069 protected ClassVisitor(final int api, final ClassVisitor classVisitor) { 070 if (api != Opcodes.ASM9 071 && api != Opcodes.ASM8 072 && api != Opcodes.ASM7 073 && api != Opcodes.ASM6 074 && api != Opcodes.ASM5 075 && api != Opcodes.ASM4 076 && api != Opcodes.ASM10_EXPERIMENTAL) { 077 throw new IllegalArgumentException("Unsupported api " + api); 078 } 079 if (api == Opcodes.ASM10_EXPERIMENTAL) { 080 Constants.checkAsmExperimental(this); 081 } 082 this.api = api; 083 this.cv = classVisitor; 084 } 085 086 /** 087 * The class visitor to which this visitor must delegate method calls. May be {@literal null}. 088 * 089 * @return the class visitor to which this visitor must delegate method calls, or {@literal null}. 090 */ 091 public ClassVisitor getDelegate() { 092 return cv; 093 } 094 095 /** 096 * Visits the header of the class. 097 * 098 * @param version the class version. The minor version is stored in the 16 most significant bits, 099 * and the major version in the 16 least significant bits. 100 * @param access the class's access flags (see {@link Opcodes}). This parameter also indicates if 101 * the class is deprecated {@link Opcodes#ACC_DEPRECATED} or a record {@link 102 * Opcodes#ACC_RECORD}. 103 * @param name the internal name of the class (see {@link Type#getInternalName()}). 104 * @param signature the signature of this class. May be {@literal null} if the class is not a 105 * generic one, and does not extend or implement generic classes or interfaces. 106 * @param superName the internal of name of the super class (see {@link Type#getInternalName()}). 107 * For interfaces, the super class is {@link Object}. May be {@literal null}, but only for the 108 * {@link Object} class. 109 * @param interfaces the internal names of the class's interfaces (see {@link 110 * Type#getInternalName()}). May be {@literal null}. 111 */ 112 public void visit( 113 final int version, 114 final int access, 115 final String name, 116 final String signature, 117 final String superName, 118 final String[] interfaces) { 119 if (api < Opcodes.ASM8 && (access & Opcodes.ACC_RECORD) != 0) { 120 throw new UnsupportedOperationException("Records requires ASM8"); 121 } 122 if (cv != null) { 123 cv.visit(version, access, name, signature, superName, interfaces); 124 } 125 } 126 127 /** 128 * Visits the source of the class. 129 * 130 * @param source the name of the source file from which the class was compiled. May be {@literal 131 * null}. 132 * @param debug additional debug information to compute the correspondence between source and 133 * compiled elements of the class. May be {@literal null}. 134 */ 135 public void visitSource(final String source, final String debug) { 136 if (cv != null) { 137 cv.visitSource(source, debug); 138 } 139 } 140 141 /** 142 * Visit the module corresponding to the class. 143 * 144 * @param name the fully qualified name (using dots) of the module. 145 * @param access the module access flags, among {@code ACC_OPEN}, {@code ACC_SYNTHETIC} and {@code 146 * ACC_MANDATED}. 147 * @param version the module version, or {@literal null}. 148 * @return a visitor to visit the module values, or {@literal null} if this visitor is not 149 * interested in visiting this module. 150 */ 151 public ModuleVisitor visitModule(final String name, final int access, final String version) { 152 if (api < Opcodes.ASM6) { 153 throw new UnsupportedOperationException("Module requires ASM6"); 154 } 155 if (cv != null) { 156 return cv.visitModule(name, access, version); 157 } 158 return null; 159 } 160 161 /** 162 * Visits the nest host class of the class. A nest is a set of classes of the same package that 163 * share access to their private members. One of these classes, called the host, lists the other 164 * members of the nest, which in turn should link to the host of their nest. This method must be 165 * called only once and only if the visited class is a non-host member of a nest. A class is 166 * implicitly its own nest, so it's invalid to call this method with the visited class name as 167 * argument. 168 * 169 * @param nestHost the internal name of the host class of the nest (see {@link 170 * Type#getInternalName()}). 171 */ 172 public void visitNestHost(final String nestHost) { 173 if (api < Opcodes.ASM7) { 174 throw new UnsupportedOperationException("NestHost requires ASM7"); 175 } 176 if (cv != null) { 177 cv.visitNestHost(nestHost); 178 } 179 } 180 181 /** 182 * Visits the enclosing class of the class. This method must be called only if this class is a 183 * local or anonymous class. See the JVMS 4.7.7 section for more details. 184 * 185 * @param owner internal name of the enclosing class of the class (see {@link 186 * Type#getInternalName()}). 187 * @param name the name of the method that contains the class, or {@literal null} if the class is 188 * not enclosed in a method or constructor of its enclosing class (e.g. if it is enclosed in 189 * an instance initializer, static initializer, instance variable initializer, or class 190 * variable initializer). 191 * @param descriptor the descriptor of the method that contains the class, or {@literal null} if 192 * the class is not enclosed in a method or constructor of its enclosing class (e.g. if it is 193 * enclosed in an instance initializer, static initializer, instance variable initializer, or 194 * class variable initializer). 195 */ 196 public void visitOuterClass(final String owner, final String name, final String descriptor) { 197 if (cv != null) { 198 cv.visitOuterClass(owner, name, descriptor); 199 } 200 } 201 202 /** 203 * Visits an annotation of the class. 204 * 205 * @param descriptor the class descriptor of the annotation class. 206 * @param visible {@literal true} if the annotation is visible at runtime. 207 * @return a visitor to visit the annotation values, or {@literal null} if this visitor is not 208 * interested in visiting this annotation. 209 */ 210 public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) { 211 if (cv != null) { 212 return cv.visitAnnotation(descriptor, visible); 213 } 214 return null; 215 } 216 217 /** 218 * Visits an annotation on a type in the class signature. 219 * 220 * @param typeRef a reference to the annotated type. The sort of this type reference must be 221 * {@link TypeReference#CLASS_TYPE_PARAMETER}, {@link 222 * TypeReference#CLASS_TYPE_PARAMETER_BOUND} or {@link TypeReference#CLASS_EXTENDS}. See 223 * {@link TypeReference}. 224 * @param typePath the path to the annotated type argument, wildcard bound, array element type, or 225 * static inner type within 'typeRef'. May be {@literal null} if the annotation targets 226 * 'typeRef' as a whole. 227 * @param descriptor the class descriptor of the annotation class. 228 * @param visible {@literal true} if the annotation is visible at runtime. 229 * @return a visitor to visit the annotation values, or {@literal null} if this visitor is not 230 * interested in visiting this annotation. 231 */ 232 public AnnotationVisitor visitTypeAnnotation( 233 final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) { 234 if (api < Opcodes.ASM5) { 235 throw new UnsupportedOperationException("TypeAnnotation requires ASM5"); 236 } 237 if (cv != null) { 238 return cv.visitTypeAnnotation(typeRef, typePath, descriptor, visible); 239 } 240 return null; 241 } 242 243 /** 244 * Visits a non standard attribute of the class. 245 * 246 * @param attribute an attribute. 247 */ 248 public void visitAttribute(final Attribute attribute) { 249 if (cv != null) { 250 cv.visitAttribute(attribute); 251 } 252 } 253 254 /** 255 * Visits a member of the nest. A nest is a set of classes of the same package that share access 256 * to their private members. One of these classes, called the host, lists the other members of the 257 * nest, which in turn should link to the host of their nest. This method must be called only if 258 * the visited class is the host of a nest. A nest host is implicitly a member of its own nest, so 259 * it's invalid to call this method with the visited class name as argument. 260 * 261 * @param nestMember the internal name of a nest member (see {@link Type#getInternalName()}). 262 */ 263 public void visitNestMember(final String nestMember) { 264 if (api < Opcodes.ASM7) { 265 throw new UnsupportedOperationException("NestMember requires ASM7"); 266 } 267 if (cv != null) { 268 cv.visitNestMember(nestMember); 269 } 270 } 271 272 /** 273 * Visits a permitted subclasses. A permitted subclass is one of the allowed subclasses of the 274 * current class. 275 * 276 * @param permittedSubclass the internal name of a permitted subclass (see {@link 277 * Type#getInternalName()}). 278 */ 279 public void visitPermittedSubclass(final String permittedSubclass) { 280 if (api < Opcodes.ASM9) { 281 throw new UnsupportedOperationException("PermittedSubclasses requires ASM9"); 282 } 283 if (cv != null) { 284 cv.visitPermittedSubclass(permittedSubclass); 285 } 286 } 287 288 /** 289 * Visits information about an inner class. This inner class is not necessarily a member of the 290 * class being visited. More precisely, every class or interface C which is referenced by this 291 * class and which is not a package member must be visited with this method. This class must 292 * reference its nested class or interface members, and its enclosing class, if any. See the JVMS 293 * 4.7.6 section for more details. 294 * 295 * @param name the internal name of C (see {@link Type#getInternalName()}). 296 * @param outerName the internal name of the class or interface C is a member of (see {@link 297 * Type#getInternalName()}). Must be {@literal null} if C is not the member of a class or 298 * interface (e.g. for local or anonymous classes). 299 * @param innerName the (simple) name of C. Must be {@literal null} for anonymous inner classes. 300 * @param access the access flags of C originally declared in the source code from which this 301 * class was compiled. 302 */ 303 public void visitInnerClass( 304 final String name, final String outerName, final String innerName, final int access) { 305 if (cv != null) { 306 cv.visitInnerClass(name, outerName, innerName, access); 307 } 308 } 309 310 /** 311 * Visits a record component of the class. 312 * 313 * @param name the record component name. 314 * @param descriptor the record component descriptor (see {@link Type}). 315 * @param signature the record component signature. May be {@literal null} if the record component 316 * type does not use generic types. 317 * @return a visitor to visit this record component annotations and attributes, or {@literal null} 318 * if this class visitor is not interested in visiting these annotations and attributes. 319 */ 320 public RecordComponentVisitor visitRecordComponent( 321 final String name, final String descriptor, final String signature) { 322 if (api < Opcodes.ASM8) { 323 throw new UnsupportedOperationException("Record requires ASM8"); 324 } 325 if (cv != null) { 326 return cv.visitRecordComponent(name, descriptor, signature); 327 } 328 return null; 329 } 330 331 /** 332 * Visits a field of the class. 333 * 334 * @param access the field's access flags (see {@link Opcodes}). This parameter also indicates if 335 * the field is synthetic and/or deprecated. 336 * @param name the field's name. 337 * @param descriptor the field's descriptor (see {@link Type}). 338 * @param signature the field's signature. May be {@literal null} if the field's type does not use 339 * generic types. 340 * @param value the field's initial value. This parameter, which may be {@literal null} if the 341 * field does not have an initial value, must be an {@link Integer}, a {@link Float}, a {@link 342 * Long}, a {@link Double} or a {@link String} (for {@code int}, {@code float}, {@code long} 343 * or {@code String} fields respectively). <i>This parameter is only used for static 344 * fields</i>. Its value is ignored for non static fields, which must be initialized through 345 * bytecode instructions in constructors or methods. 346 * @return a visitor to visit field annotations and attributes, or {@literal null} if this class 347 * visitor is not interested in visiting these annotations and attributes. 348 */ 349 public FieldVisitor visitField( 350 final int access, 351 final String name, 352 final String descriptor, 353 final String signature, 354 final Object value) { 355 if (cv != null) { 356 return cv.visitField(access, name, descriptor, signature, value); 357 } 358 return null; 359 } 360 361 /** 362 * Visits a method of the class. This method <i>must</i> return a new {@link MethodVisitor} 363 * instance (or {@literal null}) each time it is called, i.e., it should not return a previously 364 * returned visitor. 365 * 366 * @param access the method's access flags (see {@link Opcodes}). This parameter also indicates if 367 * the method is synthetic and/or deprecated. 368 * @param name the method's name. 369 * @param descriptor the method's descriptor (see {@link Type}). 370 * @param signature the method's signature. May be {@literal null} if the method parameters, 371 * return type and exceptions do not use generic types. 372 * @param exceptions the internal names of the method's exception classes (see {@link 373 * Type#getInternalName()}). May be {@literal null}. 374 * @return an object to visit the byte code of the method, or {@literal null} if this class 375 * visitor is not interested in visiting the code of this method. 376 */ 377 public MethodVisitor visitMethod( 378 final int access, 379 final String name, 380 final String descriptor, 381 final String signature, 382 final String[] exceptions) { 383 if (cv != null) { 384 return cv.visitMethod(access, name, descriptor, signature, exceptions); 385 } 386 return null; 387 } 388 389 /** 390 * Visits the end of the class. This method, which is the last one to be called, is used to inform 391 * the visitor that all the fields and methods of the class have been visited. 392 */ 393 public void visitEnd() { 394 if (cv != null) { 395 cv.visitEnd(); 396 } 397 } 398}