KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > jlink > ClassNameReader


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.jlink;
19
20 import java.io.DataInput JavaDoc;
21 import java.io.DataInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 /**
26  * Reads just enough of a class file to determine the class' full name.
27  *
28  * <p>Extremely minimal constant pool implementation, mainly to support extracting
29  * strings from a class file.
30  */

31 class ConstantPool {
32     // CheckStyle:VisibilityModifier OFF - bc
33
static final
34         byte UTF8 = 1, UNUSED = 2, INTEGER = 3, FLOAT = 4, LONG = 5, DOUBLE = 6,
35         CLASS = 7, STRING = 8, FIELDREF = 9, METHODREF = 10,
36         INTERFACEMETHODREF = 11, NAMEANDTYPE = 12;
37
38     byte[] types;
39
40     Object JavaDoc[] values;
41     // CheckStyle:VisibilityModifier ON
42

43     /**
44      * Create a constant pool.
45      * @param data the data input containing the class.
46      * @throws IOException if there is an error.
47      */

48     ConstantPool(DataInput JavaDoc data) throws IOException JavaDoc {
49         super();
50
51         int count = data.readUnsignedShort();
52         types = new byte [ count ];
53         values = new Object JavaDoc [ count ];
54         // read in all constant pool entries.
55
for (int i = 1; i < count; i++) {
56             byte type = data.readByte();
57             types[i] = type;
58             switch (type) {
59             case UTF8 :
60                 values[i] = data.readUTF();
61                 break;
62
63             case UNUSED :
64                 break;
65
66             case INTEGER :
67                 values[i] = new Integer JavaDoc(data.readInt());
68                 break;
69
70             case FLOAT :
71                 values[i] = new Float JavaDoc(data.readFloat());
72                 break;
73
74             case LONG :
75                 values[i] = new Long JavaDoc(data.readLong());
76                 ++i;
77                 break;
78
79             case DOUBLE :
80                 values[i] = new Double JavaDoc(data.readDouble());
81                 ++i;
82                 break;
83
84             case CLASS :
85             case STRING :
86                 values[i] = new Integer JavaDoc(data.readUnsignedShort());
87                 break;
88
89             case FIELDREF :
90             case METHODREF :
91             case INTERFACEMETHODREF :
92             case NAMEANDTYPE :
93                 values[i] = new Integer JavaDoc(data.readInt());
94                 break;
95             default:
96                 // Do nothing
97
}
98         }
99     }
100 }
101
102 /**
103  * Provides a quick and dirty way to determine the true name of a class
104  * given just an InputStream. Reads in just enough to perform this
105  * minimal task only.
106  */

107 public class ClassNameReader extends Object JavaDoc {
108
109     /**
110      * Get the class name of a class in an input stream.
111      *
112      * @param input an <code>InputStream</code> value
113      * @return the name of the class
114      * @exception IOException if an error occurs
115      */

116     public static String JavaDoc getClassName(InputStream JavaDoc input) throws IOException JavaDoc {
117         DataInputStream JavaDoc data = new DataInputStream JavaDoc(input);
118         // verify this is a valid class file.
119
int cookie = data.readInt();
120         if (cookie != 0xCAFEBABE) {
121             return null;
122         }
123         /* int version = */ data.readInt();
124         // read the constant pool.
125
ConstantPool constants = new ConstantPool(data);
126         Object JavaDoc[] values = constants.values;
127         // read access flags and class index.
128
/* int accessFlags = */ data.readUnsignedShort();
129         int classIndex = data.readUnsignedShort();
130         Integer JavaDoc stringIndex = (Integer JavaDoc) values[classIndex];
131         String JavaDoc className = (String JavaDoc) values[stringIndex.intValue()];
132         return className;
133     }
134
135
136 }
137
138
139
Popular Tags