X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fclassgen%2FCPGen.java;h=6b3db97df06ef43a6b458800c8cb0a8358ba0763;hb=85004bd030f69603a32140de6f61891c349b6b4f;hp=56d51497461d15afb094c7c0061c625191d1ae83;hpb=0087af3d283954bbeeaaa857914ce9e06c39fcc7;p=org.ibex.classgen.git diff --git a/src/org/ibex/classgen/CPGen.java b/src/org/ibex/classgen/CPGen.java index 56d5149..6b3db97 100644 --- a/src/org/ibex/classgen/CPGen.java +++ b/src/org/ibex/classgen/CPGen.java @@ -71,6 +71,18 @@ class CPGen { 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 */ @@ -90,7 +102,7 @@ class CPGen { 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)); } public final Ent add(Object o) { @@ -99,8 +111,6 @@ class CPGen { 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()); @@ -129,26 +139,30 @@ class CPGen { 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);