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