KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > compiler > ClassFile


1 // Copyright (c) Corporation for National Research Initiatives
2

3 package org.python.compiler;
4 import java.util.*;
5 import java.io.*;
6
7 class Method
8 {
9     int access, name, type;
10     Attribute[] atts;
11
12     public Method(int name, int type, int access, Attribute[] atts) {
13         this.name = name;
14         this.type = type;
15         this.access = access;
16         this.atts = atts;
17     }
18
19     public void write(DataOutputStream stream) throws IOException {
20         stream.writeShort(access);
21         stream.writeShort(name);
22         stream.writeShort(type);
23         ClassFile.writeAttributes(stream, atts);
24     }
25
26 }
27
28 public class ClassFile
29 {
30     ConstantPool pool;
31     int access;
32     public String JavaDoc name;
33     String JavaDoc superclass;
34     int[] interfaces;
35     Vector methods;
36     Vector fields;
37     Vector attributes;
38
39     public final static int PUBLIC = 0x1;
40     public final static int PRIVATE = 0x2;
41     public final static int PROTECTED = 0x4;
42     public final static int STATIC = 0x8;
43     public final static int FINAL = 0x10;
44     public final static int SYNCHRONIZED = 0x20;
45     public final static int NATIVE = 0x100;
46     public final static int ABSTRACT = 0x400;
47
48     public static String JavaDoc fixName(String JavaDoc n) {
49         if (n.indexOf('.') == -1)
50             return n;
51         char[] c = n.toCharArray();
52         for(int i=0; i<c.length; i++) {
53             if (c[i] == '.') c[i] = '/';
54         }
55         return new String JavaDoc(c);
56     }
57
58     public ClassFile(String JavaDoc name) {
59         this(name, "java/lang/Object", SYNCHRONIZED | PUBLIC);
60     }
61
62     public ClassFile(String JavaDoc name, String JavaDoc superclass, int access) {
63         this.name = fixName(name);
64         this.superclass = fixName(superclass);
65         this.interfaces = new int[0];
66         this.access = access;
67
68         pool = new ConstantPool();
69         methods = new Vector();
70         fields = new Vector();
71         attributes = new Vector();
72     }
73
74     public void addInterface(String JavaDoc name) throws IOException {
75         int[] new_interfaces = new int[interfaces.length+1];
76         System.arraycopy(interfaces, 0, new_interfaces, 0, interfaces.length);
77         new_interfaces[interfaces.length] = pool.Class(name);
78         interfaces = new_interfaces;
79     }
80
81     public Code addMethod(String JavaDoc name, String JavaDoc type, int access)
82         throws IOException
83     {
84         Code code = new Code(type, pool, (access & STATIC) == STATIC);
85         Method m = new Method(pool.UTF8(name), pool.UTF8(type), access,
86                               new Attribute[] {code});
87         methods.addElement(m);
88         return code;
89     }
90
91     public void addField(String JavaDoc name, String JavaDoc type, int access)
92         throws IOException
93     {
94         Method m = new Method(pool.UTF8(name), pool.UTF8(type), access,
95                               new Attribute[0]);
96         fields.addElement(m);
97     }
98
99     public static void writeAttributes(DataOutputStream stream,
100                                        Attribute[] atts)
101         throws IOException
102     {
103         stream.writeShort(atts.length);
104         for (int i=0; i<atts.length; i++) {
105             atts[i].write(stream);
106         }
107     }
108
109     public void writeMethods(DataOutputStream stream, Vector methods)
110         throws IOException
111     {
112         stream.writeShort(methods.size());
113         for (int i=0; i<methods.size(); i++) {
114             Method m = (Method)methods.elementAt(i);
115             m.write(stream);
116         }
117     }
118
119     public void addAttribute(Attribute attr) throws IOException {
120         attributes.addElement(attr);
121     }
122
123     public void write(DataOutputStream stream) throws IOException {
124         //Write Header
125
int thisclass = pool.Class(name);
126         int superclass = pool.Class(this.superclass);
127
128         stream.writeInt(0xcafebabe);
129         stream.writeShort(0x3);
130         stream.writeShort(0x2d);
131
132         pool.write(stream);
133
134         stream.writeShort(access);
135         stream.writeShort(thisclass);
136         stream.writeShort(superclass);
137
138         //write out interfaces
139
stream.writeShort(interfaces.length);
140         for (int i=0; i<interfaces.length; i++)
141             stream.writeShort(interfaces[i]);
142
143         writeMethods(stream, fields);
144         writeMethods(stream, methods);
145
146         //write out class attributes
147
int n = attributes.size();
148         stream.writeShort(n);
149
150         for (int i=0; i<n; i++) {
151             ((Attribute)attributes.elementAt(i)).write(stream);
152         }
153     }
154
155     public void write(OutputStream stream) throws IOException {
156         write(new DataOutputStream(stream));
157     }
158 }
159
Popular Tags