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