edu.wisc.cs.java
Class Constant

java.lang.Object
  |
  +--edu.wisc.cs.java.Constant

public class Constant
extends java.lang.Object

This class represents an entry in the constant pool table of a class file.


Field Summary
static byte CLASS
          Constant representing the constant pool entry type CLASS.
static byte DOUBLE
          Constant representing the constant pool entry type DOUBLE.
static byte FIELD_REF
          Constant representing the constant pool entry type FIELD_REF.
static byte FLOAT
          Constant representing the constant pool entry type FLOAT.
static byte INTEGER
          Constant representing the constant pool entry type INTEGER.
static byte INTERFACE_METHOD_REF
          Constant representing the constant pool entry type INTERFACE_METHOD_REF.
static byte LONG
          Constant representing the constant pool entry type LONG.
static byte METHOD_REF
          Constant representing the constant pool entry type METHOD_REF.
static byte NAME_AND_TYPE
          Constant representing the constant pool entry type NAME_AND_TYPE.
static byte STRING
          Constant representing the constant pool entry type STRING.
static byte UTF8
          Constant representing the constant pool entry type UTF8.
 
Constructor Summary
Constant(byte tp, int i1)
          Constructs a new constant pool entry of type CLASS or STRING.
Constant(byte tp, int i1, int i2)
          Constructs a new constant pool entry describing a field, method or name and type.
Constant(java.io.DataInputStream in)
          Constructs a new constant pool entry by reading data from the given stream.
Constant(double d)
          Constructs a constant pool entry of type DOUBLE.
Constant(float f)
          Constructs a constant pool entry of type FLOAT.
Constant(int i)
          Constructs a constant pool entry of type INTEGER.
Constant(long l)
          Constructs a constant pool entry of type LONG.
Constant(java.lang.String data)
          Constructs a new UTF8 constant pool entry with a specified value.
 
Method Summary
 boolean equals(java.lang.Object o)
          Compares this Constant object to another for equality.
 double getDoubleValue()
          Returns the double value of a this constant.
 float getFloatValue()
          Returns the float value of a this constant.
 int getIndex1()
          Returns the value of the first contant pool index.
 int getIndex2()
          Returns the value of the second contant pool index.
 int getIntegerValue()
          Returns the int value of a this constant.
 long getLongValue()
          Returns the long value of a this constant.
 byte getType()
          Returns the type code for this constant pool entry.
 java.lang.String getUTF()
          Returns the string contained in this constant if it is of type UTF8.
 void printData(java.io.PrintStream out)
          Writes textual information about the class represented by this Constant object to the specified destination.
 void writeTo(java.io.DataOutputStream out)
          Writes a constant to the specified stream in the format required by the Java Virtual Machine Specification.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UTF8

public static final byte UTF8
Constant representing the constant pool entry type UTF8.

INTEGER

public static final byte INTEGER
Constant representing the constant pool entry type INTEGER.

FLOAT

public static final byte FLOAT
Constant representing the constant pool entry type FLOAT.

LONG

public static final byte LONG
Constant representing the constant pool entry type LONG.

DOUBLE

public static final byte DOUBLE
Constant representing the constant pool entry type DOUBLE.

CLASS

public static final byte CLASS
Constant representing the constant pool entry type CLASS.

STRING

public static final byte STRING
Constant representing the constant pool entry type STRING.

FIELD_REF

public static final byte FIELD_REF
Constant representing the constant pool entry type FIELD_REF.

METHOD_REF

public static final byte METHOD_REF
Constant representing the constant pool entry type METHOD_REF.

INTERFACE_METHOD_REF

public static final byte INTERFACE_METHOD_REF
Constant representing the constant pool entry type INTERFACE_METHOD_REF.

NAME_AND_TYPE

public static final byte NAME_AND_TYPE
Constant representing the constant pool entry type NAME_AND_TYPE.
Constructor Detail

Constant

public Constant(java.lang.String data)
Constructs a new UTF8 constant pool entry with a specified value.
Parameters:
data - the string value for the new constant.

Constant

public Constant(int i)
Constructs a constant pool entry of type INTEGER.
Parameters:
i - the value of the new constant.

