private Hashtable entries = new Hashtable();
private int nextIndex = 1; // 0 is reserved
private int count;
- private boolean sealed;
+ private int state;
+ private static final int OPEN = 0;
+ private static final int STABLE = 1; // existing entries won't change
+ private static final int SEALED = 2; // no new entries
CPGen() { }
/*
* Entries
*/
- abstract static class Ent implements Sort.Comparable {
+ abstract static class Ent {
int index;
int tag;
int getIndex() { return index; }
void dump(DataOutput o) throws IOException { o.writeByte(tag); }
-
- public int compareTo(Object o) {
- if(!(o instanceof Ent)) return 1;
- int oi = ((Ent)o).index;
- if(index < oi) return -1;
- if(index > oi) return 1;
- return 0;
- }
}
static class OneU4Ent extends Ent {
public int hashCode() { return ~s.hashCode(); }
}
+ static class NameAndTypeKey {
+ String name;
+ String type;
+ NameAndTypeKey(String name, String type) { this.name = name; this.type = type; }
+ public boolean equals(Object o_) {
+ if(!(o_ instanceof NameAndTypeKey)) return false;
+ NameAndTypeKey o = (NameAndTypeKey) o_;
+ return o.name.equals(name) && o.type.equals(type);
+ }
+ public int hashCode() { return name.hashCode() ^ type.hashCode(); }
+ }
+
/*
* Methods
*/
- public void seal() { sealed = true; }
+ public void seal() { if(state > SEALED) throw new IllegalStateException(); state = SEALED; }
+ public void stable() { if(state > STABLE) throw new IllegalStateException(); state = STABLE; }
public final Ent get(Object o) { return (Ent) entries.get(o); }
public final Ent getUtf8(String s) { return get(new Utf8Key(s)); }
return e.getIndex();
}
- public final Ent addNameAndType(String name, String descriptor) { return add(new ClassGen.NameAndType(name,descriptor)); }
+ public final Ent addNameAndType(String name, String descriptor) { return add(new NameAndTypeKey(name,descriptor)); }
public final Ent addUtf8(String s) { return add(new Utf8Key(s)); }
- // FEATURE: Don't resolve indexes until dump (for optimize)
public final Ent add(Object o) {
- if(sealed) throw new IllegalStateException("constant pool is sealed");
+ if(state == SEALED) throw new IllegalStateException("constant pool is sealed");
Ent ent = get(o);
if(ent != null) return ent;
- if(nextIndex == 65536) throw new ClassGen.Exn("constant pool full");
-
if(o instanceof Type.Object) {
CPRefEnt ce = new CPRefEnt(7);
ce.e1 = addUtf8(((Type.Object)o).internalForm());
Utf8Ent ue = new Utf8Ent();
ue.s = ((Utf8Key)o).s;
ent = ue;
- } else if(o instanceof ClassGen.NameAndType) {
+ } else if(o instanceof NameAndTypeKey) {
CPRefEnt ce = new CPRefEnt(12);
- ClassGen.NameAndType key = (ClassGen.NameAndType) o;
+ NameAndTypeKey key = (NameAndTypeKey) o;
ce.e1 = addUtf8(key.name);
ce.e2 = addUtf8(key.type);
ent = ce;
- } else if(o instanceof ClassGen.FieldMethodRef) {
- ClassGen.FieldMethodRef key = (ClassGen.FieldMethodRef) o;
- int tag = o instanceof FieldRef ? 9 : o instanceof MethodRef ? 10 : o instanceof ClassGen.InterfaceMethodRef ? 11 : 0;
+ } else if(o instanceof ClassGen.FieldOrMethodRef) {
+ ClassGen.FieldOrMethodRef key = (ClassGen.FieldOrMethodRef) o;
+ int tag = o instanceof FieldRef ? 9 : o instanceof MethodRef ? 10 : o instanceof MethodRef.I ? 11 : 0;
if(tag == 0) throw new Error("should never happen");
CPRefEnt ce = new CPRefEnt(tag);
ce.e1 = add(key.klass);
- ce.e2 = add(key.nameAndType);
+ ce.e2 = addNameAndType(key.name,key.descriptor);
ent = ce;
} else {
throw new IllegalArgumentException("Unknown type passed to add");
}
- ent.index = nextIndex++;
- if(ent instanceof LongEnt) nextIndex++;
+ int spaces = ent instanceof LongEnt ? 2 : 1;
+
+ if(nextIndex + spaces > 65536) throw new ClassGen.Exn("constant pool full");
+
+ ent.index = nextIndex;
+ nextIndex += spaces;
count++;
entries.put(o,ent);
public int size() { return nextIndex; }
+ private static final Sort.CompareFunc compareFunc = new Sort.CompareFunc() {
+ public int compare(Object a_, Object b_) {
+ return ((Ent)a_).index - ((Ent)b_).index;
+ }
+ };
public void dump(DataOutput o) throws IOException {
Ent[] ents = new Ent[count];
int i=0;
Enumeration e = entries.keys();
while(e.hasMoreElements()) ents[i++] = (Ent) entries.get(e.nextElement());
if(i != count) throw new Error("should never happen");
- Sort.sort(ents);
+ Sort.sort(ents,compareFunc);
for(i=0;i<ents.length;i++) {
- System.err.println("" + (i+1) + ": " + ents[i]);
+ //System.err.println("" + (i+1) + ": " + ents[i]);
ents[i].dump(o);
}
}