1 | /*
|
---|
2 | * Copyright 2000-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.depend;
|
---|
18 |
|
---|
19 | import java.io.DataInputStream;
|
---|
20 | import java.io.IOException;
|
---|
21 | import java.io.InputStream;
|
---|
22 | import java.util.Vector;
|
---|
23 | import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ClassCPInfo;
|
---|
24 | import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool;
|
---|
25 | import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPoolEntry;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * A ClassFile object stores information about a Java class. The class may
|
---|
29 | * be read from a DataInputStream.and written to a DataOutputStream. These
|
---|
30 | * are usually streams from a Java class file or a class file component of a
|
---|
31 | * Jar file.
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public class ClassFile {
|
---|
35 |
|
---|
36 | /** The Magic Value that marks the start of a Java class file */
|
---|
37 | private static final int CLASS_MAGIC = 0xCAFEBABE;
|
---|
38 |
|
---|
39 | /** This class' constant pool. */
|
---|
40 | private ConstantPool constantPool;
|
---|
41 |
|
---|
42 | /** The class name for this class. */
|
---|
43 | private String className;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Read the class from a data stream. This method takes an InputStream
|
---|
47 | * as input and parses the class from the stream. <p>
|
---|
48 | *
|
---|
49 | *
|
---|
50 | *
|
---|
51 | * @param stream an InputStream from which the class will be read
|
---|
52 | * @exception IOException if there is a problem reading from the given
|
---|
53 | * stream.
|
---|
54 | * @exception ClassFormatError if the class cannot be parsed correctly
|
---|
55 | */
|
---|
56 | public void read(InputStream stream) throws IOException, ClassFormatError {
|
---|
57 | DataInputStream classStream = new DataInputStream(stream);
|
---|
58 |
|
---|
59 | if (classStream.readInt() != CLASS_MAGIC) {
|
---|
60 | throw new ClassFormatError("No Magic Code Found "
|
---|
61 | + "- probably not a Java class file.");
|
---|
62 | }
|
---|
63 |
|
---|
64 | // right we have a good looking class file.
|
---|
65 | /* int minorVersion = */ classStream.readUnsignedShort();
|
---|
66 | /* int majorVersion = */ classStream.readUnsignedShort();
|
---|
67 |
|
---|
68 | // read the constant pool in and resolve it
|
---|
69 | constantPool = new ConstantPool();
|
---|
70 |
|
---|
71 | constantPool.read(classStream);
|
---|
72 | constantPool.resolve();
|
---|
73 |
|
---|
74 | /* int accessFlags = */ classStream.readUnsignedShort();
|
---|
75 | int thisClassIndex = classStream.readUnsignedShort();
|
---|
76 | /* int superClassIndex = */ classStream.readUnsignedShort();
|
---|
77 | ClassCPInfo classInfo
|
---|
78 | = (ClassCPInfo) constantPool.getEntry(thisClassIndex);
|
---|
79 | className = classInfo.getClassName();
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Get the classes which this class references.
|
---|
85 | *
|
---|
86 | * @return a vector of class names which this class references
|
---|
87 | */
|
---|
88 | public Vector getClassRefs() {
|
---|
89 |
|
---|
90 | Vector classRefs = new Vector();
|
---|
91 |
|
---|
92 | for (int i = 0; i < constantPool.size(); ++i) {
|
---|
93 | ConstantPoolEntry entry = constantPool.getEntry(i);
|
---|
94 |
|
---|
95 | if (entry != null
|
---|
96 | && entry.getTag() == ConstantPoolEntry.CONSTANT_CLASS) {
|
---|
97 | ClassCPInfo classEntry = (ClassCPInfo) entry;
|
---|
98 |
|
---|
99 | if (!classEntry.getClassName().equals(className)) {
|
---|
100 | classRefs.addElement(ClassFileUtils.convertSlashName(classEntry.getClassName()));
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | return classRefs;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Get the class' fully qualified name in dot format.
|
---|
110 | *
|
---|
111 | * @return the class name in dot format (eg. java.lang.Object)
|
---|
112 | */
|
---|
113 | public String getFullClassName() {
|
---|
114 | return ClassFileUtils.convertSlashName(className);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|