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