FileDocCategorySizeDatePackage
ConstantValue.javaAPI DocJava SE 5 API7238Fri Aug 26 14:55:16 BST 2005com.sun.org.apache.bcel.internal.classfile

ConstantValue

public final class ConstantValue extends Attribute
This class is derived from Attribute and represents a constant value, i.e., a default value for initializing a class field. This class is instantiated by the Attribute.readAttribute() method.
version
$Id: ConstantValue.java,v 1.1.1.1 2001/10/29 20:00:00 jvanzyl Exp $
author
M. Dahm
see
Attribute

Fields Summary
private int
constantvalue_index
Constructors Summary
public ConstantValue(ConstantValue c)
Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a physical copy.

    this(c.getNameIndex(), c.getLength(), c.getConstantValueIndex(),
	 c.getConstantPool());
  
ConstantValue(int name_index, int length, DataInputStream file, ConstantPool constant_pool)
Construct object from file stream.

param
name_index Name index in constant pool
param
length Content length in bytes
param
file Input stream
param
constant_pool Array of constants
throw
IOException

    this(name_index, length, (int)file.readUnsignedShort(), constant_pool);
  
public ConstantValue(int name_index, int length, int constantvalue_index, ConstantPool constant_pool)

param
name_index Name index in constant pool
param
length Content length in bytes
param
constantvalue_index Index in constant pool
param
constant_pool Array of constants

    super(Constants.ATTR_CONSTANT_VALUE, name_index, length, constant_pool);
    this.constantvalue_index = constantvalue_index;
  
Methods Summary
public voidaccept(com.sun.org.apache.bcel.internal.classfile.Visitor v)
Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.

param
v Visitor object

    v.visitConstantValue(this);
  
private static final java.lang.StringconvertString(java.lang.String label)
Escape all occurences of newline chars '\n', quotes \", etc.

    char[]       ch  = label.toCharArray();
    StringBuffer buf = new StringBuffer();

    for(int i=0; i < ch.length; i++) {
      switch(ch[i]) {
      case '\n":
	buf.append("\\n"); break;
      case '\r":
	buf.append("\\r"); break;
      case '\"":
	buf.append("\\\""); break;
      case '\'":
	buf.append("\\'"); break;
      case '\\":
	buf.append("\\\\"); break;
      default:
	buf.append(ch[i]); break;
      }
    }

    return buf.toString();
  
public com.sun.org.apache.bcel.internal.classfile.Attributecopy(com.sun.org.apache.bcel.internal.classfile.ConstantPool constant_pool)

return
deep copy of this attribute

    ConstantValue c = (ConstantValue)clone();
    c.constant_pool = constant_pool;
    return c;
  
public final voiddump(java.io.DataOutputStream file)
Dump constant value attribute to file stream on binary format.

param
file Output file stream
throw
IOException

    super.dump(file);
    file.writeShort(constantvalue_index);
  
public final intgetConstantValueIndex()

return
Index in constant pool of constant value.

 return constantvalue_index; 
public final voidsetConstantValueIndex(int constantvalue_index)

param
constantvalue_index.

    this.constantvalue_index = constantvalue_index;
  
public final java.lang.StringtoString()

return
String representation of constant value.

    Constant c = constant_pool.getConstant(constantvalue_index);
	
    String   buf;
    int    i;

    // Print constant to string depending on its type
    switch(c.getTag()) {
    case Constants.CONSTANT_Long:    buf = "" + ((ConstantLong)c).getBytes();    break;
    case Constants.CONSTANT_Float:   buf = "" + ((ConstantFloat)c).getBytes();   break;
    case Constants.CONSTANT_Double:  buf = "" + ((ConstantDouble)c).getBytes();  break;
    case Constants.CONSTANT_Integer: buf = "" + ((ConstantInteger)c).getBytes(); break;
    case Constants.CONSTANT_String:  
      i   = ((ConstantString)c).getStringIndex();
      c   = constant_pool.getConstant(i, Constants.CONSTANT_Utf8);
      buf = "\"" + convertString(((ConstantUtf8)c).getBytes()) + "\"";
      break;
    default: throw new InternalError("Type of ConstValue invalid: " + c);
    }

    return buf;