KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > classfile > ConstantCP


1 /*
2  * Copyright 2000-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.bcel.classfile;
18
19 import java.io.DataInputStream JavaDoc;
20 import java.io.DataOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import org.apache.bcel.Constants;
23
24 /**
25  * Abstract super class for Fieldref and Methodref constants.
26  *
27  * @version $Id: ConstantCP.java 386056 2006-03-15 11:31:56Z tcurdt $
28  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
29  * @see ConstantFieldref
30  * @see ConstantMethodref
31  * @see ConstantInterfaceMethodref
32  */

33 public abstract class ConstantCP extends Constant {
34
35     /** References to the constants containing the class and the field signature
36      */

37     protected int class_index, name_and_type_index;
38
39
40     /**
41      * Initialize from another object.
42      */

43     public ConstantCP(ConstantCP c) {
44         this(c.getTag(), c.getClassIndex(), c.getNameAndTypeIndex());
45     }
46
47
48     /**
49      * Initialize instance from file data.
50      *
51      * @param tag Constant type tag
52      * @param file Input stream
53      * @throws IOException
54      */

55     ConstantCP(byte tag, DataInputStream JavaDoc file) throws IOException JavaDoc {
56         this(tag, file.readUnsignedShort(), file.readUnsignedShort());
57     }
58
59
60     /**
61      * @param class_index Reference to the class containing the field
62      * @param name_and_type_index and the field signature
63      */

64     protected ConstantCP(byte tag, int class_index, int name_and_type_index) {
65         super(tag);
66         this.class_index = class_index;
67         this.name_and_type_index = name_and_type_index;
68     }
69
70
71     /**
72      * Dump constant field reference to file stream in binary format.
73      *
74      * @param file Output file stream
75      * @throws IOException
76      */

77     public final void dump( DataOutputStream JavaDoc file ) throws IOException JavaDoc {
78         file.writeByte(tag);
79         file.writeShort(class_index);
80         file.writeShort(name_and_type_index);
81     }
82
83
84     /**
85      * @return Reference (index) to class this field or method belongs to.
86      */

87     public final int getClassIndex() {
88         return class_index;
89     }
90
91
92     /**
93      * @return Reference (index) to signature of the field.
94      */

95     public final int getNameAndTypeIndex() {
96         return name_and_type_index;
97     }
98
99
100     /**
101      * @param class_index points to Constant_class
102      */

103     public final void setClassIndex( int class_index ) {
104         this.class_index = class_index;
105     }
106
107
108     /**
109      * @return Class this field belongs to.
110      */

111     public String JavaDoc getClass( ConstantPool cp ) {
112         return cp.constantToString(class_index, Constants.CONSTANT_Class);
113     }
114
115
116     /**
117      * @param name_and_type_index points to Constant_NameAndType
118      */

119     public final void setNameAndTypeIndex( int name_and_type_index ) {
120         this.name_and_type_index = name_and_type_index;
121     }
122
123
124     /**
125      * @return String representation.
126      */

127     public final String JavaDoc toString() {
128         return super.toString() + "(class_index = " + class_index + ", name_and_type_index = "
129                 + name_and_type_index + ")";
130     }
131 }
132
Popular Tags