001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package components;
028:
029: import java.io.DataInput;
030: import java.io.DataOutput;
031: import java.io.IOException;
032: import util.DataFormatException;
033: import jcc.Const;
034:
035: // This class represents a CONSTANT_NameAndType
036:
037: public class NameAndTypeConstant extends ConstantObject {
038: // Filled in by Clas.resolveConstants()
039: public UnicodeConstant name;
040: public UnicodeConstant type;
041:
042: // Read in from classfile
043: int nameIndex;
044: int typeIndex;
045:
046: // The unique ID for this name and type (from Std2ID)
047: public int ID = 0;
048:
049: private NameAndTypeConstant(int t, int ni, int ti) {
050: tag = t;
051: nameIndex = ni;
052: typeIndex = ti;
053: nSlots = 1;
054: }
055:
056: public NameAndTypeConstant(UnicodeConstant name,
057: UnicodeConstant type) {
058: tag = Const.CONSTANT_NAMEANDTYPE;
059: this .name = name;
060: this .type = type;
061: nSlots = 1;
062: resolved = true;
063: }
064:
065: public static ConstantObject read(int t, DataInput i)
066: throws IOException {
067: return new NameAndTypeConstant(t, i.readUnsignedShort(), i
068: .readUnsignedShort());
069: }
070:
071: public void resolve(ConstantObject table[]) {
072: if (resolved)
073: return;
074: name = (UnicodeConstant) table[nameIndex];
075: type = (UnicodeConstant) table[typeIndex];
076: resolved = true;
077: }
078:
079: public void externalize(ConstantPool p) {
080: name = (UnicodeConstant) p.add(name);
081: type = (UnicodeConstant) p.add(type);
082: }
083:
084: public void write(DataOutput o) throws IOException {
085: o.writeByte(tag);
086: if (resolved) {
087: o.writeShort(name.index);
088: o.writeShort(type.index);
089: } else {
090: throw new DataFormatException(
091: "unresolved NameAndTypeConstant");
092: //o.writeShort( nameIndex );
093: //o.writeShort( typeIndex );
094: }
095: }
096:
097: public String toString() {
098: if (resolved) {
099: return "NameAndType: " + name.string + " : " + type.string;
100: } else {
101: return "NameAndType[ " + nameIndex + " : " + typeIndex
102: + " ]";
103: }
104: }
105:
106: public void incReference() {
107: references++;
108: name.incReference();
109: type.incReference();
110: }
111:
112: public void decReference() {
113: references--;
114: name.decReference();
115: type.decReference();
116: }
117:
118: public int hashCode() {
119: return tag + name.string.hashCode() + type.string.hashCode();
120: }
121:
122: public boolean equals(Object o) {
123: if (o instanceof NameAndTypeConstant) {
124: NameAndTypeConstant n = (NameAndTypeConstant) o;
125: return name.string.equals(n.name.string)
126: && type.string.equals(n.type.string);
127: } else {
128: return false;
129: }
130: }
131:
132: public boolean isResolved() {
133: return true;
134: }
135: }
|