1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * This file is available under and governed by the GNU General Public
  27  * License version 2 only, as published by the Free Software Foundation.
  28  * However, the following notice accompanied the original version of this
  29  * file:
  30  *
  31  * ASM: a very small and fast Java bytecode manipulation framework
  32  * Copyright (c) 2000-2011 INRIA, France Telecom
  33  * All rights reserved.
  34  *
  35  * Redistribution and use in source and binary forms, with or without
  36  * modification, are permitted provided that the following conditions
  37  * are met:
  38  * 1. Redistributions of source code must retain the above copyright
  39  *    notice, this list of conditions and the following disclaimer.
  40  * 2. Redistributions in binary form must reproduce the above copyright
  41  *    notice, this list of conditions and the following disclaimer in the
  42  *    documentation and/or other materials provided with the distribution.
  43  * 3. Neither the name of the copyright holders nor the names of its
  44  *    contributors may be used to endorse or promote products derived from
  45  *    this software without specific prior written permission.
  46  *
  47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  57  * THE POSSIBILITY OF SUCH DAMAGE.
  58  */
  59 package jdk.internal.org.objectweb.asm;
  60 
  61 /**
  62  * A visitor to visit a Java class. The methods of this class must be called
  63  * in the following order: <tt>visit</tt> [ <tt>visitSource</tt> ] [
  64  * <tt>visitOuterClass</tt> ] ( <tt>visitAnnotation</tt> |
  65  * <tt>visitAttribute</tt> )* ( <tt>visitInnerClass</tt> |
  66  * <tt>visitField</tt> | <tt>visitMethod</tt> )* <tt>visitEnd</tt>.
  67  *
  68  * @author Eric Bruneton
  69  */
  70 public abstract class ClassVisitor {
  71 
  72     /**
  73      * The ASM API version implemented by this visitor. The value of this field
  74      * must be one of {@link Opcodes#ASM4}.
  75      */
  76     protected final int api;
  77 
  78     /**
  79      * The class visitor to which this visitor must delegate method calls. May
  80      * be null.
  81      */
  82     protected ClassVisitor cv;
  83 
  84     /**
  85      * Constructs a new {@link ClassVisitor}.
  86      *
  87      * @param api the ASM API version implemented by this visitor. Must be one
  88      *        of {@link Opcodes#ASM4}.
  89      */
  90     public ClassVisitor(final int api) {
  91         this(api, null);
  92     }
  93 
  94     /**
  95      * Constructs a new {@link ClassVisitor}.
  96      *
  97      * @param api the ASM API version implemented by this visitor. Must be one
  98      *        of {@link Opcodes#ASM4}.
  99      * @param cv the class visitor to which this visitor must delegate method
 100      *        calls. May be null.
 101      */
 102     public ClassVisitor(final int api, final ClassVisitor cv) {
 103         /*if (api != Opcodes.ASM4) {
 104             throw new IllegalArgumentException();
 105         }*/
 106         this.api = api;
 107         this.cv = cv;
 108     }
 109 
 110     /**
 111      * Visits the header of the class.
 112      *
 113      * @param version the class version.
 114      * @param access the class's access flags (see {@link Opcodes}). This
 115      *        parameter also indicates if the class is deprecated.
 116      * @param name the internal name of the class (see
 117      *        {@link Type#getInternalName() getInternalName}).
 118      * @param signature the signature of this class. May be <tt>null</tt> if
 119      *        the class is not a generic one, and does not extend or implement
 120      *        generic classes or interfaces.
 121      * @param superName the internal of name of the super class (see
 122      *        {@link Type#getInternalName() getInternalName}). For interfaces,
 123      *        the super class is {@link Object}. May be <tt>null</tt>, but
 124      *        only for the {@link Object} class.
 125      * @param interfaces the internal names of the class's interfaces (see
 126      *        {@link Type#getInternalName() getInternalName}). May be
 127      *        <tt>null</tt>.
 128      */
 129     public void visit(
 130         int version,
 131         int access,
 132         String name,
 133         String signature,
 134         String superName,
 135         String[] interfaces)
 136     {
 137         if (cv != null) {
 138             cv.visit(version, access, name, signature, superName, interfaces);
 139         }
 140     }
 141 
 142     /**
 143      * Visits the source of the class.
 144      *
 145      * @param source the name of the source file from which the class was
 146      *        compiled. May be <tt>null</tt>.
 147      * @param debug additional debug information to compute the correspondance
 148      *        between source and compiled elements of the class. May be
 149      *        <tt>null</tt>.
 150      */
 151     public void visitSource(String source, String debug) {
 152         if (cv != null) {
 153             cv.visitSource(source, debug);
 154         }
 155     }
 156 
 157     /**
 158      * Visits the enclosing class of the class. This method must be called only
 159      * if the class has an 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
 163      *        <tt>null</tt> if the class is not enclosed in a method of its
 164      *        enclosing class.
 165      * @param desc the descriptor of the method that contains the class, or
 166      *        <tt>null</tt> if the class is not enclosed in a method of its
 167      *        enclosing class.
 168      */
 169     public void visitOuterClass(String owner, String name, String desc) {
 170         if (cv != null)  {
 171             cv.visitOuterClass(owner, name, desc);
 172         }
 173     }
 174 
 175     /**
 176      * Visits an annotation of the class.
 177      *
 178      * @param desc the class descriptor of the annotation class.
 179      * @param visible <tt>true</tt> if the annotation is visible at runtime.
 180      * @return a visitor to visit the annotation values, or <tt>null</tt> if
 181      *         this visitor is not interested in visiting this annotation.
 182      */
 183     public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
 184         if (cv != null) {
 185             return cv.visitAnnotation(desc, visible);
 186         }
 187         return null;
 188     }
 189 
 190     /**
 191      * Visits a non standard attribute of the class.
 192      *
 193      * @param attr an attribute.
 194      */
 195     public void visitAttribute(Attribute attr) {
 196         if (cv != null) {
 197             cv.visitAttribute(attr);
 198         }
 199     }
 200 
 201     /**
 202      * Visits information about an inner class. This inner class is not
 203      * necessarily a member of the class being visited.
 204      *
 205      * @param name the internal name of an inner class (see
 206      *        {@link Type#getInternalName() getInternalName}).
 207      * @param outerName the internal name of the class to which the inner class
 208      *        belongs (see {@link Type#getInternalName() getInternalName}). May
 209      *        be <tt>null</tt> for not member classes.
 210      * @param innerName the (simple) name of the inner class inside its
 211      *        enclosing class. May be <tt>null</tt> for anonymous inner
 212      *        classes.
 213      * @param access the access flags of the inner class as originally declared
 214      *        in the enclosing class.
 215      */
 216     public void visitInnerClass(
 217         String name,
 218         String outerName,
 219         String innerName,
 220         int access)
 221     {
 222         if (cv != null) {
 223             cv.visitInnerClass(name, outerName, innerName, access);
 224         }
 225     }
 226 
 227     /**
 228      * Visits a field of the class.
 229      *
 230      * @param access the field's access flags (see {@link Opcodes}). This
 231      *        parameter also indicates if the field is synthetic and/or
 232      *        deprecated.
 233      * @param name the field's name.
 234      * @param desc the field's descriptor (see {@link Type Type}).
 235      * @param signature the field's signature. May be <tt>null</tt> if the
 236      *        field's type does not use generic types.
 237      * @param value the field's initial value. This parameter, which may be
 238      *        <tt>null</tt> if the field does not have an initial value, must
 239      *        be an {@link Integer}, a {@link Float}, a {@link Long}, a
 240      *        {@link Double} or a {@link String} (for <tt>int</tt>,
 241      *        <tt>float</tt>, <tt>long</tt> or <tt>String</tt> fields
 242      *        respectively). <i>This parameter is only used for static fields</i>.
 243      *        Its value is ignored for non static fields, which must be
 244      *        initialized through bytecode instructions in constructors or
 245      *        methods.
 246      * @return a visitor to visit field annotations and attributes, or
 247      *         <tt>null</tt> if this class visitor is not interested in
 248      *         visiting these annotations and attributes.
 249      */
 250     public FieldVisitor visitField(
 251         int access,
 252         String name,
 253         String desc,
 254         String signature,
 255         Object value)
 256     {
 257         if (cv != null) {
 258             return cv.visitField(access, name, desc, signature, value);
 259         }
 260         return null;
 261     }
 262 
 263     /**
 264      * Visits a method of the class. This method <i>must</i> return a new
 265      * {@link MethodVisitor} instance (or <tt>null</tt>) each time it is
 266      * called, i.e., it should not return a previously returned visitor.
 267      *
 268      * @param access the method's access flags (see {@link Opcodes}). This
 269      *        parameter also indicates if the method is synthetic and/or
 270      *        deprecated.
 271      * @param name the method's name.
 272      * @param desc the method's descriptor (see {@link Type Type}).
 273      * @param signature the method's signature. May be <tt>null</tt> if the
 274      *        method parameters, return type and exceptions do not use generic
 275      *        types.
 276      * @param exceptions the internal names of the method's exception classes
 277      *        (see {@link Type#getInternalName() getInternalName}). May be
 278      *        <tt>null</tt>.
 279      * @return an object to visit the byte code of the method, or <tt>null</tt>
 280      *         if this class visitor is not interested in visiting the code of
 281      *         this method.
 282      */
 283     public MethodVisitor visitMethod(
 284         int access,
 285         String name,
 286         String desc,
 287         String signature,
 288         String[] exceptions)
 289     {
 290         if (cv != null) {
 291             return cv.visitMethod(access, name, desc, signature, exceptions);
 292         }
 293         return null;
 294     }
 295 
 296     /**
 297      * Visits the end of the class. This method, which is the last one to be
 298      * called, is used to inform the visitor that all the fields and methods of
 299      * the class have been visited.
 300      */
 301     public void visitEnd() {
 302         if (cv != null) {
 303             cv.visitEnd();
 304         }
 305     }
 306 }