001: // Copyright (c) Corporation for National Research Initiatives
002:
003: package org.python.compiler;
004:
005: import java.util.*;
006: import java.io.*;
007:
008: class Method {
009: int access, name, type;
010: Attribute[] atts;
011:
012: public Method(int name, int type, int access, Attribute[] atts) {
013: this .name = name;
014: this .type = type;
015: this .access = access;
016: this .atts = atts;
017: }
018:
019: public void write(DataOutputStream stream) throws IOException {
020: stream.writeShort(access);
021: stream.writeShort(name);
022: stream.writeShort(type);
023: ClassFile.writeAttributes(stream, atts);
024: }
025:
026: }
027:
028: public class ClassFile {
029: ConstantPool pool;
030: int access;
031: public String name;
032: String super class;
033: int[] interfaces;
034: Vector methods;
035: Vector fields;
036: Vector attributes;
037:
038: public final static int PUBLIC = 0x1;
039: public final static int PRIVATE = 0x2;
040: public final static int PROTECTED = 0x4;
041: public final static int STATIC = 0x8;
042: public final static int FINAL = 0x10;
043: public final static int SYNCHRONIZED = 0x20;
044: public final static int NATIVE = 0x100;
045: public final static int ABSTRACT = 0x400;
046:
047: public static String fixName(String n) {
048: if (n.indexOf('.') == -1)
049: return n;
050: char[] c = n.toCharArray();
051: for (int i = 0; i < c.length; i++) {
052: if (c[i] == '.')
053: c[i] = '/';
054: }
055: return new String(c);
056: }
057:
058: public ClassFile(String name) {
059: this (name, "java/lang/Object", SYNCHRONIZED | PUBLIC);
060: }
061:
062: public ClassFile(String name, String super class, int access) {
063: this .name = fixName(name);
064: this .super class = fixName(super class);
065: this .interfaces = new int[0];
066: this .access = access;
067:
068: pool = new ConstantPool();
069: methods = new Vector();
070: fields = new Vector();
071: attributes = new Vector();
072: }
073:
074: public void addInterface(String name) throws IOException {
075: int[] new_interfaces = new int[interfaces.length + 1];
076: System.arraycopy(interfaces, 0, new_interfaces, 0,
077: interfaces.length);
078: new_interfaces[interfaces.length] = pool.Class(name);
079: interfaces = new_interfaces;
080: }
081:
082: public Code addMethod(String name, String type, int access)
083: throws IOException {
084: Code code = new Code(type, pool, (access & STATIC) == STATIC);
085: Method m = new Method(pool.UTF8(name), pool.UTF8(type), access,
086: new Attribute[] { code });
087: methods.addElement(m);
088: return code;
089: }
090:
091: public void addField(String name, String type, int access)
092: throws IOException {
093: Method m = new Method(pool.UTF8(name), pool.UTF8(type), access,
094: new Attribute[0]);
095: fields.addElement(m);
096: }
097:
098: public static void writeAttributes(DataOutputStream stream,
099: Attribute[] atts) throws IOException {
100: stream.writeShort(atts.length);
101: for (int i = 0; i < atts.length; i++) {
102: atts[i].write(stream);
103: }
104: }
105:
106: public void writeMethods(DataOutputStream stream, Vector methods)
107: throws IOException {
108: stream.writeShort(methods.size());
109: for (int i = 0; i < methods.size(); i++) {
110: Method m = (Method) methods.elementAt(i);
111: m.write(stream);
112: }
113: }
114:
115: public void addAttribute(Attribute attr) throws IOException {
116: attributes.addElement(attr);
117: }
118:
119: public void write(DataOutputStream stream) throws IOException {
120: //Write Header
121: int this class = pool.Class(name);
122: int super class = pool.Class(this .super class);
123:
124: stream.writeInt(0xcafebabe);
125: stream.writeShort(0x3);
126: stream.writeShort(0x2d);
127:
128: pool.write(stream);
129:
130: stream.writeShort(access);
131: stream.writeShort(this class);
132: stream.writeShort(super class);
133:
134: //write out interfaces
135: stream.writeShort(interfaces.length);
136: for (int i = 0; i < interfaces.length; i++)
137: stream.writeShort(interfaces[i]);
138:
139: writeMethods(stream, fields);
140: writeMethods(stream, methods);
141:
142: //write out class attributes
143: int n = attributes.size();
144: stream.writeShort(n);
145:
146: for (int i = 0; i < n; i++) {
147: ((Attribute) attributes.elementAt(i)).write(stream);
148: }
149: }
150:
151: public void write(OutputStream stream) throws IOException {
152: write(new DataOutputStream(stream));
153: }
154: }
|