FileDocCategorySizeDatePackage
ConstantPool.javaAPI DocGlassfish v2 API12673Fri May 04 22:34:28 BST 2007com.sun.jdo.api.persistence.enhancer.classfile

ConstantPool

public class ConstantPool extends Object implements VMConstants
Constant Pool implementation - this represents the constant pool of a class in a class file.

Fields Summary
private Vector
pool
private boolean
hashed
private Hashtable
utfTable
private Hashtable
unicodeTable
private Hashtable
stringTable
private Hashtable
classTable
private Hashtable
intTable
private Hashtable
floatTable
private Hashtable
longTable
private Hashtable
doubleTable
private Vector
methodRefTable
private Vector
fieldRefTable
private Vector
ifaceMethodRefTable
private Vector
nameAndTypeTable
Constructors Summary
ConstantPool()

    pool.addElement(null);
  
ConstantPool(DataInputStream input)

    pool.addElement(null);
    int nconstants = input.readUnsignedShort()-1;
    while (nconstants > 0)
      nconstants -= readConstant(input);

    resolvePool();
  
Methods Summary
public ConstClassaddClass(java.lang.String className)
Find or create a class constant in the pool

    hashConstants();
    ConstClass c = (ConstClass) classTable.get(className);
    if (c == null) {
      c = new ConstClass(addUtf8(className));
      internConstant(c);
    }
    return c;
  
public ConstDoubleaddDouble(double d)
Find or create a double constant in the pool

    hashConstants();
    Double dobj = new Double(d);
    ConstDouble cd = (ConstDouble) doubleTable.get(dobj);
    if (cd == null) {
      cd = new ConstDouble(d);
      internConstant(cd);
      internConstant(null);
    }
    return cd;
  
public ConstFieldRefaddFieldRef(java.lang.String className, java.lang.String fieldName, java.lang.String type)
Find or create a field constant in the pool

    hashConstants();
    ConstFieldRef f = (ConstFieldRef)
      searchTable(fieldRefTable, className, fieldName, type);

    if (f == null) {
      f = new ConstFieldRef (addClass(className),
			     addNameAndType(fieldName, type));
      internConstant(f);
    }
    return f;
  
public ConstFloataddFloat(float f)
Find or create a float constant in the pool

    hashConstants();
    Float fo = new Float(f);
    ConstFloat cf = (ConstFloat) floatTable.get(fo);
    if (cf == null) {
      cf = new ConstFloat(f);
      internConstant(cf);
    }
    return cf;
  
public ConstIntegeraddInteger(int i)
Find or create an integer constant in the pool

    hashConstants();
    Integer io = new Integer(i);
    ConstInteger ci = (ConstInteger) intTable.get(io);
    if (ci == null) {
      ci = new ConstInteger(i);
      internConstant(ci);
    }
    return ci;
  
public ConstInterfaceMethodRefaddInterfaceMethodRef(java.lang.String className, java.lang.String methodName, java.lang.String type)
Find or create an interface method constant in the pool

    hashConstants();
    ConstInterfaceMethodRef m = (ConstInterfaceMethodRef)
      searchTable(ifaceMethodRefTable, className, methodName, type);
    if (m == null) {
      m = new ConstInterfaceMethodRef (addClass(className),
				       addNameAndType(methodName, type));
      internConstant(m);
    }
    return m;
  
public ConstLongaddLong(long l)
Find or create a long constant in the pool

    hashConstants();
    Long lo = new Long(l);
    ConstLong cl = (ConstLong) longTable.get(lo);
    if (cl == null) {
      cl = new ConstLong(l);
      internConstant(cl);
      internConstant(null);
    }
    return cl;
  
public ConstMethodRefaddMethodRef(java.lang.String className, java.lang.String methodName, java.lang.String type)
Find or create a method constant in the pool

    hashConstants();
    ConstMethodRef m = (ConstMethodRef)
      searchTable(methodRefTable, className, methodName, type);
    if (m == null) {
      m = new ConstMethodRef (addClass(className),
			      addNameAndType(methodName, type));
      internConstant(m);
    }
    return m;
  
public ConstNameAndTypeaddNameAndType(java.lang.String name, java.lang.String type)
Find or create a name/type constant in the pool

    hashConstants();
    for (int i=0; i<nameAndTypeTable.size(); i++) {
      ConstNameAndType nt = (ConstNameAndType) nameAndTypeTable.elementAt(i);
      if (nt.name().asString().equals(name) &&
	  nt.signature().asString().equals(type))
	return nt;
    }

    ConstNameAndType nt =
      new ConstNameAndType(addUtf8(name), addUtf8(type));
    internConstant(nt);
    return nt;
  
public ConstStringaddString(java.lang.String s)
Find or create a string constant in the pool

    hashConstants();
    ConstString cs = (ConstString) stringTable.get(s);
    if (cs == null) {
      cs = new ConstString(addUtf8(s));
      internConstant(cs);
    }
    return cs;
  
public ConstUnicodeaddUnicode(java.lang.String s)
Find or create a unicode constant in the pool Obsolete?

    hashConstants();
    ConstUnicode u = (ConstUnicode) unicodeTable.get(s);
    if (u == null) {
      u = new ConstUnicode(s);
      internConstant(u);
    }
    return u;
  
public ConstUtf8addUtf8(java.lang.String s)
Find or create a utf8 constant in the pool

    hashConstants();
    ConstUtf8 u = (ConstUtf8) utfTable.get(s);
    if (u == null) {
      u = new ConstUtf8(s);
      internConstant(u);
    }
    return u;
  
