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: // ClassConstant represents a CONSTANT_Class stored in a constant pool.
036:
037: public class ClassConstant extends ConstantObject {
038: public UnicodeConstant name;
039:
040: private boolean didLookup;
041: private ClassInfo theClass;
042:
043: public int nameIndex;
044:
045: ClassConstant(int t, int n) {
046: tag = t;
047: nameIndex = n;
048: nSlots = 1;
049: }
050:
051: public ClassConstant(UnicodeConstant n) {
052: tag = Const.CONSTANT_CLASS;
053: name = n;
054: nSlots = 1;
055: resolved = true;
056: }
057:
058: // Read from constant pool.
059: public static ClassConstant read(int tag, DataInput in)
060: throws IOException {
061: return new ClassConstant(tag, in.readUnsignedShort());
062: }
063:
064: public void resolve(ConstantObject table[]) {
065: if (resolved)
066: return;
067: name = (UnicodeConstant) table[nameIndex];
068: resolved = true;
069: }
070:
071: public void externalize(ConstantPool p) {
072: name = (UnicodeConstant) p.add(name);
073: }
074:
075: // Write out reference to the ClassClass data structure
076: public void write(DataOutput o) throws IOException {
077: o.writeByte(tag);
078: if (!resolved)
079: throw new DataFormatException("unresolved ClassConstant");
080: int x = name.index;
081: if (x == 0)
082: throw new DataFormatException("0 name index for "
083: + name.string);
084: o.writeShort(x);
085: }
086:
087: public String toString() {
088: if (resolved)
089: return "Class: " + name.toString();
090: else
091: return "Class: [" + nameIndex + "]";
092: }
093:
094: public int hashCode() {
095: return tag + name.hashCode();
096: }
097:
098: public void incReference() {
099: references++;
100: name.incReference();
101: }
102:
103: public void decReference() {
104: references--;
105: name.decReference();
106: }
107:
108: public boolean equals(Object o) {
109: if (o instanceof ClassConstant) {
110: ClassConstant c = (ClassConstant) o;
111: return name.string.equals(c.name.string);
112: } else {
113: return false;
114: }
115: }
116:
117: public ClassInfo find() {
118: if (!didLookup) {
119: theClass = ClassInfo.lookupClass(name.string);
120: didLookup = true;
121: }
122: return theClass; // which may be null
123: }
124:
125: public void forget() {
126: didLookup = false;
127: theClass = null;
128: }
129:
130: public boolean isResolved() {
131: return find() != null;
132: }
133: }
|