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      */
 172     public FieldNode(final int access, final String name, final String desc,
 173             final String signature, final Object value) {
 174         this(Opcodes.ASM5, access, name, desc, signature, value);
 175     }
 176 
 177     /**
 178      * Constructs a new {@link FieldNode}. <i>Subclasses must not use this
 179      * constructor</i>. Instead, they must use the
 180      * {@link #FieldNode(int, int, String, String, String, Object)} version.
 181      *
 182      * @param api
 183      *            the ASM API version implemented by this visitor. Must be one
 184      *            of {@link Opcodes#ASM4} or {@link Opcodes#ASM5}.
 185      * @param access
 186      *            the field's access flags (see
 187      *            {@link jdk.internal.org.objectweb.asm.Opcodes}). This parameter also
 188      *            indicates if the field is synthetic and/or deprecated.
 189      * @param name
 190      *            the field's name.
 191      * @param desc
 192      *            the field's descriptor (see {@link jdk.internal.org.objectweb.asm.Type
 193      *            Type}).
 194      * @param signature
 195      *            the field's signature.
 196      * @param value
 197      *            the field's initial value. This parameter, which may be
 198      *            <tt>null</tt> if the field does not have an initial value,
 199      *            must be an {@link Integer}, a {@link Float}, a {@link Long}, a
 200      *            {@link Double} or a {@link String}.
 201      */
 202     public FieldNode(final int api, final int access, final String name,
 203             final String desc, final String signature, final Object value) {
 204         super(api);
 205         this.access = access;
 206         this.name = name;
 207         this.desc = desc;
 208         this.signature = signature;
 209         this.value = value;
 210     }
 211 
 212     // ------------------------------------------------------------------------
 213     // Implementation of the FieldVisitor abstract class
 214     // ------------------------------------------------------------------------
 215 
 216     @Override
 217     public AnnotationVisitor visitAnnotation(final String desc,
 218             final boolean visible) {
 219         AnnotationNode an = new AnnotationNode(desc);
 220         if (visible) {
 221             if (visibleAnnotations == null) {
 222                 visibleAnnotations = new ArrayList<AnnotationNode>(1);
 223             }
 224             visibleAnnotations.add(an);
 225         } else {
 226             if (invisibleAnnotations == null) {
 227                 invisibleAnnotations = new ArrayList<AnnotationNode>(1);
 228             }
 229             invisibleAnnotations.add(an);
 230         }
 231         return an;
 232     }
 233 
 234     @Override
 235     public AnnotationVisitor visitTypeAnnotation(int typeRef,
 236             TypePath typePath, String desc, boolean visible) {
 237         TypeAnnotationNode an = new TypeAnnotationNode(typeRef, typePath, desc);
 238         if (visible) {
 239             if (visibleTypeAnnotations == null) {
 240                 visibleTypeAnnotations = new ArrayList<TypeAnnotationNode>(1);
 241             }
 242             visibleTypeAnnotations.add(an);
 243         } else {
 244             if (invisibleTypeAnnotations == null) {
 245                 invisibleTypeAnnotations = new ArrayList<TypeAnnotationNode>(1);
 246             }
 247             invisibleTypeAnnotations.add(an);
 248         }
 249         return an;
 250     }
 251 
 252     @Override
 253     public void visitAttribute(final Attribute attr) {
 254         if (attrs == null) {
 255             attrs = new ArrayList<Attribute>(1);
 256         }
 257         attrs.add(attr);
 258     }
 259 
 260     @Override
 261     public void visitEnd() {
 262     }
 263 
 264     // ------------------------------------------------------------------------
 265     // Accept methods
 266     // ------------------------------------------------------------------------
 267 
 268     /**
 269      * Checks that this field node is compatible with the given ASM API version.
 270      * This methods checks that this node, and all its nodes recursively, do not
 271      * contain elements that were introduced in more recent versions of the ASM
 272      * API than the given version.
 273      *
 274      * @param api
 275      *            an ASM API version. Must be one of {@link Opcodes#ASM4} or
 276      *            {@link Opcodes#ASM5}.
 277      */
 278     public void check(final int api) {
 279         if (api == Opcodes.ASM4) {
 280             if (visibleTypeAnnotations != null
 281                     && visibleTypeAnnotations.size() > 0) {
 282                 throw new RuntimeException();
 283             }
 284             if (invisibleTypeAnnotations != null
 285                     && invisibleTypeAnnotations.size() > 0) {
 286                 throw new RuntimeException();
 287             }
 288         }
 289     }
 290 
 291     /**
 292      * Makes the given class visitor visit this field.
 293      *
 294      * @param cv
 295      *            a class visitor.
 296      */
 297     public void accept(final ClassVisitor cv) {
 298         FieldVisitor fv = cv.visitField(access, name, desc, signature, value);
 299         if (fv == null) {
 300             return;
 301         }
 302         int i, n;
 303         n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
 304         for (i = 0; i < n; ++i) {
 305             AnnotationNode an = visibleAnnotations.get(i);
 306             an.accept(fv.visitAnnotation(an.desc, true));
 307         }
 308         n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size();
 309         for (i = 0; i < n; ++i) {
 310             AnnotationNode an = invisibleAnnotations.get(i);
 311             an.accept(fv.visitAnnotation(an.desc, false));
 312         }
 313         n = visibleTypeAnnotations == null ? 0 : visibleTypeAnnotations.size();
 314         for (i = 0; i < n; ++i) {
 315             TypeAnnotationNode an = visibleTypeAnnotations.get(i);
 316             an.accept(fv.visitTypeAnnotation(an.typeRef, an.typePath, an.desc,
 317                     true));
 318         }
 319         n = invisibleTypeAnnotations == null ? 0 : invisibleTypeAnnotations
 320                 .size();
 321         for (i = 0; i < n; ++i) {
 322             TypeAnnotationNode an = invisibleTypeAnnotations.get(i);
 323             an.accept(fv.visitTypeAnnotation(an.typeRef, an.typePath, an.desc,
 324                     false));
 325         }
 326         n = attrs == null ? 0 : attrs.size();
 327         for (i = 0; i < n; ++i) {
 328             fv.visitAttribute(attrs.get(i));
 329         }
 330         fv.visitEnd();
 331     }
 332 }