KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 package org.apache.tools.ant.taskdefs.optional.depend;
19
20 import java.io.DataInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ClassCPInfo;
25 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool;
26 import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPoolEntry;
27
28 /**
29  * A ClassFile object stores information about a Java class. The class may
30  * be read from a DataInputStream.and written to a DataOutputStream. These
31  * are usually streams from a Java class file or a class file component of a
32  * Jar file.
33  *
34  */

35 public class ClassFile {
36
37     /** The Magic Value that marks the start of a Java class file */
38     private static final int CLASS_MAGIC = 0xCAFEBABE;
39
40     /** This class' constant pool. */
41     private ConstantPool constantPool;
42
43     /** The class name for this class. */
44     private String JavaDoc className;
45
46     /**
47      * Read the class from a data stream. This method takes an InputStream
48      * as input and parses the class from the stream. <p>
49      *
50      *
51      *
52      * @param stream an InputStream from which the class will be read
53      * @exception IOException if there is a problem reading from the given
54      * stream.
55      * @exception ClassFormatError if the class cannot be parsed correctly
56      */

57     public void read(InputStream JavaDoc stream) throws IOException JavaDoc, ClassFormatError JavaDoc {
58         DataInputStream JavaDoc classStream = new DataInputStream JavaDoc(stream);
59
60         if (classStream.readInt() != CLASS_MAGIC) {
61             throw new ClassFormatError JavaDoc("No Magic Code Found "
62                 + "- probably not a Java class file.");
63         }
64
65         // right we have a good looking class file.
66
/* int minorVersion = */ classStream.readUnsignedShort();
67         /* int majorVersion = */ classStream.readUnsignedShort();
68
69         // read the constant pool in and resolve it
70
constantPool = new ConstantPool();
71
72         constantPool.read(classStream);
73         constantPool.resolve();
74
75         /* int accessFlags = */ classStream.readUnsignedShort();
76         int thisClassIndex = classStream.readUnsignedShort();
77         /* int superClassIndex = */ classStream.readUnsignedShort();
78         ClassCPInfo classInfo
79             = (ClassCPInfo) constantPool.getEntry(thisClassIndex);
80         className = classInfo.getClassName();
81     }
82
83
84     /**
85      * Get the classes which this class references.
86      *
87      * @return a vector of class names which this class references
88      */

89     public Vector JavaDoc getClassRefs() {
90
91         Vector JavaDoc classRefs = new Vector JavaDoc();
92
93         for (int i = 0; i < constantPool.size(); ++i) {
94             ConstantPoolEntry entry = constantPool.getEntry(i);
95
96             if (entry != null
97                 && entry.getTag() == ConstantPoolEntry.CONSTANT_CLASS) {
98                 ClassCPInfo classEntry = (ClassCPInfo) entry;
99
100                 if (!classEntry.getClassName().equals(className)) {
101                     classRefs.addElement(
102                         ClassFileUtils.convertSlashName(classEntry.getClassName()));
103                 }
104             }
105         }
106
107         return classRefs;
108     }
109
110     /**
111      * Get the class' fully qualified name in dot format.
112      *
113      * @return the class name in dot format (eg. java.lang.Object)
114      */

115     public String JavaDoc getFullClassName() {
116         return ClassFileUtils.convertSlashName(className);
117     }
118 }
119
120
Popular Tags