001: /* ===========================================================================
002: * $RCSfile: InnerClassesInfo.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 an Inner Classes table entry.
027: *
028: * @author Mark Welsh
029: */
030: public class InnerClassesInfo {
031: // Constants -------------------------------------------------------------
032:
033: // Fields ----------------------------------------------------------------
034: private int u2innerClassInfoIndex;
035: private int u2outerClassInfoIndex;
036: private int u2innerNameIndex;
037: private int u2innerClassAccessFlags;
038:
039: // Class Methods ---------------------------------------------------------
040: public static InnerClassesInfo create(DataInput din)
041: throws Exception {
042: InnerClassesInfo ici = new InnerClassesInfo();
043: ici.read(din);
044: return ici;
045: }
046:
047: // Instance Methods ------------------------------------------------------
048: private InnerClassesInfo() {
049: }
050:
051: /** Return the inner class index. */
052: protected int getInnerClassIndex() {
053: return u2innerClassInfoIndex;
054: }
055:
056: /** Return the name index. */
057: protected int getInnerNameIndex() {
058: return u2innerNameIndex;
059: }
060:
061: /** Set the name index. */
062: protected void setInnerNameIndex(int index) {
063: u2innerNameIndex = index;
064: }
065:
066: /** Check for Utf8 references to constant pool and mark them. */
067: protected void markUtf8Refs(ConstantPool pool) throws Exception {
068: // BUGFIX: a Swing1.1beta3 class has name index of zero - this is valid
069: if (u2innerNameIndex != 0) {
070: pool.incRefCount(u2innerNameIndex);
071: }
072: }
073:
074: private void read(DataInput din) throws Exception {
075: u2innerClassInfoIndex = din.readUnsignedShort();
076: u2outerClassInfoIndex = din.readUnsignedShort();
077: u2innerNameIndex = din.readUnsignedShort();
078: u2innerClassAccessFlags = din.readUnsignedShort();
079: }
080:
081: /** Export the representation to a DataOutput stream. */
082: public void write(DataOutput dout) throws Exception {
083: dout.writeShort(u2innerClassInfoIndex);
084: dout.writeShort(u2outerClassInfoIndex);
085: dout.writeShort(u2innerNameIndex);
086: dout.writeShort(u2innerClassAccessFlags);
087: }
088:
089: /** Do necessary name remapping. */
090: protected void remap(ClassFile cf, NameMapper nm) throws Exception {
091: if (u2innerNameIndex != 0) {
092: // Get the full inner class name
093: ClassCpInfo innerClassInfo = (ClassCpInfo) cf
094: .getCpEntry(u2innerClassInfoIndex);
095: String innerClassName = ((Utf8CpInfo) cf
096: .getCpEntry(innerClassInfo.getNameIndex()))
097: .getString();
098: // It is the remapped simple name that must be stored, so truncate
099: String remapClass = nm.mapClass(innerClassName);
100: remapClass = remapClass.substring(remapClass
101: .lastIndexOf('$') + 1);
102: u2innerNameIndex = cf.remapUtf8To(remapClass,
103: u2innerNameIndex);
104: }
105: }
106: }
|