View Javadoc
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 annotation. The methods of this class must be
63   * called in the following order: ( <tt>visit</tt> | <tt>visitEnum</tt> |
64   * <tt>visitAnnotation</tt> | <tt>visitArray</tt> )* <tt>visitEnd</tt>.
65   *
66   * @author Eric Bruneton
67   * @author Eugene Kuleshov
68   */
69  public abstract class AnnotationVisitor {
70  
71      /**
72       * The ASM API version implemented by this visitor. The value of this field
73       * must be one of {@link Opcodes#ASM4} or {@link Opcodes#ASM5}.
74       */
75      protected final int api;
76  
77      /**
78       * The annotation visitor to which this visitor must delegate method calls.
79       * May be null.
80       */
81      protected AnnotationVisitor av;
82  
83      /**
84       * Constructs a new {@link AnnotationVisitor}.
85       *
86       * @param api
87       *            the ASM API version implemented by this visitor. Must be one
88       *            of {@link Opcodes#ASM4} or {@link Opcodes#ASM5}.
89       */
90      public AnnotationVisitor(final int api) {
91          this(api, null);
92      }
93  
94      /**
95       * Constructs a new {@link AnnotationVisitor}.
96       *
97       * @param api
98       *            the ASM API version implemented by this visitor. Must be one
99       *            of {@link Opcodes#ASM4} or {@link Opcodes#ASM5}.
100      * @param av
101      *            the annotation visitor to which this visitor must delegate
102      *            method calls. May be null.
103      */
104     public AnnotationVisitor(final int api, final AnnotationVisitor av) {
105         if (api != Opcodes.ASM4 && api != Opcodes.ASM5) {
106             throw new IllegalArgumentException();
107         }
108         this.api = api;
109         this.av = av;
110     }
111 
112     /**
113      * Visits a primitive value of the annotation.
114      *
115      * @param name
116      *            the value name.
117      * @param value
118      *            the actual value, whose type must be {@link Byte},
119      *            {@link Boolean}, {@link Character}, {@link Short},
120      *            {@link Integer} , {@link Long}, {@link Float}, {@link Double},
121      *            {@link String} or {@link Type} or OBJECT or ARRAY sort. This
122      *            value can also be an array of byte, boolean, short, char, int,
123      *            long, float or double values (this is equivalent to using
124      *            {@link #visitArray visitArray} and visiting each array element
125      *            in turn, but is more convenient).
126      */
127     public void visit(String name, Object value) {
128         if (av != null) {
129             av.visit(name, value);
130         }
131     }
132 
133     /**
134      * Visits an enumeration value of the annotation.
135      *
136      * @param name
137      *            the value name.
138      * @param desc
139      *            the class descriptor of the enumeration class.
140      * @param value
141      *            the actual enumeration value.
142      */
143     public void visitEnum(String name, String desc, String value) {
144         if (av != null) {
145             av.visitEnum(name, desc, value);
146         }
147     }
148 
149     /**
150      * Visits a nested annotation value of the annotation.
151      *
152      * @param name
153      *            the value name.
154      * @param desc
155      *            the class descriptor of the nested annotation class.
156      * @return a visitor to visit the actual nested annotation value, or
157      *         <tt>null</tt> if this visitor is not interested in visiting this
158      *         nested annotation. <i>The nested annotation value must be fully
159      *         visited before calling other methods on this annotation
160      *         visitor</i>.
161      */
162     public AnnotationVisitor visitAnnotation(String name, String desc) {
163         if (av != null) {
164             return av.visitAnnotation(name, desc);
165         }
166         return null;
167     }
168 
169     /**
170      * Visits an array value of the annotation. Note that arrays of primitive
171      * types (such as byte, boolean, short, char, int, long, float or double)
172      * can be passed as value to {@link #visit visit}. This is what
173      * {@link ClassReader} does.
174      *
175      * @param name
176      *            the value name.
177      * @return a visitor to visit the actual array value elements, or
178      *         <tt>null</tt> if this visitor is not interested in visiting these
179      *         values. The 'name' parameters passed to the methods of this
180      *         visitor are ignored. <i>All the array values must be visited
181      *         before calling other methods on this annotation visitor</i>.
182      */
183     public AnnotationVisitor visitArray(String name) {
184         if (av != null) {
185             return av.visitArray(name);
186         }
187         return null;
188     }
189 
190     /**
191      * Visits the end of the annotation.
192      */
193     public void visitEnd() {
194         if (av != null) {
195             av.visitEnd();
196         }
197     }
198 }