public ConstBasicconstantAt(int index)
Return the constant in the pool at the specified entry index

    return (ConstBasic) pool.elementAt(index);
  
private voidhashConstants()

    if (hashed)
      return;

    /* Enter objects into the hash tables */
    for (int j=0; j<pool.size(); j++) {
      ConstBasic c = constantAt(j);
      if (c != null) {
	recordConstant(c);
      }
    }

    hashed = true;
  
private voidinternConstant(ConstBasic c)

    if (c != null) {
      c.setIndex(pool.size());
      recordConstant(c);
    }
    pool.addElement(c);
  
public intnEntries()
Return the number of pool entries.


  /* public accessors */

           
     
    return pool.size();
  
voidprint(java.io.PrintStream out)

    for (int i=0; i<pool.size(); i++) {
      ConstBasic c = constantAt(i);
      if (c != null) {
        out.print (i);
        out.print (": ");//NOI18N
        out.println (c.toString());
      }
    }
  
private intreadConstant(java.io.DataInputStream input)

    ConstBasic basic;
    byte b = input.readByte();
    int slots = 1;
    switch (b) {
    case CONSTANTUtf8:
      basic = ConstUtf8.read(input);
      break;
    case CONSTANTUnicode:
      basic = ConstUnicode.read(input);
      break;
    case CONSTANTInteger:
      basic = ConstInteger.read(input);
      break;
    case CONSTANTFloat:
      basic = ConstFloat.read(input);
      break;
    case CONSTANTLong:
      basic = ConstLong.read(input);
      slots = 2;
      break;
    case CONSTANTDouble:
      basic = ConstDouble.read(input);
      slots = 2;
      break;
    case CONSTANTClass:
      basic = ConstClass.read(input);
      break;
    case CONSTANTString:
      basic = ConstString.read(input);
      break;
    case CONSTANTFieldRef:
      basic = ConstFieldRef.read(input);
      break;
    case CONSTANTMethodRef:
      basic = ConstMethodRef.read(input);
      break;
    case CONSTANTInterfaceMethodRef:
      basic = ConstInterfaceMethodRef.read(input);
      break;
    case CONSTANTNameAndType:
      basic = ConstNameAndType.read(input);
      break;
    default:
        throw new ClassFormatError("Don't know this constant type: " +//NOI18N
			 Integer.toString(b));
    }

    pool.addElement(basic);
    if (slots > 1)
      pool.addElement(null);   
    return slots;
  
private voidrecordConstant(ConstBasic c)

    if (c != null) {
      switch (c.tag()) {
      case CONSTANTUtf8:
	utfTable.put(((ConstUtf8)c).asString(), c);
	break;
      case CONSTANTUnicode:
	unicodeTable.put(((ConstUnicode)c).asString(), c);
	break;
      case CONSTANTInteger:
	intTable.put(new Integer(((ConstInteger)c).value()), c);
	break;
      case CONSTANTFloat:
	floatTable.put(new Float(((ConstFloat)c).value()), c);
	break;
      case CONSTANTLong:
	longTable.put(new Long(((ConstLong)c).value()), c);
	break;
      case CONSTANTDouble:
	doubleTable.put(new Double(((ConstDouble)c).value()), c);
	break;
      case CONSTANTClass:
	classTable.put(((ConstClass)c).asString(), c);
	break;
      case CONSTANTString:
	stringTable.put(((ConstString)c).value().asString(), c);
	break;
      case CONSTANTFieldRef:
	fieldRefTable.addElement(c);
	break;
      case CONSTANTMethodRef:
	methodRefTable.addElement(c);
	break;
      case CONSTANTInterfaceMethodRef:
	ifaceMethodRefTable.addElement(c);
	break;
      case CONSTANTNameAndType:
	nameAndTypeTable.addElement(c);
	break;
      }
    }
  
private voidresolvePool()

    /* resolve indexes to object references */
    for (int i=0; i<pool.size(); i++) {
      ConstBasic c = constantAt(i);
      if (c != null) {
	c.setIndex(i);
        c.resolve(this);
      }
    }
  
private ConstBasicMemberRefsearchTable(java.util.Vector table, java.lang.String cname, java.lang.String mname, java.lang.String sig)

    for (int i=0; i<table.size(); i++) {
      ConstBasicMemberRef memRef = (ConstBasicMemberRef) table.elementAt(i);
      if (memRef.className().asString().equals(cname) &&
	  memRef.nameAndType().name().asString().equals(mname) &&
	  memRef.nameAndType().signature().asString().equals(sig))
	return memRef;
    }
    return null;
  
voidsummarize()

    int stringSize = 0;
    int nStrings = 0;
    for (int i=0; i<pool.size(); i++) {
      ConstBasic c = constantAt(i);
      if (c != null && c.tag() == CONSTANTUtf8) {
	ConstUtf8 utf8 = (ConstUtf8) c;
	stringSize += utf8.asString().length();
	nStrings++;
      }
    }
    System.out.println("  " + nStrings + " strings totalling " + //NOI18N
		       stringSize + " bytes");//NOI18N
  
voidwrite(java.io.DataOutputStream buff)

    buff.writeShort(pool.size());
    for (int i=1; i<pool.size(); i++) {
      ConstBasic cb = (ConstBasic) pool.elementAt(i);
      if (cb != null) {
	buff.writeByte((byte) cb.tag());
	cb.formatData(buff);
      }
    }