}
f = new File(f,a[i] + ".class");
}
- dump(new FileOutputStream(f));
+ OutputStream os = new FileOutputStream(f);
+ dump(os);
+ os.close();
}
/** Writes the classfile data to the outputstream specified
}
private void _dump(DataOutput o) throws IOException {
+ cp.optimize();
+ cp.stable();
+
cp.add(thisType);
cp.add(superType);
if(interfaces != null) for(int i=0;i<interfaces.length;i++) cp.add(interfaces[i]);
if(sourceFile != null && !attributes.contains("SourceFile")) attributes.add("SourceFile",cp.addUtf8(sourceFile));
-
- cp.stable();
-
+
for(int i=0;i<methods.size();i++) ((MethodGen)methods.elementAt(i)).finish();
for(int i=0;i<fields.size();i++) ((FieldGen)fields.elementAt(i)).finish();
o.writeShort(3); // minor_version
o.writeShort(45); // major_version
- o.writeShort(cp.size()); // constant_pool_count
cp.dump(o); // constant_pool
o.writeShort(flags);
attributes.dump(o); // attributes
}
+ public ClassGen read(File f) throws ClassReadExn {
+ InputStream is = new FileInputStream(f);
+ ClassGen ret = read(is);
+ is.close();
+ return ret;
+ }
+
+ public ClassGen read(InputStream is) throws ClassReadExn {
+ return new ClassGen(new DataInputStream(new BufferedInputStream(is)));
+ }
+
+ ClassGen(DataInput i) throws ClassReadExn {
+ if(i.readInt() != 0xcadebabe) throw new ClassReadExn("invalid magic");
+ short minor = i.readInt();
+ if(minor != 3) throw new ClassReadExn("invalid minor version: " + minor);
+ if(i.readInt() != 45) throw new ClassReadExn("invalid major version");
+ cp = new CPGen(i);
+ }
+
/** Thrown when class generation fails for a reason not under the control of the user
(IllegalStateExceptions are thrown in those cases */
public static class Exn extends RuntimeException {
public Exn(String s) { super(s); }
}
+ public static class ClassReadExn extends IOException {
+ public ClassReadExn(String s) { super(s); }
+ }
+
/** A class representing a field or method reference. This is used as an argument to the INVOKE*, GET*, and PUT* bytecodes
@see MethodRef
@see FieldRef
o.write(buf);
} else if(val instanceof CPGen.Ent) {
o.writeInt(2);
- o.writeShort(((CPGen.Ent)val).getIndex());
+ o.writeShort(cp.getIndex((CPGen.Ent)val));
} else {
throw new Error("should never happen");
}