KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > CpNameAndType


1 /*
2  * JBET - Java Binary Enhancement Tool
3  * Copyright (c) 2003 Networks Associates Technology, Inc.
4  *
5  * This software was developed under DARPA/SPAWAR contract
6  * N66001-00-C-8602 "SPMA" as part of the
7  * DARPA OASIS research program.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */

30
31 /**
32  * Represent a name_and_type record in the constant pool. The
33  * name_and_type record represents a member (data or function) name
34  * (i.e., foo), and it's type, as specified by the descriptor grammar
35  * presenented in Chapter 4 of the Java Virtual Machine Specification.
36  *
37  * $Id: CpNameAndType.java,v 1.5 2003/09/09 17:31:53 areisse Exp $
38  *
39  * @author Larry D'Anna
40  * @author Lee Badger
41  * @version 0.1
42  * @since JDK 1.1.8
43  */

44
45 package jbet;
46 import java.io.*;
47
48 class CpNameAndType extends CpEntry {
49     int nameIndex = -1; // JVM spec 4.4.6 name_index
50
int typeIndex = -1; // "" descriptor_index
51
// tag is found in CpEntry
52

53     Object JavaDoc _type; // the associated type
54
String JavaDoc n; // "" name
55

56
57     // Constructors
58

59
60     /**
61      *
62      * @see ConstantPool.ConstantPool (DataInputStream dataIn) ...
63      */

64     CpNameAndType (int i, CPInterface cp) {
65     super(i, CPInterface.CONSTANT_NameAndType, cp);
66     }
67
68     /**
69      *
70      * @see ConstantPool.cpNameAndTypeAt (int index) ...
71      */

72     CpNameAndType (CPInterface cp, String JavaDoc name, Object JavaDoc type) {
73     super(cp.poolCount(), CPInterface.CONSTANT_NameAndType, cp);
74     n = name;
75     _type = type;
76     }
77
78     public String JavaDoc toString() {
79     return n + " " + _type;
80     }
81
82     boolean isMethod() { return _type instanceof Descriptor; }
83     boolean isField() { return _type instanceof Type; }
84
85
86     String JavaDoc name() { return n; }
87     Descriptor descriptor() { return (Descriptor) _type; } ;
88     Type type() { return (Type) _type; };
89
90     void setup() throws ClassFileException {
91     if (n == null || _type == null) {
92         CpUtf8 cp = constantPool.cpUtf8At(nameIndex);
93         n = cp.string;
94         cp = constantPool.cpUtf8At(typeIndex);
95         if (cp.string.startsWith("("))
96         _type = new Descriptor ( cp.string );
97         else
98         _type = new Type ( cp.string );
99     }
100     }
101
102
103     public int hashCode() {
104     return n.hashCode() + _type.hashCode();
105     }
106
107     public boolean equals(Object JavaDoc o) {
108     if (!(o instanceof CpNameAndType)) return false;
109     CpNameAndType cp = (CpNameAndType) o;
110     return cp.n.equals(n) && cp._type.equals(_type);
111     }
112
113
114     void write(DataOutputStream dataOut) throws IOException {
115     dataOut.writeByte(tag);
116     dataOut.writeShort(nameIndex);
117     dataOut.writeShort(typeIndex);
118     }
119
120 }
121
122
Popular Tags