Constant Pool classes

The Constant Pool classes allows you to manipulate the entries of a classfile's constant pool. A constant pool contains entries for:

  • The non-immediate constants used in the class. (An immediate constant is a small numeric value that is directly encoded into an instruction for efficiency).
  • The fields, methods, classes, and arrays used in the class.

The ValueRef class is for all the non-immediate int, long, float, double, and String constants used in the class. The FieldRef class is for the fields referenced in the class. The MethodRef class is for the methods referenced in the class. The TypeRef class is for the classes and arrays referenced in the class.

These classes do not have public constructors. To reach a classfile's pool entries, you must use firstValueRef, firstFieldRef, firstMethodRef, or firstTypeRef in the ClassFile class. You can also get the pool entry referenced by an instruction via getReferenceArg.

Method Summary

  • getObject – returns the object that this pool entry is a reference for
  • getTypeRef – returns the TypeRef contained in this field or method reference
  • next – returns the next pool entry  of the same type
  • redirect – redirects method invokations to your copy of the method
  • toName – returns the name of this field, method, or class reference
  • toQualifiedName – returns the qualified name of this class reference
  • toString – returns the info in this pool entry in a source-like form

Fields

public Object userDefined

Field that can be used by an application developer to store arbitrary data about this object during the trek.

Methods

In ValueRef:  public Object getObject()
In FieldRef:  public Field getObject()
In MethodRef: public Method getObject()
In TypeRef:   public ClassFile getObject()

Returns the String, Integer, Long, Float, or Double object referenced by this ValueRef.

Returns the code object referenced by this field, method, or class reference if its classfile can be accessed via getClassFile. Otherwise an exception is thrown.

An array reference does not reference an object; so getObject for an array TypeRef always returns null.

In FieldRef:   public TypeRef getTypeRef()
In MethodRef:  public TypeRef getTypeRef()

Returns the TypeRef of the referenced member's class.

In ValueRef:   public Constant next()
In FieldRef:   public FieldRef next()
In MethodRef:  public MethodRef next()
In TypeRef:    public TypeRef next()

Returns the next entry with the same type as this entry. If there are no more entries of this type, null is returned instead.

In MethodRef:  public void redirect(String clname, String name, String desc)

Modifies the MethodRef to identify a method with the specified class, name, and descriptor. Since all invokexxx instructions of a method reference the same MethodRef, this will cause each usage of that method within this classfile to invoke your version instead.

Parameters:

clname – the fully qualified name of your method's class

name – if null, the MethodRef's current method name is used. If not null, name becomes the MethodRef's new method name.

desc – if null, the MethodRef's current argument types and return type are used. If not null, its value sets the new types. The format of desc must be as described in The Java Virtual Machine Specification. Also if the new types are not consistent with the arguments of each invokation of this method, a runtime error will result. For example, you could safely specify a type of Object where previously the type had been String.

In FieldRef:   public String toName()
In MethodRef:  public String toName()
In TypeRef:    public String toName()

Returns the name of the referenced field, method, or class. Returns null if this entry is an array reference.

In TypeRef:  public String toQualifiedName()

Returns the qualified name of the referenced class. Returns null if this entry is an array reference.

In all:  public String toString()

Returns the information in this entry in a source-like form. For a String constant, bounding double quotes are included in what is returned. For a field or method reference, the signature of the referenced member is returned. For a type reference, the returned string looks like the right term of an instanceof.