KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > bytecode > ClassFile


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.bytecode;
26
27 import java.io.*;
28 import java.lang.reflect.Modifier JavaDoc;
29
30 public class ClassFile {
31     ConstantPool pool;
32
33     int modifiers;
34     ConstantPool.Class thisClass;
35     ConstantPool.Class superClass;
36     ConstantPool.Class[] interfaces;
37
38     FieldInfo[] fields;
39     MethodInfo[] methods;
40
41     Attributes attributes;
42
43     //public ClassFile(String filename) {
44
//}
45

46     public String JavaDoc getClassName() {
47         return thisClass.getName();
48     }
49
50     private void assertTrue(boolean test, String JavaDoc message) {
51         if (!test) throw new RuntimeException JavaDoc(message);
52     }
53
54     void readConstantPool(DataInputStream stream) throws IOException {
55         pool = new ConstantPool();
56         pool.readFrom(stream);
57     }
58
59     void readInterfaces(DataInputStream stream) throws IOException {
60         int interfacesCount = stream.readUnsignedShort();
61         interfaces = new ConstantPool.Class[interfacesCount];
62         for(int i=0; i<interfacesCount; i++) {
63             interfaces[i] = pool.getClass(stream.readUnsignedShort());
64         }
65     }
66
67     class MemberInfo {
68         int modifiers;
69         ConstantPool.UTF name;
70         ConstantPool.UTF descriptor;
71         Attributes attributes;
72
73         void readFrom(DataInputStream stream) throws IOException {
74             modifiers = stream.readUnsignedShort();
75             name = pool.getUTF(stream.readUnsignedShort());
76             descriptor = pool.getUTF(stream.readUnsignedShort());
77             attributes = Attributes.createFrom(ClassFile.this, stream);
78         }
79
80         public ConstantPool.Class[] getExceptions() {
81             Attributes.ExceptionsAttribute excs =
82                     (Attributes.ExceptionsAttribute)
83                         attributes.findAttribute(Attributes.ExceptionsAttribute.class);
84             if (excs == null) return null;
85             else return excs.exceptions;
86         }
87
88         public boolean isDeprecated() {
89             return attributes.findAttribute(Attributes.DeprecatedAttribute.class) != null;
90         }
91
92
93         public String JavaDoc toString() {
94             return Modifier.toString(modifiers)+" "+name.value+" "+descriptor.value+": "+attributes;
95         }
96     }
97
98     class FieldInfo extends MemberInfo {}
99
100     void readFields(DataInputStream stream) throws IOException {
101         int fieldsCount = stream.readUnsignedShort();
102         fields = new FieldInfo[fieldsCount];
103         for(int i=0; i<fieldsCount; i++) {
104             fields[i] = new FieldInfo();
105             fields[i].readFrom(stream);
106         }
107     }
108
109     class MethodInfo extends MemberInfo {
110         //public ConstantPool.Class[] getExceptions() {
111
//
112
//}
113
}
114
115     void readMethods(DataInputStream stream) throws IOException {
116         int methodsCount = stream.readUnsignedShort();
117         methods = new MethodInfo[methodsCount];
118         for(int i=0; i<methodsCount; i++) {
119             methods[i] = new MethodInfo();
120             methods[i].readFrom(stream);
121         }
122     }
123
124     void readAttributes(DataInputStream stream) throws IOException {
125         attributes = Attributes.createFrom(this, stream);
126     }
127
128     public void readFrom(DataInputStream stream) throws IOException {
129         int magic = stream.readInt();
130         int minorVersion = stream.readUnsignedShort();
131         int majorVersion = stream.readUnsignedShort();
132         assertTrue(magic == 0xCAFEBABE, "bad magic number: "+Integer.toString(magic, 16));
133         //XXX think more carefully about these constraints
134
//XXX we need to support 45, 46, and 47 at a minimum to handle jdk1.1, 1.2, and 1.3
135
assertTrue(majorVersion >= 45, "bad major version: "+majorVersion);
136         //assert(minorVersion >= 3, "bad minor version: "+minorVersion);
137

138         readConstantPool(stream);
139
140         modifiers = stream.readUnsignedShort();
141
142         thisClass = pool.getClass(stream.readUnsignedShort());
143         superClass = pool.getClass(stream.readUnsignedShort());
144
145         readInterfaces(stream);
146         readFields(stream);
147         readMethods(stream);
148         readAttributes(stream);
149     }
150
151     void print() {
152         System.err.println("modifiers: "+Modifier.toString(modifiers));
153         System.err.println("this: "+thisClass);
154         System.err.println("extends: "+superClass);
155         System.err.println("interfaces");
156         printObjects(interfaces);
157         System.err.println("fields");
158         printObjects(fields);
159         System.err.println("methods");
160         printObjects(methods);
161         System.err.println("attributes: "+attributes);
162     }
163
164     static void printObjects(Object JavaDoc[] objects) {
165         for(int i=0; i<objects.length; i++) {
166             System.err.println(""+i+": "+objects[i]);
167         }
168     }
169
170     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
171         ClassFile cf = new ClassFile();
172         cf.readFrom(new DataInputStream(new FileInputStream(args[0])));
173         cf.print();
174         for (int i = 0; i < cf.pool.constants.length; i++) {
175             System.err.println(cf.pool.constants[i]);
176         }
177     }
178 }
179
Popular Tags