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 
  70 /**
  71  * A node that represents a field.
  72  *
  73  * @author Eric Bruneton
  74  */
  75 public class FieldNode extends FieldVisitor {
  76 
  77     /**
  78      * The field's access flags (see {@link jdk.internal.org.objectweb.asm.Opcodes}). This
  79      * field also indicates if the field is synthetic and/or deprecated.
  80      */
  81     public int access;
  82 
  83     /**
  84      * The field's name.
  85      */
  86     public String name;
  87 
  88     /**
  89      * The field's descriptor (see {@link jdk.internal.org.objectweb.asm.Type}).
  90      */
  91     public String desc;
  92 
  93     /**
  94      * The field's signature. May be <tt>null</tt>.
  95      */
  96     public String signature;
  97 
  98     /**
  99      * The field's initial value. This field, which may be <tt>null</tt> if
 100      * the field does not have an initial value, must be an {@link Integer}, a
 101      * {@link Float}, a {@link Long}, a {@link Double} or a {@link String}.
 102      */
 103     public Object value;
 104 
 105     /**
 106      * The runtime visible annotations of this field. This list is a list of
 107      * {@link AnnotationNode} objects. May be <tt>null</tt>.
 108      *
 109      * @associates jdk.internal.org.objectweb.asm.tree.AnnotationNode
 110      * @label visible
 111      */
 112     public List<AnnotationNode> visibleAnnotations;
 113 
 114     /**
 115      * The runtime invisible annotations of this field. This list is a list of
 116      * {@link AnnotationNode} objects. May be <tt>null</tt>.
 117      *
 118      * @associates jdk.internal.org.objectweb.asm.tree.AnnotationNode
 119      * @label invisible
 120      */
 121     public List<AnnotationNode> invisibleAnnotations;
 122 
 123     /**
 124      * The non standard attributes of this field. This list is a list of
 125      * {@link Attribute} objects. May be <tt>null</tt>.
 126      *
 127      * @associates jdk.internal.org.objectweb.asm.Attribute
 128      */
 129     public List<Attribute> attrs;
 130 
 131     /**
 132      * Constructs a new {@link FieldNode}. <i>Subclasses must not use this
 133      * constructor</i>. Instead, they must use the
 134      * {@link #FieldNode(int, int, String, String, String, Object)} version.
 135      *
 136      * @param access the field's access flags (see
 137      *        {@link jdk.internal.org.objectweb.asm.Opcodes}). This parameter also indicates
 138      *        if the field is synthetic and/or deprecated.
 139      * @param name the field's name.
 140      * @param desc the field's descriptor (see {@link jdk.internal.org.objectweb.asm.Type
 141      *        Type}).
 142      * @param signature the field's signature.
 143      * @param value the field's initial value. This parameter, which may be
 144      *        <tt>null</tt> if the field does not have an initial value, must be
 145      *        an {@link Integer}, a {@link Float}, a {@link Long}, a
 146      *        {@link Double} or a {@link String}.
 147      */
 148     public FieldNode(
 149         final int access,
 150         final String name,
 151         final String desc,
 152         final String signature,
 153         final Object value)
 154     {
 155         this(Opcodes.ASM4, access, name, desc, signature, value);
 156     }
 157 
 158     /**
 159      * Constructs a new {@link FieldNode}. <i>Subclasses must not use this
 160      * constructor</i>. Instead, they must use the
 161      * {@link #FieldNode(int, int, String, String, String, Object)} version.
 162      *
 163      * @param api the ASM API version implemented by this visitor. Must be one
 164      *        of {@link Opcodes#ASM4}.
 165      * @param access the field's access flags (see
 166      *        {@link jdk.internal.org.objectweb.asm.Opcodes}). This parameter also indicates
 167      *        if the field is synthetic and/or deprecated.
 168      * @param name the field's name.
 169      * @param desc the field's descriptor (see {@link jdk.internal.org.objectweb.asm.Type
 170      *        Type}).
 171      * @param signature the field's signature.
 172      * @param value the field's initial value. This parameter, which may be
 173      *        <tt>null</tt> if the field does not have an initial value, must be
 174      *        an {@link Integer}, a {@link Float}, a {@link Long}, a
 175      *        {@link Double} or a {@link String}.
 176      */
 177     public FieldNode(
 178         final int api,
 179         final int access,
 180         final String name,
 181         final String desc,
 182         final String signature,
 183         final Object value)
 184     {
 185         super(api);
 186         this.access = access;
 187         this.name = name;
 188         this.desc = desc;
 189         this.signature = signature;
 190         this.value = value;
 191     }
 192 
 193     // ------------------------------------------------------------------------
 194     // Implementation of the FieldVisitor abstract class
 195     // ------------------------------------------------------------------------
 196 
 197     @Override
 198     public AnnotationVisitor visitAnnotation(
 199         final String desc,
 200         final boolean visible)
 201     {
 202         AnnotationNode an = new AnnotationNode(desc);
 203         if (visible) {
 204             if (visibleAnnotations == null) {
 205                 visibleAnnotations = new ArrayList<AnnotationNode>(1);
 206             }
 207             visibleAnnotations.add(an);
 208         } else {
 209             if (invisibleAnnotations == null) {
 210                 invisibleAnnotations = new ArrayList<AnnotationNode>(1);
 211             }
 212             invisibleAnnotations.add(an);
 213         }
 214         return an;
 215     }
 216 
 217     @Override
 218     public void visitAttribute(final Attribute attr) {
 219         if (attrs == null) {
 220             attrs = new ArrayList<Attribute>(1);
 221         }
 222         attrs.add(attr);
 223     }
 224 
 225     @Override
 226     public void visitEnd() {
 227     }
 228 
 229     // ------------------------------------------------------------------------
 230     // Accept methods
 231     // ------------------------------------------------------------------------
 232 
 233     /**
 234      * Checks that this field node is compatible with the given ASM API version.
 235      * This methods checks that this node, and all its nodes recursively, do not
 236      * contain elements that were introduced in more recent versions of the ASM
 237      * API than the given version.
 238      *
 239      * @param api an ASM API version. Must be one of {@link Opcodes#ASM4}.
 240      */
 241     public void check(final int api) {
 242         // nothing to do
 243     }
 244 
 245     /**
 246      * Makes the given class visitor visit this field.
 247      *
 248      * @param cv a class visitor.
 249      */
 250     public void accept(final ClassVisitor cv) {
 251         FieldVisitor fv = cv.visitField(access, name, desc, signature, value);
 252         if (fv == null) {
 253             return;
 254         }
 255         int i, n;
 256         n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
 257         for (i = 0; i < n; ++i) {
 258             AnnotationNode an = visibleAnnotations.get(i);
 259             an.accept(fv.visitAnnotation(an.desc, true));
 260         }
 261         n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size();
 262         for (i = 0; i < n; ++i) {
 263             AnnotationNode an = invisibleAnnotations.get(i);
 264             an.accept(fv.visitAnnotation(an.desc, false));
 265         }
 266         n = attrs == null ? 0 : attrs.size();
 267         for (i = 0; i < n; ++i) {
 268             fv.visitAttribute(attrs.get(i));
 269         }
 270         fv.visitEnd();
 271     }
 272 }