001: /* ===========================================================================
002: * $RCSfile: RefCpInfo.java,v $
003: * ===========================================================================
004: *
005: * RetroGuard -- an obfuscation package for Java classfiles.
006: *
007: * Copyright (c) 1998-2006 Mark Welsh (markw@retrologic.com)
008: *
009: * This program can be redistributed and/or modified under the terms of the
010: * Version 2 of the GNU General Public License as published by the Free
011: * Software Foundation.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: */
019:
020: package COM.rl.obf.classfile;
021:
022: import java.io.*;
023: import java.util.*;
024:
025: /**
026: * Representation of a 'ref'-type entry in the ConstantPool.
027: *
028: * @author Mark Welsh
029: */
030: abstract public class RefCpInfo extends CpInfo {
031: // Constants -------------------------------------------------------------
032:
033: // Fields ----------------------------------------------------------------
034: private int u2classIndex;
035: private int u2nameAndTypeIndex;
036:
037: // Class Methods ---------------------------------------------------------
038:
039: // Instance Methods ------------------------------------------------------
040: protected RefCpInfo(int tag) {
041: super (tag);
042: }
043:
044: /** Return the class index. */
045: protected int getClassIndex() {
046: return u2classIndex;
047: }
048:
049: /** Return the name-and-type index. */
050: protected int getNameAndTypeIndex() {
051: return u2nameAndTypeIndex;
052: }
053:
054: /** Set the name-and-type index. */
055: protected void setNameAndTypeIndex(int index) {
056: u2nameAndTypeIndex = index;
057: }
058:
059: /** Return the method's class string name. */
060: public String getClassName(ClassFile cf) throws Exception {
061: return ((ClassCpInfo) cf.getCpEntry(u2classIndex)).getName(cf);
062: }
063:
064: /** Return the method's string name. */
065: public String getName(ClassFile cf) throws Exception {
066: NameAndTypeCpInfo ntCpInfo = (NameAndTypeCpInfo) cf
067: .getCpEntry(u2nameAndTypeIndex);
068: return ((Utf8CpInfo) cf.getCpEntry(ntCpInfo.getNameIndex()))
069: .getString();
070: }
071:
072: /** Return the method's string descriptor. */
073: public String getDescriptor(ClassFile cf) throws Exception {
074: NameAndTypeCpInfo ntCpInfo = (NameAndTypeCpInfo) cf
075: .getCpEntry(u2nameAndTypeIndex);
076: return ((Utf8CpInfo) cf.getCpEntry(ntCpInfo
077: .getDescriptorIndex())).getString();
078: }
079:
080: /** Check for N+T references to constant pool and mark them. */
081: protected void markNTRefs(ConstantPool pool) throws Exception {
082: pool.incRefCount(u2nameAndTypeIndex);
083: }
084:
085: /** Read the 'info' data following the u1tag byte. */
086: protected void readInfo(DataInput din) throws Exception {
087: u2classIndex = din.readUnsignedShort();
088: u2nameAndTypeIndex = din.readUnsignedShort();
089: }
090:
091: /** Write the 'info' data following the u1tag byte. */
092: protected void writeInfo(DataOutput dout) throws Exception {
093: dout.writeShort(u2classIndex);
094: dout.writeShort(u2nameAndTypeIndex);
095: }
096:
097: /** Dump the content of the class file to the specified file (used for debugging). */
098: public void dump(PrintWriter pw, ClassFile cf, int index)
099: throws Exception {
100: pw.println(" Ref "
101: + Integer.toString(index)
102: + ": "
103: + ((Utf8CpInfo) cf.getCpEntry(((ClassCpInfo) cf
104: .getCpEntry(u2classIndex)).getNameIndex()))
105: .getString()
106: + " "
107: + ((Utf8CpInfo) cf
108: .getCpEntry(((NameAndTypeCpInfo) cf
109: .getCpEntry(u2nameAndTypeIndex))
110: .getNameIndex())).getString()
111: + " "
112: + ((Utf8CpInfo) cf.getCpEntry(((NameAndTypeCpInfo) cf
113: .getCpEntry(u2nameAndTypeIndex))
114: .getDescriptorIndex())).getString());
115: }
116: }
|