home *** CD-ROM | disk | FTP | other *** search
- package sunw.demo.classfile;
-
- import java.io.DataOutputStream;
- import java.io.FilterOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.Hashtable;
- import java.util.Vector;
-
- final class ClassFile {
- private static final int MAGIC = -889275714;
- private static final short MAJOR = 45;
- private static final short MINOR = 3;
- static final short ACC_PUBLIC = 1;
- static final short ACC_FINAL = 16;
- static final short ACC_SUPER = 32;
- static final short ACC_INTERFACE = 512;
- static final short ACC_ABSTRACT = 1024;
- private Vector constantPool = new Vector(1);
- private Hashtable constantHash = new Hashtable();
- private short accessFlags = 33;
- private ClassConstant thisClass;
- private ClassConstant superClass;
- private Vector interfaces;
- private Vector fields;
- private Vector methods;
- private Vector attributes;
-
- ClassFile(String var1, String var2) {
- this.thisClass = this.addClassConstant(var1);
- this.superClass = this.addClassConstant(var2);
- }
-
- public ClassConstant addClassConstant(String var1) {
- return new ClassConstant(var1, this);
- }
-
- public synchronized short addConstantPoolEntry(ConstantPoolEntry var1) {
- ConstantPoolEntry var2 = (ConstantPoolEntry)this.constantHash.get(var1);
- if (var2 != null) {
- return var2.getConstantPoolIndex();
- } else {
- this.constantHash.put(var1, var1);
- this.constantPool.addElement(var1);
- return (short)this.constantPool.size();
- }
- }
-
- public FieldConstant addFieldConstant(String var1, String var2, String var3) {
- return new FieldConstant(var1, var2, var3, this);
- }
-
- public synchronized void addFieldDesc(FieldDesc var1) {
- if (this.fields == null) {
- this.fields = new Vector(1);
- }
-
- this.fields.addElement(var1);
- }
-
- public IntegerConstant addIntegerConstant(int var1) {
- return new IntegerConstant(var1, this);
- }
-
- public void addInterface(String var1) {
- if (this.interfaces == null) {
- this.interfaces = new Vector(1);
- }
-
- this.interfaces.addElement(this.addClassConstant(var1));
- }
-
- public MethodConstant addMethodConstant(String var1, String var2, String var3) {
- return new MethodConstant(var1, var2, var3, this);
- }
-
- public synchronized void addMethodDesc(MethodDesc var1) {
- if (this.methods == null) {
- this.methods = new Vector(1);
- }
-
- this.methods.addElement(var1);
- }
-
- public StringConstant addStringConstant(String var1) {
- UTF8Constant var2 = this.addUTF8Constant(var1);
- StringConstant var3 = new StringConstant(var2, this);
- return var3;
- }
-
- public UTF8Constant addUTF8Constant(String var1) {
- return new UTF8Constant(var1, this);
- }
-
- static boolean debug() {
- return false;
- }
-
- public static String fieldType(String var0) {
- return "L" + fullyQualifiedForm(var0) + ";";
- }
-
- public static String fullyQualifiedForm(String var0) {
- return var0.replace('.', '/');
- }
-
- public synchronized short getAccessFlags() {
- return this.accessFlags;
- }
-
- public synchronized void setAccessFlags(short var1) {
- this.accessFlags = var1;
- }
-
- public synchronized void write(OutputStream var1) throws IOException {
- DataOutputStream var2 = new DataOutputStream(var1);
-
- try {
- var2.writeInt(-889275714);
- var2.writeShort(3);
- var2.writeShort(45);
- this.writeConstantPool(var2);
- if (debug()) {
- System.err.println("access: " + this.accessFlags);
- }
-
- var2.writeShort(this.accessFlags);
- var2.writeShort(this.thisClass.getConstantPoolIndex());
- var2.writeShort(this.superClass.getConstantPoolIndex());
- this.writeInterfaces(var2);
- this.writeFields(var2);
- this.writeMethods(var2);
- this.writeAttributes(var2);
- ((FilterOutputStream)var2).close();
- } catch (IOException var3) {
- System.err.println("Bad IO");
- } catch (Exception var4) {
- System.err.println("Oops");
- }
-
- }
-
- private void writeAttributes(DataOutputStream var1) throws IOException {
- if (this.attributes != null) {
- if (debug()) {
- System.err.println("write attributes: " + this.attributes.size());
- }
-
- var1.writeShort(this.attributes.size());
-
- for(int var2 = 0; var2 < this.attributes.size(); ++var2) {
- ((Attribute)this.attributes.elementAt(var2)).write(var1);
- }
- } else {
- var1.writeShort(0);
- }
-
- }
-
- private void writeConstantPool(DataOutputStream var1) throws IOException {
- if (debug()) {
- System.err.println("write constant pool: " + this.constantPool.size());
- }
-
- var1.writeShort(this.constantPool.size() + 1);
-
- for(int var2 = 0; var2 < this.constantPool.size(); ++var2) {
- ((ConstantPoolEntry)this.constantPool.elementAt(var2)).write(var1);
- }
-
- }
-
- private void writeFields(DataOutputStream var1) throws IOException {
- if (this.fields != null) {
- if (debug()) {
- System.err.println("write fields: " + this.fields.size());
- }
-
- var1.writeShort(this.fields.size());
-
- for(int var2 = 0; var2 < this.fields.size(); ++var2) {
- ((FieldDesc)this.fields.elementAt(var2)).write(var1);
- }
- } else {
- var1.writeShort(0);
- }
-
- }
-
- private void writeInterfaces(DataOutputStream var1) throws IOException {
- if (this.interfaces != null) {
- if (debug()) {
- System.err.println("write interfaces: " + this.interfaces.size());
- }
-
- var1.writeShort(this.interfaces.size());
-
- for(int var2 = 0; var2 < this.interfaces.size(); ++var2) {
- var1.writeShort(((ConstantPoolEntry)this.interfaces.elementAt(var2)).getConstantPoolIndex());
- }
- } else {
- var1.writeShort(0);
- }
-
- }
-
- private void writeMethods(DataOutputStream var1) throws IOException {
- if (this.methods != null) {
- if (debug()) {
- System.err.println("write methods: " + this.methods.size());
- }
-
- var1.writeShort(this.methods.size());
-
- for(int var2 = 0; var2 < this.methods.size(); ++var2) {
- ((MethodDesc)this.methods.elementAt(var2)).write(var1);
- }
- } else {
- var1.writeShort(0);
- }
-
- }
- }
-