• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // ASM: a very small and fast Java bytecode manipulation framework
2 // Copyright (c) 2000-2011 INRIA, France Telecom
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions
7 // are met:
8 // 1. Redistributions of source code must retain the above copyright
9 //    notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 //    notice, this list of conditions and the following disclaimer in the
12 //    documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holders nor the names of its
14 //    contributors may be used to endorse or promote products derived from
15 //    this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 // THE POSSIBILITY OF SUCH DAMAGE.
28 package org.objectweb.asm;
29 
30 /**
31  * A visitor to visit a Java field. The methods of this class must be called in the following order:
32  * ( {@code visitAnnotation} | {@code visitTypeAnnotation} | {@code visitAttribute} )* {@code
33  * visitEnd}.
34  *
35  * @author Eric Bruneton
36  */
37 public abstract class FieldVisitor {
38 
39   /**
40    * The ASM API version implemented by this visitor. The value of this field must be one of the
41    * {@code ASM}<i>x</i> values in {@link Opcodes}.
42    */
43   protected final int api;
44 
45   /** The field visitor to which this visitor must delegate method calls. May be {@literal null}. */
46   protected FieldVisitor fv;
47 
48   /**
49    * Constructs a new {@link FieldVisitor}.
50    *
51    * @param api the ASM API version implemented by this visitor. Must be one of the {@code
52    *     ASM}<i>x</i> values in {@link Opcodes}.
53    */
FieldVisitor(final int api)54   protected FieldVisitor(final int api) {
55     this(api, null);
56   }
57 
58   /**
59    * Constructs a new {@link FieldVisitor}.
60    *
61    * @param api the ASM API version implemented by this visitor. Must be one of the {@code
62    *     ASM}<i>x</i> values in {@link Opcodes}.
63    * @param fieldVisitor the field visitor to which this visitor must delegate method calls. May be
64    *     null.
65    */
FieldVisitor(final int api, final FieldVisitor fieldVisitor)66   protected FieldVisitor(final int api, final FieldVisitor fieldVisitor) {
67     if (api != Opcodes.ASM9
68         && api != Opcodes.ASM8
69         && api != Opcodes.ASM7
70         && api != Opcodes.ASM6
71         && api != Opcodes.ASM5
72         && api != Opcodes.ASM4
73         && api != Opcodes.ASM10_EXPERIMENTAL) {
74       throw new IllegalArgumentException("Unsupported api " + api);
75     }
76     if (api == Opcodes.ASM10_EXPERIMENTAL) {
77       Constants.checkAsmExperimental(this);
78     }
79     this.api = api;
80     this.fv = fieldVisitor;
81   }
82 
83   /**
84    * The field visitor to which this visitor must delegate method calls. May be {@literal null}.
85    *
86    * @return the field visitor to which this visitor must delegate method calls, or {@literal null}.
87    */
getDelegate()88   public FieldVisitor getDelegate() {
89     return fv;
90   }
91 
92   /**
93    * Visits an annotation of the field.
94    *
95    * @param descriptor the class descriptor of the annotation class.
96    * @param visible {@literal true} if the annotation is visible at runtime.
97    * @return a visitor to visit the annotation values, or {@literal null} if this visitor is not
98    *     interested in visiting this annotation.
99    */
visitAnnotation(final String descriptor, final boolean visible)100   public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
101     if (fv != null) {
102       return fv.visitAnnotation(descriptor, visible);
103     }
104     return null;
105   }
106 
107   /**
108    * Visits an annotation on the type of the field.
109    *
110    * @param typeRef a reference to the annotated type. The sort of this type reference must be
111    *     {@link TypeReference#FIELD}. See {@link TypeReference}.
112    * @param typePath the path to the annotated type argument, wildcard bound, array element type, or
113    *     static inner type within 'typeRef'. May be {@literal null} if the annotation targets
114    *     'typeRef' as a whole.
115    * @param descriptor the class descriptor of the annotation class.
116    * @param visible {@literal true} if the annotation is visible at runtime.
117    * @return a visitor to visit the annotation values, or {@literal null} if this visitor is not
118    *     interested in visiting this annotation.
119    */
visitTypeAnnotation( final int typeRef, final TypePath typePath, final String descriptor, final boolean visible)120   public AnnotationVisitor visitTypeAnnotation(
121       final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
122     if (api < Opcodes.ASM5) {
123       throw new UnsupportedOperationException("This feature requires ASM5");
124     }
125     if (fv != null) {
126       return fv.visitTypeAnnotation(typeRef, typePath, descriptor, visible);
127     }
128     return null;
129   }
130 
131   /**
132    * Visits a non standard attribute of the field.
133    *
134    * @param attribute an attribute.
135    */
visitAttribute(final Attribute attribute)136   public void visitAttribute(final Attribute attribute) {
137     if (fv != null) {
138       fv.visitAttribute(attribute);
139     }
140   }
141 
142   /**
143    * Visits the end of the field. This method, which is the last one to be called, is used to inform
144    * the visitor that all the annotations and attributes of the field have been visited.
145    */
visitEnd()146   public void visitEnd() {
147     if (fv != null) {
148       fv.visitEnd();
149     }
150   }
151 }
152