Constant

public Constant(long l)
Constructs a constant pool entry of type LONG.
Parameters:
l - the value of the new constant.

Constant

public Constant(float f)
Constructs a constant pool entry of type FLOAT.
Parameters:
f - the value of the new constant.

Constant

public Constant(double d)
Constructs a constant pool entry of type DOUBLE.
Parameters:
d - the value of the new constant.

Constant

public Constant(byte tp,
                int i1)
Constructs a new constant pool entry of type CLASS or STRING.
Parameters:
tp - the type of the new constant pool entry, either STRING or CLASS.
i1 - the index of the UTF8 entry describing this constant.
Throws:
java.lang.IllegalArgumentException - if the type is inconsistant with two indices.

Constant

public Constant(byte tp,
                int i1,
                int i2)
Constructs a new constant pool entry describing a field, method or name and type. The value of the tp parameter may be one of: FIELD_REF, METHOD_REF, INTERFACE_METHOD_REF, or NAME_AND_TYPE.
Parameters:
tp - the type of the new constant pool entry.
i1 - the index of an associated constant, either a CLASS or a UTF8.
i2 - the index of an associated constant, either a NAME_AND_TYPE or a UTF8.
Throws:
java.lang.IllegalArgumentException - if the type is inconsistant with two indices.

Constant

public Constant(java.io.DataInputStream in)
         throws java.io.IOException
Constructs a new constant pool entry by reading data from the given stream.
Parameters:
in - the stream from which to read the constant pool entry.
Throws:
java.io.IOException - if there is an error reading from the stream.
Method Detail

getType

public byte getType()
Returns the type code for this constant pool entry.
Returns:
the type code for this constant pool entry.

getUTF

public java.lang.String getUTF()
Returns the string contained in this constant if it is of type UTF8.
Returns:
the string contained in this constant if it is of type UTF8.
Throws:
UnsupportedOperationException - if the constant is not of type UTF8.

getIntegerValue

public int getIntegerValue()
Returns the int value of a this constant.
Returns:
the int value of a this constant.
Throws:
UnsupportedOperationException - if the constant is not of type INTEGER.

getLongValue

public long getLongValue()
Returns the long value of a this constant.
Returns:
the long value of a this constant.
Throws:
UnsupportedOperationException - if the constant is not of type LONG.

getFloatValue

public float getFloatValue()
Returns the float value of a this constant.
Returns:
the float value of a this constant.
Throws:
UnsupportedOperationException - if the constant is not of type FLOAT.

getDoubleValue

public double getDoubleValue()
Returns the double value of a this constant.
Returns:
the double value of a this constant.
Throws:
UnsupportedOperationException - if the constant is not of type DOUBLE.

getIndex1

public int getIndex1()
Returns the value of the first contant pool index. This is supported on constants of type CLASS, STRING, FIELD_REF, METHOD_REF, INTERFACE_REF, and NAME_AND_TYPE only.
Returns:
the value of the first constant pool index.
Throws:
UnsupportedOperationException - if the constant is not of a type which supports this method.

getIndex2

public int getIndex2()
Returns the value of the second contant pool index. This is supported on constants of type FIELD_REF, METHOD_REF, INTERFACE_REF, and NAME_AND_TYPE only.
Returns:
the value of the second constant pool index.
Throws:
UnsupportedOperationException - if the constant is not of a type which supports this method.

equals

public boolean equals(java.lang.Object o)
Compares this Constant object to another for equality.
Overrides:
equals in class java.lang.Object
Parameters:
o - the object to compare this Constant to.
Returns:
true if the objects are equivilant, false otherwise.

writeTo

public void writeTo(java.io.DataOutputStream out)
             throws java.io.IOException
Writes a constant to the specified stream in the format required by the Java Virtual Machine Specification.
Parameters:
out - a destination stream to write the classfile data to.
Throws:
java.io.IOException - if there is a problem writing to the stream.

printData

public void printData(java.io.PrintStream out)
Writes textual information about the class represented by this Constant object to the specified destination. The destination is usually System.out or System.err. This method is primarily for debugging purposes.
Parameters:
out - a PrintStream connected to the desired destination.