home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 1999 March / maximum-cd-1999-03.iso / Feature / Lotus / ORGANIZE / COMPNENT / LTOUIN21.ZIP / sunw / demo / classfile / ClassFile.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-03-12  |  5.8 KB  |  224 lines

  1. package sunw.demo.classfile;
  2.  
  3. import java.io.DataOutputStream;
  4. import java.io.FilterOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.util.Hashtable;
  8. import java.util.Vector;
  9.  
  10. final class ClassFile {
  11.    private static final int MAGIC = -889275714;
  12.    private static final short MAJOR = 45;
  13.    private static final short MINOR = 3;
  14.    static final short ACC_PUBLIC = 1;
  15.    static final short ACC_FINAL = 16;
  16.    static final short ACC_SUPER = 32;
  17.    static final short ACC_INTERFACE = 512;
  18.    static final short ACC_ABSTRACT = 1024;
  19.    private Vector constantPool = new Vector(1);
  20.    private Hashtable constantHash = new Hashtable();
  21.    private short accessFlags = 33;
  22.    private ClassConstant thisClass;
  23.    private ClassConstant superClass;
  24.    private Vector interfaces;
  25.    private Vector fields;
  26.    private Vector methods;
  27.    private Vector attributes;
  28.  
  29.    ClassFile(String var1, String var2) {
  30.       this.thisClass = this.addClassConstant(var1);
  31.       this.superClass = this.addClassConstant(var2);
  32.    }
  33.  
  34.    public ClassConstant addClassConstant(String var1) {
  35.       return new ClassConstant(var1, this);
  36.    }
  37.  
  38.    public synchronized short addConstantPoolEntry(ConstantPoolEntry var1) {
  39.       ConstantPoolEntry var2 = (ConstantPoolEntry)this.constantHash.get(var1);
  40.       if (var2 != null) {
  41.          return var2.getConstantPoolIndex();
  42.       } else {
  43.          this.constantHash.put(var1, var1);
  44.          this.constantPool.addElement(var1);
  45.          return (short)this.constantPool.size();
  46.       }
  47.    }
  48.  
  49.    public FieldConstant addFieldConstant(String var1, String var2, String var3) {
  50.       return new FieldConstant(var1, var2, var3, this);
  51.    }
  52.  
  53.    public synchronized void addFieldDesc(FieldDesc var1) {
  54.       if (this.fields == null) {
  55.          this.fields = new Vector(1);
  56.       }
  57.  
  58.       this.fields.addElement(var1);
  59.    }
  60.  
  61.    public IntegerConstant addIntegerConstant(int var1) {
  62.       return new IntegerConstant(var1, this);
  63.    }
  64.  
  65.    public void addInterface(String var1) {
  66.       if (this.interfaces == null) {
  67.          this.interfaces = new Vector(1);
  68.       }
  69.  
  70.       this.interfaces.addElement(this.addClassConstant(var1));
  71.    }
  72.  
  73.    public MethodConstant addMethodConstant(String var1, String var2, String var3) {
  74.       return new MethodConstant(var1, var2, var3, this);
  75.    }
  76.  
  77.    public synchronized void addMethodDesc(MethodDesc var1) {
  78.       if (this.methods == null) {
  79.          this.methods = new Vector(1);
  80.       }
  81.  
  82.       this.methods.addElement(var1);
  83.    }
  84.  
  85.    public StringConstant addStringConstant(String var1) {
  86.       UTF8Constant var2 = this.addUTF8Constant(var1);
  87.       StringConstant var3 = new StringConstant(var2, this);
  88.       return var3;
  89.    }
  90.  
  91.    public UTF8Constant addUTF8Constant(String var1) {
  92.       return new UTF8Constant(var1, this);
  93.    }
  94.  
  95.    static boolean debug() {
  96.       return false;
  97.    }
  98.  
  99.    public static String fieldType(String var0) {
  100.       return "L" + fullyQualifiedForm(var0) + ";";
  101.    }
  102.  
  103.    public static String fullyQualifiedForm(String var0) {
  104.       return var0.replace('.', '/');
  105.    }
  106.  
  107.    public synchronized short getAccessFlags() {
  108.       return this.accessFlags;
  109.    }
  110.  
  111.    public synchronized void setAccessFlags(short var1) {
  112.       this.accessFlags = var1;
  113.    }
  114.  
  115.    public synchronized void write(OutputStream var1) throws IOException {
  116.       DataOutputStream var2 = new DataOutputStream(var1);
  117.  
  118.       try {
  119.          var2.writeInt(-889275714);
  120.          var2.writeShort(3);
  121.          var2.writeShort(45);
  122.          this.writeConstantPool(var2);
  123.          if (debug()) {
  124.             System.err.println("access: " + this.accessFlags);
  125.          }
  126.  
  127.          var2.writeShort(this.accessFlags);
  128.          var2.writeShort(this.thisClass.getConstantPoolIndex());
  129.          var2.writeShort(this.superClass.getConstantPoolIndex());
  130.          this.writeInterfaces(var2);
  131.          this.writeFields(var2);
  132.          this.writeMethods(var2);
  133.          this.writeAttributes(var2);
  134.          ((FilterOutputStream)var2).close();
  135.       } catch (IOException var3) {
  136.          System.err.println("Bad IO");
  137.       } catch (Exception var4) {
  138.          System.err.println("Oops");
  139.       }
  140.  
  141.    }
  142.  
  143.    private void writeAttributes(DataOutputStream var1) throws IOException {
  144.       if (this.attributes != null) {
  145.          if (debug()) {
  146.             System.err.println("write attributes: " + this.attributes.size());
  147.          }
  148.  
  149.          var1.writeShort(this.attributes.size());
  150.  
  151.          for(int var2 = 0; var2 < this.attributes.size(); ++var2) {
  152.             ((Attribute)this.attributes.elementAt(var2)).write(var1);
  153.          }
  154.       } else {
  155.          var1.writeShort(0);
  156.       }
  157.  
  158.    }
  159.  
  160.    private void writeConstantPool(DataOutputStream var1) throws IOException {
  161.       if (debug()) {
  162.          System.err.println("write constant pool: " + this.constantPool.size());
  163.       }
  164.  
  165.       var1.writeShort(this.constantPool.size() + 1);
  166.  
  167.       for(int var2 = 0; var2 < this.constantPool.size(); ++var2) {
  168.          ((ConstantPoolEntry)this.constantPool.elementAt(var2)).write(var1);
  169.       }
  170.  
  171.    }
  172.  
  173.    private void writeFields(DataOutputStream var1) throws IOException {
  174.       if (this.fields != null) {
  175.          if (debug()) {
  176.             System.err.println("write fields: " + this.fields.size());
  177.          }
  178.  
  179.          var1.writeShort(this.fields.size());
  180.  
  181.          for(int var2 = 0; var2 < this.fields.size(); ++var2) {
  182.             ((FieldDesc)this.fields.elementAt(var2)).write(var1);
  183.          }
  184.       } else {
  185.          var1.writeShort(0);
  186.       }
  187.  
  188.    }
  189.  
  190.    private void writeInterfaces(DataOutputStream var1) throws IOException {
  191.       if (this.interfaces != null) {
  192.          if (debug()) {
  193.             System.err.println("write interfaces: " + this.interfaces.size());
  194.          }
  195.  
  196.          var1.writeShort(this.interfaces.size());
  197.  
  198.          for(int var2 = 0; var2 < this.interfaces.size(); ++var2) {
  199.             var1.writeShort(((ConstantPoolEntry)this.interfaces.elementAt(var2)).getConstantPoolIndex());
  200.          }
  201.       } else {
  202.          var1.writeShort(0);
  203.       }
  204.  
  205.    }
  206.  
  207.    private void writeMethods(DataOutputStream var1) throws IOException {
  208.       if (this.methods != null) {
  209.          if (debug()) {
  210.             System.err.println("write methods: " + this.methods.size());
  211.          }
  212.  
  213.          var1.writeShort(this.methods.size());
  214.  
  215.          for(int var2 = 0; var2 < this.methods.size(); ++var2) {
  216.             ((MethodDesc)this.methods.elementAt(var2)).write(var1);
  217.          }
  218.       } else {
  219.          var1.writeShort(0);
  220.       }
  221.  
  222.    }
  223. }
  224.