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 22 package com.sun.org.apache.bcel.internal.classfile; 23 24 import java.io.DataInput; 25 import java.io.DataOutputStream; 26 import java.io.IOException; 27 28 import com.sun.org.apache.bcel.internal.Const; 29 30 /** 31 * This class is derived from <em>Attribute</em> and represents a constant 32 * value, i.e., a default value for initializing a class field. 33 * This class is instantiated by the <em>Attribute.readAttribute()</em> method. 34 * 35 * @version $Id: ConstantValue.java 1749603 2016-06-21 20:50:19Z ggregory $ 36 * @see Attribute 37 */ 38 public final class ConstantValue extends Attribute { 39 40 private int constantvalue_index; 41 42 43 /** 44 * Initialize from another object. Note that both objects use the same 45 * references (shallow copy). Use clone() for a physical copy. 46 */ ConstantValue(final ConstantValue c)47 public ConstantValue(final ConstantValue c) { 48 this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(), c.getConstantPool()); 49 } 50 51 52 /** 53 * Construct object from input stream. 54 * @param name_index Name index in constant pool 55 * @param length Content length in bytes 56 * @param input Input stream 57 * @param constant_pool Array of constants 58 * @throws IOException 59 */ ConstantValue(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)60 ConstantValue(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) 61 throws IOException { 62 this(name_index, length, input.readUnsignedShort(), constant_pool); 63 } 64 65 66 /** 67 * @param name_index Name index in constant pool 68 * @param length Content length in bytes 69 * @param constantvalue_index Index in constant pool 70 * @param constant_pool Array of constants 71 */ ConstantValue(final int name_index, final int length, final int constantvalue_index, final ConstantPool constant_pool)72 public ConstantValue(final int name_index, final int length, final int constantvalue_index, 73 final ConstantPool constant_pool) { 74 super(Const.ATTR_CONSTANT_VALUE, name_index, length, constant_pool); 75 this.constantvalue_index = constantvalue_index; 76 } 77 78 79 /** 80 * Called by objects that are traversing the nodes of the tree implicitely 81 * defined by the contents of a Java class. I.e., the hierarchy of methods, 82 * fields, attributes, etc. spawns a tree of objects. 83 * 84 * @param v Visitor object 85 */ 86 @Override accept( final Visitor v )87 public void accept( final Visitor v ) { 88 v.visitConstantValue(this); 89 } 90 91 92 /** 93 * Dump constant value attribute to file stream on binary format. 94 * 95 * @param file Output file stream 96 * @throws IOException 97 */ 98 @Override dump( final DataOutputStream file )99 public final void dump( final DataOutputStream file ) throws IOException { 100 super.dump(file); 101 file.writeShort(constantvalue_index); 102 } 103 104 105 /** 106 * @return Index in constant pool of constant value. 107 */ getConstantValueIndex()108 public final int getConstantValueIndex() { 109 return constantvalue_index; 110 } 111 112 113 /** 114 * @param constantvalue_index the index info the constant pool of this constant value 115 */ setConstantValueIndex( final int constantvalue_index )116 public final void setConstantValueIndex( final int constantvalue_index ) { 117 this.constantvalue_index = constantvalue_index; 118 } 119 120 121 /** 122 * @return String representation of constant value. 123 */ 124 @Override toString()125 public final String toString() { 126 Constant c = super.getConstantPool().getConstant(constantvalue_index); 127 String buf; 128 int i; 129 // Print constant to string depending on its type 130 switch (c.getTag()) { 131 case Const.CONSTANT_Long: 132 buf = String.valueOf(((ConstantLong) c).getBytes()); 133 break; 134 case Const.CONSTANT_Float: 135 buf = String.valueOf(((ConstantFloat) c).getBytes()); 136 break; 137 case Const.CONSTANT_Double: 138 buf = String.valueOf(((ConstantDouble) c).getBytes()); 139 break; 140 case Const.CONSTANT_Integer: 141 buf = String.valueOf(((ConstantInteger) c).getBytes()); 142 break; 143 case Const.CONSTANT_String: 144 i = ((ConstantString) c).getStringIndex(); 145 c = super.getConstantPool().getConstant(i, Const.CONSTANT_Utf8); 146 buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\""; 147 break; 148 default: 149 throw new IllegalStateException("Type of ConstValue invalid: " + c); 150 } 151 return buf; 152 } 153 154 155 /** 156 * @return deep copy of this attribute 157 */ 158 @Override copy( final ConstantPool _constant_pool )159 public Attribute copy( final ConstantPool _constant_pool ) { 160 final ConstantValue c = (ConstantValue) clone(); 161 c.setConstantPool(_constant_pool); 162 return c; 163 } 164 } 165