KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cojen > classfile > constant > ConstantClassInfo


1 /*
2  * Copyright 2004 Brian S O'Neill
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.cojen.classfile.constant;
18
19 import java.io.DataOutput JavaDoc;
20 import java.io.IOException JavaDoc;
21 import org.cojen.classfile.ConstantInfo;
22 import org.cojen.classfile.ConstantPool;
23 import org.cojen.classfile.TypeDesc;
24
25 /**
26  * This class corresponds to the CONSTANT_Class_info structure as defined in
27  * section 4.4.1 of <i>The Java Virtual Machine Specification</i>.
28  *
29  * @author Brian S O'Neill
30  */

31 public class ConstantClassInfo extends ConstantInfo {
32     private final TypeDesc mType;
33     private final ConstantUTFInfo mNameConstant;
34     
35     public ConstantClassInfo(ConstantUTFInfo nameConstant) {
36         super(TAG_CLASS);
37         String JavaDoc name = nameConstant.getValue();
38         if (!name.endsWith(";") && !name.startsWith("[")) {
39             mType = TypeDesc.forClass(name);
40         } else {
41             mType = TypeDesc.forDescriptor(name);
42         }
43         mNameConstant = nameConstant;
44     }
45
46     public ConstantClassInfo(ConstantPool cp, String JavaDoc className) {
47         super(TAG_CLASS);
48         String JavaDoc desc = className.replace('.', '/');
49         mType = TypeDesc.forClass(className);
50         mNameConstant = cp.addConstantUTF(desc);
51     }
52     
53     /**
54      * Used to describe an array class.
55      */

56     public ConstantClassInfo(ConstantPool cp, String JavaDoc className, int dim) {
57         super(TAG_CLASS);
58         TypeDesc type = TypeDesc.forClass(className);
59         String JavaDoc desc;
60         if (dim > 0) {
61             while (--dim >= 0) {
62                 type = type.toArrayType();
63             }
64             desc = type.getDescriptor();
65         } else {
66             desc = className.replace('.', '/');
67         }
68         mType = type;
69         mNameConstant = cp.addConstantUTF(desc);
70     }
71     
72     public ConstantClassInfo(ConstantPool cp, TypeDesc type) {
73         super(TAG_CLASS);
74         String JavaDoc desc;
75         if (type.isArray()) {
76             desc = type.getDescriptor();
77         } else {
78             desc = type.getRootName().replace('.', '/');
79         }
80         mType = type;
81         mNameConstant = cp.addConstantUTF(desc);
82     }
83
84     public TypeDesc getType() {
85         return mType;
86     }
87
88     public int hashCode() {
89         return mType.hashCode();
90     }
91     
92     public boolean equals(Object JavaDoc obj) {
93         if (this == obj) {
94             return true;
95         }
96         if (obj instanceof ConstantClassInfo) {
97             ConstantClassInfo other = (ConstantClassInfo)obj;
98             return mType.equals(other.mType);
99         }
100         return false;
101     }
102     
103     public void writeTo(DataOutput JavaDoc dout) throws IOException JavaDoc {
104         super.writeTo(dout);
105         dout.writeShort(mNameConstant.getIndex());
106     }
107
108     public String JavaDoc toString() {
109         return "CONSTANT_Class_info: ".concat(getType().getFullName());
110     }
111 }
112
Popular Tags