From 0087af3d283954bbeeaaa857914ce9e06c39fcc7 Mon Sep 17 00:00:00 2001 From: brian Date: Thu, 27 May 2004 07:03:46 +0000 Subject: [PATCH] cp cleanup, setsourcefile darcs-hash:20040527070346-24bed-d9812c9e818a589db4cd6467655dad5ba36585a1.gz --- src/org/ibex/classgen/AttrGen.java | 32 ------------------ src/org/ibex/classgen/CPGen.java | 11 ++++--- src/org/ibex/classgen/ClassGen.java | 60 +++++++++++++++++++++++++++------- src/org/ibex/classgen/FieldGen.java | 4 +-- src/org/ibex/classgen/MethodGen.java | 11 +++---- 5 files changed, 61 insertions(+), 57 deletions(-) delete mode 100644 src/org/ibex/classgen/AttrGen.java diff --git a/src/org/ibex/classgen/AttrGen.java b/src/org/ibex/classgen/AttrGen.java deleted file mode 100644 index 758d9f0..0000000 --- a/src/org/ibex/classgen/AttrGen.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.ibex.classgen; - -import java.io.*; -import java.util.*; - -public class AttrGen { - private final CPGen cp; - private final Hashtable ht = new Hashtable(); - - public AttrGen(CPGen cp) { - this.cp = cp; - } - - public void add(String s, byte[] data) { - cp.addUtf8(s); - ht.put(s,data); - } - - public boolean contains(String s) { return ht.get(s) != null; } - - public int size() { return ht.size(); } - - public void dump(DataOutput o) throws IOException { - for(Enumeration e = ht.keys(); e.hasMoreElements();) { - String name = (String) e.nextElement(); - byte[] val = (byte[]) ht.get(name); - o.writeShort(cp.getUtf8Index(name)); - o.writeInt(val.length); - o.write(val); - } - } -} diff --git a/src/org/ibex/classgen/CPGen.java b/src/org/ibex/classgen/CPGen.java index dece6c2..56d5149 100644 --- a/src/org/ibex/classgen/CPGen.java +++ b/src/org/ibex/classgen/CPGen.java @@ -11,7 +11,10 @@ class CPGen { 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() { } @@ -71,7 +74,8 @@ class CPGen { /* * 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)); } @@ -89,9 +93,8 @@ class CPGen { public final Ent addNameAndType(String name, String descriptor) { return add(new ClassGen.NameAndType(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; diff --git a/src/org/ibex/classgen/ClassGen.java b/src/org/ibex/classgen/ClassGen.java index fd732ec..de32d3c 100644 --- a/src/org/ibex/classgen/ClassGen.java +++ b/src/org/ibex/classgen/ClassGen.java @@ -7,6 +7,7 @@ public class ClassGen implements CGConst { private Type.Object thisType; private Type.Object superType; int flags; + private String sourceFile; private Vector interfaces = new Vector(); private Vector fields = new Vector(); @@ -42,6 +43,8 @@ public class ClassGen implements CGConst { return fg; } + public void setSourceFile(String sourceFile) { this.sourceFile = sourceFile; } + public void dump(String s) throws IOException { dump(new File(s)); } public void dump(File f) throws IOException { if(f.isDirectory()) { @@ -55,6 +58,7 @@ public class ClassGen implements CGConst { } dump(new FileOutputStream(f)); } + public void dump(OutputStream os) throws IOException { DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(os)); _dump(dos); @@ -65,7 +69,12 @@ public class ClassGen implements CGConst { cp.add(thisType); cp.add(superType); for(int i=0;i