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.tree; 60 61 import java.util.ArrayList; 62 import java.util.List; 63 64 import jdk.internal.org.objectweb.asm.AnnotationVisitor; 65 import jdk.internal.org.objectweb.asm.Attribute; 66 import jdk.internal.org.objectweb.asm.ClassVisitor; 67 import jdk.internal.org.objectweb.asm.FieldVisitor; 68 import jdk.internal.org.objectweb.asm.Opcodes; 69 import jdk.internal.org.objectweb.asm.TypePath; 70 71 /** 72 * A node that represents a field. 73 * 74 * @author Eric Bruneton 75 */ 76 public class FieldNode extends FieldVisitor { 77 78 /** 79 * The field's access flags (see {@link jdk.internal.org.objectweb.asm.Opcodes}). This 80 * field also indicates if the field is synthetic and/or deprecated. 81 */ 82 public int access; 83 84 /** 85 * The field's name. 86 */ 87 public String name; 88 89 /** 90 * The field's descriptor (see {@link jdk.internal.org.objectweb.asm.Type}). 91 */ 92 public String desc; 93 94 /** 95 * The field's signature. May be <tt>null</tt>. 96 */ 97 public String signature; 98 99 /** 100 * The field's initial value. This field, which may be <tt>null</tt> if the 101 * field does not have an initial value, must be an {@link Integer}, a 102 * {@link Float}, a {@link Long}, a {@link Double} or a {@link String}. 103 */ 104 public Object value; 105 106 /** 107 * The runtime visible annotations of this field. This list is a list of 108 * {@link AnnotationNode} objects. May be <tt>null</tt>. 109 * 110 * @associates jdk.internal.org.objectweb.asm.tree.AnnotationNode 111 * @label visible 112 */ 113 public List<AnnotationNode> visibleAnnotations; 114 115 /** 116 * The runtime invisible annotations of this field. This list is a list of 117 * {@link AnnotationNode} objects. May be <tt>null</tt>. 118 * 119 * @associates jdk.internal.org.objectweb.asm.tree.AnnotationNode 120 * @label invisible 121 */ 122 public List<AnnotationNode> invisibleAnnotations; 123 124 /** 125 * The runtime visible type annotations of this field. This list is a list 126 * of {@link TypeAnnotationNode} objects. May be <tt>null</tt>. 127 * 128 * @associates jdk.internal.org.objectweb.asm.tree.TypeAnnotationNode 129 * @label visible 130 */ 131 public List<TypeAnnotationNode> visibleTypeAnnotations; 132 133 /** 134 * The runtime invisible type annotations of this field. This list is a list 135 * of {@link TypeAnnotationNode} objects. May be <tt>null</tt>. 136 * 137 * @associates jdk.internal.org.objectweb.asm.tree.TypeAnnotationNode 138 * @label invisible 139 */ 140 public List<TypeAnnotationNode> invisibleTypeAnnotations; 141 142 /** 143 * The non standard attributes of this field. This list is a list of 144 * {@link Attribute} objects. May be <tt>null</tt>. 145 * 146 * @associates jdk.internal.org.objectweb.asm.Attribute 147 */ 148 public List<Attribute> attrs; 149 150 /** 151 * Constructs a new {@link FieldNode}. <i>Subclasses must not use this 152 * constructor</i>. Instead, they must use the 153 * {@link #FieldNode(int, int, String, String, String, Object)} version. 154 * 155 * @param access 156 * the field's access flags (see 157 * {@link jdk.internal.org.objectweb.asm.Opcodes}). This parameter also 158 * indicates if the field is synthetic and/or deprecated. 159 * @param name 160 * the field's name. 161 * @param desc 162 * the field's descriptor (see {@link jdk.internal.org.objectweb.asm.Type 163 * Type}). 164 * @param signature 165 * the field's signature. 166 * @param value 167 * the field's initial value. This parameter, which may be 168 * <tt>null</tt> if the field does not have an initial value, 169 * must be an {@link Integer}, a {@link Float}, a {@link Long}, a 170 * {@link Double} or a {@link String}. 171 * @throws IllegalStateException 172 * If a subclass calls this constructor. 173 */ 174 public FieldNode(final int access, final String name, final String desc, 175 final String signature, final Object value) { 176 this(Opcodes.ASM6, access, name, desc, signature, value); 177 if (getClass() != FieldNode.class) { 178 throw new IllegalStateException(); 179 } 180 } 181 182 /** 183 * Constructs a new {@link FieldNode}. <i>Subclasses must not use this 184 * constructor</i>. 185 * 186 * @param api 187 * the ASM API version implemented by this visitor. Must be one 188 * of {@link Opcodes#ASM4} or {@link Opcodes#ASM5}. 189 * @param access 190 * the field's access flags (see 191 * {@link jdk.internal.org.objectweb.asm.Opcodes}). This parameter also 192 * indicates if the field is synthetic and/or deprecated. 193 * @param name 194 * the field's name. 195 * @param desc 196 * the field's descriptor (see {@link jdk.internal.org.objectweb.asm.Type 197 * Type}). 198 * @param signature 199 * the field's signature. 200 * @param value 201 * the field's initial value. This parameter, which may be 202 * <tt>null</tt> if the field does not have an initial value, 203 * must be an {@link Integer}, a {@link Float}, a {@link Long}, a 204 * {@link Double} or a {@link String}. 205 */ 206 public FieldNode(final int api, final int access, final String name, 207 final String desc, final String signature, final Object value) { 208 super(api); 209 this.access = access; 210 this.name = name; 211 this.desc = desc; 212 this.signature = signature; 213 this.value = value; 214 } 215 216 // ------------------------------------------------------------------------ 217 // Implementation of the FieldVisitor abstract class 218 // ------------------------------------------------------------------------ 219 220 @Override 221 public AnnotationVisitor visitAnnotation(final String desc, 222 final boolean visible) { 223 AnnotationNode an = new AnnotationNode(desc); 224 if (visible) { 225 if (visibleAnnotations == null) { 226 visibleAnnotations = new ArrayList<AnnotationNode>(1); 227 } 228 visibleAnnotations.add(an); 229 } else { 230 if (invisibleAnnotations == null) { 231 invisibleAnnotations = new ArrayList<AnnotationNode>(1); 232 } 233 invisibleAnnotations.add(an); 234 } 235 return an; 236 } 237 238 @Override 239 public AnnotationVisitor visitTypeAnnotation(int typeRef, 240 TypePath typePath, String desc, boolean visible) { 241 TypeAnnotationNode an = new TypeAnnotationNode(typeRef, typePath, desc); 242 if (visible) { 243 if (visibleTypeAnnotations == null) { 244 visibleTypeAnnotations = new ArrayList<TypeAnnotationNode>(1); 245 } 246 visibleTypeAnnotations.add(an); 247 } else { 248 if (invisibleTypeAnnotations == null) { 249 invisibleTypeAnnotations = new ArrayList<TypeAnnotationNode>(1); 250 } 251 invisibleTypeAnnotations.add(an); 252 } 253 return an; 254 } 255 256 @Override 257 public void visitAttribute(final Attribute attr) { 258 if (attrs == null) { 259 attrs = new ArrayList<Attribute>(1); 260 } 261 attrs.add(attr); 262 } 263 264 @Override 265 public void visitEnd() { 266 } 267 268 // ------------------------------------------------------------------------ 269 // Accept methods 270 // ------------------------------------------------------------------------ 271 272 /** 273 * Checks that this field node is compatible with the given ASM API version. 274 * This methods checks that this node, and all its nodes recursively, do not 275 * contain elements that were introduced in more recent versions of the ASM 276 * API than the given version. 277 * 278 * @param api 279 * an ASM API version. Must be one of {@link Opcodes#ASM4}, 280 * {@link Opcodes#ASM5} or {@link Opcodes#ASM6}. 281 */ 282 public void check(final int api) { 283 if (api == Opcodes.ASM4) { 284 if (visibleTypeAnnotations != null 285 && visibleTypeAnnotations.size() > 0) { 286 throw new RuntimeException(); 287 } 288 if (invisibleTypeAnnotations != null 289 && invisibleTypeAnnotations.size() > 0) { 290 throw new RuntimeException(); 291 } 292 } 293 } 294 295 /** 296 * Makes the given class visitor visit this field. 297 * 298 * @param cv 299 * a class visitor. 300 */ 301 public void accept(final ClassVisitor cv) { 302 FieldVisitor fv = cv.visitField(access, name, desc, signature, value); 303 if (fv == null) { 304 return; 305 } 306 int i, n; 307 n = visibleAnnotations == null ? 0 : visibleAnnotations.size(); 308 for (i = 0; i < n; ++i) { 309 AnnotationNode an = visibleAnnotations.get(i); 310 an.accept(fv.visitAnnotation(an.desc, true)); 311 } 312 n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size(); 313 for (i = 0; i < n; ++i) { 314 AnnotationNode an = invisibleAnnotations.get(i); 315 an.accept(fv.visitAnnotation(an.desc, false)); 316 } 317 n = visibleTypeAnnotations == null ? 0 : visibleTypeAnnotations.size(); 318 for (i = 0; i < n; ++i) { 319 TypeAnnotationNode an = visibleTypeAnnotations.get(i); 320 an.accept(fv.visitTypeAnnotation(an.typeRef, an.typePath, an.desc, 321 true)); 322 } 323 n = invisibleTypeAnnotations == null ? 0 : invisibleTypeAnnotations 324 .size(); 325 for (i = 0; i < n; ++i) { 326 TypeAnnotationNode an = invisibleTypeAnnotations.get(i); 327 an.accept(fv.visitTypeAnnotation(an.typeRef, an.typePath, an.desc, 328 false)); 329 } 330 n = attrs == null ? 0 : attrs.size(); 331 for (i = 0; i < n; ++i) { 332 fv.visitAttribute(attrs.get(i)); 333 } 334 fv.visitEnd(); 335 } 336 }