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.IOException;
031: import util.DataFormatException;
032: import jcc.Const;
033:
034: /*
035: * An abstract class for representing objects in the constant pools
036: * All constants are derived from here.
037: * Note how this and all its derivatives are Cloneable.
038: */
039:
040: public abstract class ConstantObject extends ClassComponent implements
041: Cloneable {
042: public int references = 0;
043: public int ldcReferences = 0;
044: public boolean shared = false;
045: public ConstantPool containingPool; // if shared == true, then container.
046:
047: public int index;
048:
049: // The type of constant pool entry.
050: public int tag;
051:
052: // The number of slots this thing "uses" -- usually 1, sometimes 2
053: public int nSlots;
054:
055: public void incldcReference() {
056: ldcReferences++;
057: }
058:
059: public void decldcReference() {
060: ldcReferences--;
061: }
062:
063: public void clearldcReference() {
064: ldcReferences = 0;
065: }
066:
067: // Some items are reference counted so that the most frequently
068: // accessed ones can be placed in the first 256 entries of the
069: // constant table. Others are just marked as used so we know
070: // to include them somewhere in the table.
071: public void incReference() {
072: references++;
073: }
074:
075: public void decReference() {
076: references--;
077: }
078:
079: public void clearReference() {
080: references = 0;
081: }
082:
083: public Object clone() {
084: ConstantObject newC;
085: try {
086: newC = (ConstantObject) super .clone();
087: newC.references = 0;
088: newC.shared = false;
089: newC.index = 0;
090: return newC;
091: } catch (CloneNotSupportedException e) {
092: e.printStackTrace();
093: System.exit(1);
094: }
095: return null;
096: }
097:
098: public abstract boolean isResolved();
099:
100: static public ConstantObject readObject(DataInput i)
101: throws IOException {
102: // read the tag and dispatch accordingly
103: int tag = i.readUnsignedByte();
104: switch (tag) {
105: case Const.CONSTANT_UTF8:
106: return UnicodeConstant.read(tag, i);
107: case Const.CONSTANT_INTEGER:
108: case Const.CONSTANT_FLOAT:
109: return SingleValueConstant.read(tag, i);
110: case Const.CONSTANT_DOUBLE:
111: case Const.CONSTANT_LONG:
112: return DoubleValueConstant.read(tag, i);
113: case Const.CONSTANT_STRING:
114: return StringConstant.read(tag, i);
115: case Const.CONSTANT_NAMEANDTYPE:
116: return NameAndTypeConstant.read(tag, i);
117: case Const.CONSTANT_CLASS:
118: return ClassConstant.read(tag, i);
119: case Const.CONSTANT_FIELD:
120: return FieldConstant.read(tag, i);
121: case Const.CONSTANT_METHOD:
122: return MethodConstant.read(tag, i);
123: case Const.CONSTANT_INTERFACEMETHOD:
124: return InterfaceConstant.read(tag, i);
125: default:
126: throw new DataFormatException("Format error (constant tag "
127: + tag + " )");
128: }
129: }
130: }
|