01: /* ===========================================================================
02: * $RCSfile: AnnotationInfo.java,v $
03: * ===========================================================================
04: *
05: * RetroGuard -- an obfuscation package for Java classfiles.
06: *
07: * Copyright (c) 1998-2006 Mark Welsh (markw@retrologic.com)
08: *
09: * This program can be redistributed and/or modified under the terms of the
10: * Version 2 of the GNU General Public License as published by the Free
11: * Software Foundation.
12: *
13: * This program is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU General Public License for more details.
17: *
18: */
19:
20: package COM.rl.obf.classfile;
21:
22: import java.io.*;
23: import java.util.*;
24:
25: /**
26: * Representation of an annotation table entry.
27: *
28: * @author Mark Welsh
29: */
30: public class AnnotationInfo {
31: // Constants -------------------------------------------------------------
32:
33: // Fields ----------------------------------------------------------------
34: private int u2typeIndex;
35: private int u2numMemberValuePairs;
36: private MemberValuePairInfo[] memberValuePairTable;
37:
38: // Class Methods ---------------------------------------------------------
39: public static AnnotationInfo create(DataInput din) throws Exception {
40: AnnotationInfo ai = new AnnotationInfo();
41: ai.read(din);
42: return ai;
43: }
44:
45: // Instance Methods ------------------------------------------------------
46: private AnnotationInfo() {
47: }
48:
49: /** Return type index into Constant Pool. */
50: protected int getTypeIndex() {
51: return u2typeIndex;
52: }
53:
54: /** Check for Utf8 references to constant pool and mark them. */
55: protected void markUtf8Refs(ConstantPool pool) throws Exception {
56: pool.incRefCount(u2typeIndex);
57: for (int i = 0; i < memberValuePairTable.length; i++) {
58: memberValuePairTable[i].markUtf8Refs(pool);
59: }
60: }
61:
62: private void read(DataInput din) throws Exception {
63: u2typeIndex = din.readUnsignedShort();
64: u2numMemberValuePairs = din.readUnsignedShort();
65: memberValuePairTable = new MemberValuePairInfo[u2numMemberValuePairs];
66: for (int i = 0; i < u2numMemberValuePairs; i++) {
67: memberValuePairTable[i] = MemberValuePairInfo.create(din);
68: }
69: }
70:
71: /** Export the representation to a DataOutput stream. */
72: public void write(DataOutput dout) throws Exception {
73: dout.writeShort(u2typeIndex);
74: dout.writeShort(u2numMemberValuePairs);
75: for (int i = 0; i < u2numMemberValuePairs; i++) {
76: memberValuePairTable[i].write(dout);
77: }
78: }
79:
80: /** Do necessary name remapping. */
81: protected void remap(ClassFile cf, NameMapper nm) throws Exception {
82: String oldType = cf.getUtf8(u2typeIndex);
83: String newType = nm.mapDescriptor(oldType);
84: u2typeIndex = cf.remapUtf8To(newType, u2typeIndex);
85: for (int i = 0; i < u2numMemberValuePairs; i++) {
86: memberValuePairTable[i].remap(cf, nm);
87: }
88: }
89:
90: /** Provide debugging dump of this object. */
91: public void dump(PrintStream ps, ClassFile cf) throws Exception {
92: ps.println("u2typeIndex : " + u2typeIndex + " "
93: + cf.getUtf8(u2typeIndex));
94: ps.println("u2numMemberValuePairs : " + u2numMemberValuePairs);
95: for (int i = 0; i < u2numMemberValuePairs; i++) {
96: memberValuePairTable[i].dump(ps, cf);
97: }
98: }
99: }
|