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