001: /**
002: * YGuard -- an obfuscation library for Java(TM) classfiles.
003: *
004: * Original Copyright (c) 1999 Mark Welsh (markw@retrologic.com)
005: * Modifications Copyright (c) 2002 yWorks GmbH (yguard@yworks.com)
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * The author may be contacted at yguard@yworks.com
022: *
023: * Java and all Java-based marks are trademarks or registered
024: * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
025: */package com.yworks.yguard.obf.classfile;
026:
027: import java.io.*;
028: import java.util.*;
029:
030: /**
031: * Representation of an entry in the ConstantPool. Specific types of entry
032: * have their representations sub-classed from this.
033: *
034: * @author Mark Welsh
035: */
036: abstract public class CpInfo implements ClassConstants {
037: // Constants -------------------------------------------------------------
038:
039: // Fields ----------------------------------------------------------------
040: private int u1tag;
041: private byte info[];
042:
043: protected int refCount = 0; // Used for reference counting in Constant Pool
044:
045: // Class Methods ---------------------------------------------------------
046: /**
047: * Create a new CpInfo from the data passed.
048: *
049: * @throws IOException if class file is corrupt or incomplete
050: */
051: public static CpInfo create(DataInput din)
052: throws java.io.IOException {
053: if (din == null)
054: throw new NullPointerException(
055: "No input stream was provided.");
056:
057: // Instantiate based on tag byte
058: CpInfo ci = null;
059: switch (din.readUnsignedByte()) {
060: case CONSTANT_Utf8:
061: ci = new Utf8CpInfo();
062: break;
063: case CONSTANT_Integer:
064: ci = new IntegerCpInfo();
065: break;
066: case CONSTANT_Float:
067: ci = new FloatCpInfo();
068: break;
069: case CONSTANT_Long:
070: ci = new LongCpInfo();
071: break;
072: case CONSTANT_Double:
073: ci = new DoubleCpInfo();
074: break;
075: case CONSTANT_Class:
076: ci = new ClassCpInfo();
077: break;
078: case CONSTANT_String:
079: ci = new StringCpInfo();
080: break;
081: case CONSTANT_Fieldref:
082: ci = new FieldrefCpInfo();
083: break;
084: case CONSTANT_Methodref:
085: ci = new MethodrefCpInfo();
086: break;
087: case CONSTANT_InterfaceMethodref:
088: ci = new InterfaceMethodrefCpInfo();
089: break;
090: case CONSTANT_NameAndType:
091: ci = new NameAndTypeCpInfo();
092: break;
093: default:
094: throw new IOException("Unknown tag type in constant pool.");
095: }
096: ci.readInfo(din);
097: return ci;
098: }
099:
100: // Instance Methods ------------------------------------------------------
101: protected CpInfo(int tag) {
102: u1tag = tag;
103: }
104:
105: /** Read the 'info' data following the u1tag byte; over-ride this in sub-classes. */
106: abstract protected void readInfo(DataInput din)
107: throws java.io.IOException;
108:
109: /** Check for Utf8 references to constant pool and mark them; over-ride this in sub-classes. */
110: protected void markUtf8Refs(ConstantPool pool) {
111: }
112:
113: /** Check for NameAndType references to constant pool and mark them; over-ride this in sub-classes. */
114: protected void markNTRefs(ConstantPool pool) {
115: }
116:
117: /** Export the representation to a DataOutput stream. */
118: public void write(DataOutput dout) throws java.io.IOException {
119: if (dout == null)
120: throw new IOException("No output stream was provided.");
121: dout.writeByte(u1tag);
122: writeInfo(dout);
123: }
124:
125: /** Write the 'info' data following the u1tag byte; over-ride this in sub-classes. */
126: abstract protected void writeInfo(DataOutput dout)
127: throws java.io.IOException;
128:
129: /** Return the reference count. */
130: public int getRefCount() {
131: return refCount;
132: }
133:
134: /** Increment the reference count. */
135: public void incRefCount() {
136: refCount++;
137: }
138:
139: /** Decrement the reference count. */
140: public void decRefCount() {
141: if (refCount == 0)
142: throw new IllegalStateException(
143: "Illegal to reference count is already zero.");
144: refCount--;
145: }
146:
147: /** Reset the reference count to zero. */
148: public void resetRefCount() {
149: refCount = 0;
150: }
151:
152: /** Dump the content of the class file to the specified file (used for debugging). */
153: public void dump(PrintWriter pw, ClassFile cf, int index) {
154: pw.println(this.getClass().getName());
155: }
156: }
|