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:
034: // Represents a CONSTANT_String. These constants are somewhat
035: // complicated in that we must fake string bodies for them.
036:
037: public class StringConstant extends ConstantObject {
038: // Filled in by Clas.resolveConstants()
039: public UnicodeConstant str;
040:
041: public int strIndex; // unresolved index of UnicodeConstant
042:
043: // used by core-image writer.
044: public int unicodeIndex = -1;
045: public int unicodeOffset = 0;
046: public boolean dataWritten = false;
047: public boolean handleWritten = false;
048:
049: private StringConstant(int t, int i) {
050: tag = t;
051: strIndex = i;
052: nSlots = 1;
053: }
054:
055: public static ConstantObject read(int t, DataInput i)
056: throws IOException {
057: return new StringConstant(t, i.readUnsignedShort());
058: }
059:
060: public void resolve(ConstantObject table[]) {
061: if (resolved)
062: return;
063: str = (UnicodeConstant) table[strIndex];
064: resolved = true;
065: }
066:
067: public void externalize(ConstantPool p) {
068: str = (UnicodeConstant) p.add(str);
069: }
070:
071: public void write(DataOutput o) throws IOException {
072: o.writeByte(tag);
073: if (resolved) {
074: o.writeShort(str.index);
075: } else {
076: throw new DataFormatException("unresolved StringConstant");
077: //o.writeShort( strIndex );
078: }
079: }
080:
081: public String toString() {
082: return "String: "
083: + ((resolved) ? str.string : ("[ " + strIndex + " ]"));
084: }
085:
086: public void incReference() {
087: references++;
088: str.incReference();
089: }
090:
091: public void decReference() {
092: references--;
093: str.decReference();
094: }
095:
096: public int hashCode() {
097: return tag + str.string.hashCode();
098: }
099:
100: public boolean equals(Object o) {
101: if (o instanceof StringConstant) {
102: StringConstant s = (StringConstant) o;
103: return str.string.equals(s.str.string);
104: } else {
105: return false;
106: }
107: }
108:
109: public boolean isResolved() {
110: return true;
111: }
112: }
|