View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.bcel.classfile;
18  
19  import java.io.DataInput;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.bcel.Const;
24  import org.apache.bcel.util.Args;
25  
26  /**
27   * This class is derived from <em>Attribute</em> and represents a constant value, i.e., a default value for initializing
28   * a class field. This class is instantiated by the <em>Attribute.readAttribute()</em> method.
29   *
30   * <pre>
31   * ConstantValue_attribute {
32   *   u2 attribute_name_index;
33   *   u4 attribute_length;
34   *   u2 constantvalue_index;
35   * }
36   * </pre>
37   * @see Attribute
38   */
39  public final class ConstantValue extends Attribute {
40  
41      private int constantValueIndex;
42  
43      /**
44       * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
45       * physical copy.
46       *
47       * @param c Source to copy.
48       */
49      public ConstantValue(final ConstantValue c) {
50          this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool());
51      }
52  
53      /**
54       * Constructs object from input stream.
55       *
56       * @param nameIndex Name index in constant pool
57       * @param length Content length in bytes
58       * @param input Input stream
59       * @param constantPool Array of constants
60       * @throws IOException if an I/O error occurs.
61       */
62      ConstantValue(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
63          this(nameIndex, length, input.readUnsignedShort(), constantPool);
64      }
65  
66      /**
67       * @param nameIndex Name index in constant pool
68       * @param length Content length in bytes
69       * @param constantValueIndex Index in constant pool
70       * @param constantPool Array of constants
71       */
72      public ConstantValue(final int nameIndex, final int length, final int constantValueIndex, final ConstantPool constantPool) {
73          super(Const.ATTR_CONSTANT_VALUE, nameIndex, Args.require(length, 2, "ConstantValue attribute length"), constantPool);
74          this.constantValueIndex = constantValueIndex;
75      }
76  
77      /**
78       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
79       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
80       *
81       * @param v Visitor object
82       */
83      @Override
84      public void accept(final Visitor v) {
85          v.visitConstantValue(this);
86      }
87  
88      /**
89       * @return deep copy of this attribute
90       */
91      @Override
92      public Attribute copy(final ConstantPool constantPool) {
93          final ConstantValue c = (ConstantValue) clone();
94          c.setConstantPool(constantPool);
95          return c;
96      }
97  
98      /**
99       * Dump constant value attribute to file stream on binary format.
100      *
101      * @param file Output file stream
102      * @throws IOException if an I/O error occurs.
103      */
104     @Override
105     public void dump(final DataOutputStream file) throws IOException {
106         super.dump(file);
107         file.writeShort(constantValueIndex);
108     }
109 
110     /**
111      * @return Index in constant pool of constant value.
112      */
113     public int getConstantValueIndex() {
114         return constantValueIndex;
115     }
116 
117     /**
118      * @param constantValueIndex the index info the constant pool of this constant value
119      */
120     public void setConstantValueIndex(final int constantValueIndex) {
121         this.constantValueIndex = constantValueIndex;
122     }
123 
124     /**
125      * @return String representation of constant value.
126      */
127     @Override
128     public String toString() {
129         Constant c = super.getConstantPool().getConstant(constantValueIndex);
130         String buf;
131         int i;
132         // Print constant to string depending on its type
133         switch (c.getTag()) {
134         case Const.CONSTANT_Long:
135             buf = String.valueOf(((ConstantLong) c).getBytes());
136             break;
137         case Const.CONSTANT_Float:
138             buf = String.valueOf(((ConstantFloat) c).getBytes());
139             break;
140         case Const.CONSTANT_Double:
141             buf = String.valueOf(((ConstantDouble) c).getBytes());
142             break;
143         case Const.CONSTANT_Integer:
144             buf = String.valueOf(((ConstantInteger) c).getBytes());
145             break;
146         case Const.CONSTANT_String:
147             i = ((ConstantString) c).getStringIndex();
148             c = super.getConstantPool().getConstantUtf8(i);
149             buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\"";
150             break;
151         default:
152             throw new IllegalStateException("Type of ConstValue invalid: " + c);
153         }
154         return buf;
155     }
156 }