KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > classfile > ConstantClassInfo


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.classfile;
54
55 import java.io.*;
56
57 /******************************************************************************
58  * This class corresponds to the CONSTANT_Class_info structure as defined in
59  * section 4.4.1 of <i>The Java Virtual Machine Specification</i>.
60  *
61  * @author Brian S O'Neill
62  * @version
63  * <!--$$Revision:--> 26 <!-- $-->, <!--$$JustDate:--> 01/05/14 <!-- $-->
64  */

65 public class ConstantClassInfo extends ConstantInfo {
66     private String JavaDoc mClassName;
67     private int mDim;
68     private TypeDescriptor mType;
69     
70     private ConstantUTFInfo mNameConstant;
71     
72     /**
73      * Will return either a new ConstantClassInfo object or one already in
74      * the constant pool. If it is a new ConstantClassInfo, it will be inserted
75      * into the pool.
76      */

77     static ConstantClassInfo make(ConstantPool cp, String JavaDoc className) {
78         ConstantInfo ci = new ConstantClassInfo(cp, className);
79         return (ConstantClassInfo)cp.addConstant(ci);
80     }
81     
82     /** Used to describe an array class. */
83     static ConstantClassInfo make(ConstantPool cp,
84                                   String JavaDoc className, int dim) {
85         ConstantInfo ci = new ConstantClassInfo(cp, className, dim);
86         return (ConstantClassInfo)cp.addConstant(ci);
87     }
88     
89     static ConstantClassInfo make(ConstantPool cp,
90                                   TypeDescriptor type) {
91         ConstantInfo ci = new ConstantClassInfo(cp, type);
92         return (ConstantClassInfo)cp.addConstant(ci);
93     }
94
95     ConstantClassInfo(ConstantUTFInfo nameConstant) {
96         super(TAG_CLASS);
97         mNameConstant = nameConstant;
98         String JavaDoc name = nameConstant.getValue();
99         if (!name.endsWith(";") && !name.startsWith("[")) {
100             mClassName = name.replace('/', '.');
101             mDim = 0;
102         }
103         else {
104             TypeDescriptor desc = TypeDescriptor.parseTypeDesc(name);
105             mClassName = desc.getClassName();
106             mDim = desc.getDimensions();
107         }
108     }
109
110     private ConstantClassInfo(ConstantPool cp, String JavaDoc className) {
111         super(TAG_CLASS);
112         
113         mClassName = className;
114         mDim = 0;
115         
116         String JavaDoc desc = className.replace('.', '/');
117         mNameConstant = ConstantUTFInfo.make(cp, desc);
118     }
119     
120     /** Used to describe an array class. */
121     private ConstantClassInfo(ConstantPool cp, String JavaDoc className, int dim) {
122         super(TAG_CLASS);
123         
124         mClassName = className;
125         mDim = dim;
126         
127         String JavaDoc desc;
128         if (dim > 0) {
129             desc = TypeDescriptor.generate(className, dim);
130         }
131         else {
132             desc = className.replace('.', '/');
133         }
134
135         mNameConstant = ConstantUTFInfo.make(cp, desc);
136     }
137     
138     private ConstantClassInfo(ConstantPool cp, TypeDescriptor type) {
139         super(TAG_CLASS);
140
141         mClassName = type.getClassName();
142         mDim = type.getDimensions();
143         mType = type;
144
145         String JavaDoc desc;
146         if (mDim > 0) {
147             desc = type.toString();
148         }
149         else {
150             desc = type.getClassName().replace('.', '/');
151         }
152
153         mNameConstant = ConstantUTFInfo.make(cp, desc);
154     }
155
156     public String JavaDoc getClassName() {
157         return mClassName;
158     }
159
160     public int getDimensions() {
161         return mDim;
162     }
163
164     public TypeDescriptor getTypeDescriptor() {
165         if (mType == null) {
166             mType = new TypeDescriptor(getClassName());
167
168             int dim = getDimensions();
169             if (dim > 0) {
170                 mType = new TypeDescriptor(mType, dim);
171             }
172         }
173
174         return mType;
175     }
176
177     public int hashCode() {
178         return mClassName.hashCode();
179     }
180     
181     public boolean equals(Object JavaDoc obj) {
182         if (obj instanceof ConstantClassInfo) {
183             ConstantClassInfo other = (ConstantClassInfo)obj;
184             return mClassName.equals(other.mClassName) && mDim == other.mDim;
185         }
186         
187         return false;
188     }
189     
190     public void writeTo(DataOutput dout) throws IOException {
191         super.writeTo(dout);
192         dout.writeShort(mNameConstant.getIndex());
193     }
194
195     public String JavaDoc toString() {
196         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("CONSTANT_Class_info: ");
197         buf.append(getClassName());
198         for (int i = getDimensions(); i > 0; i--) {
199             buf.append('[');
200             buf.append(']');
201         }
202         return buf.toString();
203     }
204 }
205
Popular Tags