KickJava   Java API By Example, From Geeks To Geeks.

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


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.Descriptor;
24
25 /**
26  * This class corresponds to the CONSTANT_NameAndType_info structure as defined
27  * in section 4.4.6 of <i>The Java Virtual Machine Specification</i>.
28  *
29  * @author Brian S O'Neill
30  */

31 public class ConstantNameAndTypeInfo extends ConstantInfo {
32     private final ConstantUTFInfo mNameConstant;
33     private final ConstantUTFInfo mDescriptorConstant;
34     
35     private final Descriptor mType;
36     
37     public ConstantNameAndTypeInfo(ConstantUTFInfo nameConstant,
38                                    ConstantUTFInfo descConstant) {
39         super(TAG_NAME_AND_TYPE);
40         mNameConstant = nameConstant;
41         mDescriptorConstant = descConstant;
42         mType = Descriptor.parse(descConstant.getValue());
43     }
44
45     public ConstantNameAndTypeInfo(ConstantPool cp,
46                                    String JavaDoc name,
47                                    Descriptor type) {
48         super(TAG_NAME_AND_TYPE);
49         mNameConstant = cp.addConstantUTF(name);
50         mDescriptorConstant = cp.addConstantUTF(type.getDescriptor());
51         mType = type;
52     }
53     
54     public String JavaDoc getName() {
55         return mNameConstant.getValue();
56     }
57
58     public Descriptor getType() {
59         return mType;
60     }
61
62     public int hashCode() {
63         return mNameConstant.hashCode();
64     }
65     
66     public boolean equals(Object JavaDoc obj) {
67         if (obj instanceof ConstantNameAndTypeInfo) {
68             ConstantNameAndTypeInfo other = (ConstantNameAndTypeInfo)obj;
69             return mNameConstant.equals(other.mNameConstant) &&
70                 mType.getDescriptor().equals(other.mType.getDescriptor());
71         }
72         
73         return false;
74     }
75     
76     public void writeTo(DataOutput JavaDoc dout) throws IOException JavaDoc {
77         super.writeTo(dout);
78         dout.writeShort(mNameConstant.getIndex());
79         dout.writeShort(mDescriptorConstant.getIndex());
80     }
81
82     public String JavaDoc toString() {
83         return "CONSTANT_NameAndType_info: " + getName() + ", " + getType();
84     }
85 }
86
Popular Tags