KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > sitraka > bytecode > ClassFile


1 /*
2  * Copyright 2001-2002,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.tools.ant.taskdefs.optional.sitraka.bytecode;
18
19 import java.io.DataInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ClassCPInfo;
23 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool;
24 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.Utf8CPInfo;
25 import org.apache.tools.ant.taskdefs.optional.sitraka.bytecode.attributes.AttributeInfo;
26
27
28 /**
29  * Object representing a class.
30  *
31  * Information are kept to the strict minimum for JProbe reports so
32  * that not too many objects are created for a class, otherwise the
33  * JVM can quickly run out of memory when analyzing a great deal of
34  * classes and keeping them in memory for global analysis.
35  *
36  */

37 public final class ClassFile {
38
39     private MethodInfo[] methods;
40
41     private String JavaDoc sourceFile;
42
43     private String JavaDoc fullname;
44
45     private int access_flags;
46
47     public ClassFile(InputStream JavaDoc is) throws IOException JavaDoc {
48         DataInputStream JavaDoc dis = new DataInputStream JavaDoc(is);
49         ConstantPool constantPool = new ConstantPool();
50
51         /* int magic = */ dis.readInt(); // 0xCAFEBABE
52
/* int minor = */ dis.readShort();
53         /* int major = */ dis.readShort();
54
55         constantPool.read(dis);
56         constantPool.resolve();
57
58         // class information
59
access_flags = dis.readShort();
60         int this_class = dis.readShort();
61         fullname = ((ClassCPInfo) constantPool.getEntry(this_class)).getClassName().replace('/', '.');
62         /* int super_class = */ dis.readShort();
63
64         // skip interfaces...
65
int count = dis.readShort();
66         dis.skipBytes(count * 2); // short
67

68         // skip fields...
69
int numFields = dis.readShort();
70         for (int i = 0; i < numFields; i++) {
71             // 3 short: access flags, name index, descriptor index
72
dis.skip(2 * 3);
73             // attribute list...
74
int attributes_count = dis.readUnsignedShort();
75             for (int j = 0; j < attributes_count; j++) {
76                 dis.skipBytes(2); // skip attr_id (short)
77
int len = dis.readInt();
78                 dis.skipBytes(len);
79             }
80         }
81
82         // read methods
83
int method_count = dis.readShort();
84         methods = new MethodInfo[method_count];
85         for (int i = 0; i < method_count; i++) {
86             methods[i] = new MethodInfo();
87             methods[i].read(constantPool, dis);
88         }
89
90         // get interesting attributes.
91
int attributes_count = dis.readUnsignedShort();
92         for (int j = 0; j < attributes_count; j++) {
93             int attr_id = dis.readShort();
94             int len = dis.readInt();
95             String JavaDoc attr_name = Utils.getUTF8Value(constantPool, attr_id);
96             if (AttributeInfo.SOURCE_FILE.equals(attr_name)) {
97                 int name_index = dis.readShort();
98                 sourceFile = ((Utf8CPInfo) constantPool.getEntry(name_index)).getValue();
99             } else {
100                 dis.skipBytes(len);
101             }
102         }
103     }
104
105     public int getAccess() {
106         return access_flags;
107     }
108
109     public String JavaDoc getSourceFile() {
110         return sourceFile;
111     }
112
113     public MethodInfo[] getMethods() {
114         return methods;
115     }
116
117     public String JavaDoc getFullName() {
118         return fullname;
119     }
120
121     public String JavaDoc getName() {
122         String JavaDoc name = getFullName();
123         int pos = name.lastIndexOf('.');
124         if (pos == -1) {
125             return "";
126         }
127         return name.substring(pos + 1);
128     }
129
130     public String JavaDoc getPackage() {
131         String JavaDoc name = getFullName();
132         int pos = name.lastIndexOf('.');
133         if (pos == -1) {
134             return "";
135         }
136         return name.substring(0, pos);
137     }
138
139 }
140
141
142
143
144
145
Popular Tags