jaxp/src/java.xml/share/classes/com/sun/org/apache/bcel/internal/classfile/Field.java
author joehw
Mon, 17 Apr 2017 16:24:10 -0700
changeset 44797 8b3b3b911b8a
parent 25868 686eef1e7a79
child 46174 5611d2529b49
permissions -rw-r--r--
8162572: Update License Header for all JAXP sources Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     5
/*
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    12
 *
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    14
 *
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    18
 * See the License for the specific language governing permissions and
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    19
 * limitations under the License.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    20
 */
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    21
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.bcel.internal.classfile;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import  com.sun.org.apache.bcel.internal.Constants;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.bcel.internal.generic.Type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.io.*;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 * This class represents the field info structure, i.e., the representation
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 * for a variable in the class. See JVM specification for details.
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
public final class Field extends FieldOrMethod {
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
   * Initialize from another object. Note that both objects use the same
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
   * references (shallow copy). Use clone() for a physical copy.
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
  public Field(Field c) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
    super(c);
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
   * Construct object from file stream.
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
   * @param file Input stream
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
  Field(DataInputStream file, ConstantPool constant_pool)
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
       throws IOException, ClassFormatException
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
    super(file, constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
   * @param access_flags Access rights of field
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
   * @param name_index Points to field name in constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
   * @param signature_index Points to encoded signature
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
   * @param attributes Collection of attributes
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
   * @param constant_pool Array of constants
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
  public Field(int access_flags, int name_index, int signature_index,
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
               Attribute[] attributes, ConstantPool constant_pool)
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    super(access_flags, name_index, signature_index, attributes, constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
   * Called by objects that are traversing the nodes of the tree implicitely
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   * defined by the contents of a Java class. I.e., the hierarchy of methods,
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   * fields, attributes, etc. spawns a tree of objects.
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
   * @param v Visitor object
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
  public void accept(Visitor v) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
    v.visitField(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
   * @return constant value associated with this field (may be null)
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
  public final ConstantValue getConstantValue() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    for(int i=0; i < attributes_count; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
      if(attributes[i].getTag() == Constants.ATTR_CONSTANT_VALUE)
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
        return (ConstantValue)attributes[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
   * Return string representation close to declaration format,
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
   * `public static final short MAX = 100', e.g..
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
   * @return String representation of field, including the signature.
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  public final String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    String name, signature, access; // Short cuts to constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    // Get names from constant pool
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    access    = Utility.accessToString(access_flags);
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    access    = access.equals("")? "" : (access + " ");
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    signature = Utility.signatureToString(getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
    name      = getName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    StringBuffer  buf = new StringBuffer(access + signature + " " + name);
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    ConstantValue cv  = getConstantValue();
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    if(cv != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
      buf.append(" = " + cv);
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    for(int i=0; i < attributes_count; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
      Attribute a = attributes[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
      if(!(a instanceof ConstantValue))
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
        buf.append(" [" + a.toString() + "]");
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
    return buf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
   * @return deep copy of this field
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
  public final Field copy(ConstantPool constant_pool) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    return (Field)copy_(constant_pool);
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
   * @return type of field
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
  public Type getType() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    return Type.getReturnType(getSignature());
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
}