/Users/lyon/j4p/src/javassist/bytecode/InnerClassesAttribute.java
|
1 /*
2 * Javassist, a Java-bytecode translator toolkit.
3 * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. Alternatively, the contents of this file may be used under
8 * the terms of the GNU Lesser General Public License Version 2.1 or later.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 */
15
16 package javassist.bytecode;
17
18 import java.io.DataInputStream;
19 import java.util.Map;
20 import java.io.IOException;
21
22 /**
23 * <code>InnerClasses_attribute</code>.
24 */
25 public class InnerClassesAttribute extends AttributeInfo {
26 /**
27 * The name of this attribute <code>"InnerClasses"</code>.
28 */
29 public static final String tag = "InnerClasses";
30
31 InnerClassesAttribute(ConstPool cp, int n, DataInputStream in)
32 throws IOException {
33 super(cp, n, in);
34 }
35
36 private InnerClassesAttribute(ConstPool cp, byte[] info) {
37 super(cp, tag, info);
38 }
39
40 /**
41 * Returns <code>number_of_classes</code>.
42 */
43 public int length() {
44 return ByteArray.readU16bit(get(), 0);
45 }
46
47 /**
48 * Returns <code>classes[nth].inner_class_info_index</code>.
49 */
50 public int innerClass(int nth) {
51 return ByteArray.readU16bit(get(), nth * 8 + 2);
52 }
53
54 /**
55 * Returns <code>classes[nth].outer_class_info_index</code>.
56 */
57 public int outerClass(int nth) {
58 return ByteArray.readU16bit(get(), nth * 8 + 4);
59 }
60
61 /**
62 * Returns <code>classes[nth].inner_name_index</code>.
63 */
64 public int innerName(int nth) {
65 return ByteArray.readU16bit(get(), nth * 8 + 6);
66 }
67
68 /**
69 * Returns <code>classes[nth].inner_class_access_flags</code>.
70 */
71 public int accessFlags(int nth) {
72 return ByteArray.readU16bit(get(), nth * 8 + 8);
73 }
74
75 /**
76 * Makes a copy. Class names are replaced according to the
77 * given <code>Map</code> object.
78 *
79 * @param newCp the constant pool table used by the new copy.
80 * @param classnames pairs of replaced and substituted
81 * class names.
82 */
83 public AttributeInfo copy(ConstPool newCp, Map classnames) {
84 byte[] src = get();
85 byte[] dest = new byte[src.length];
86 ConstPool cp = getConstPool();
87 InnerClassesAttribute attr = new InnerClassesAttribute(newCp, dest);
88 int n = ByteArray.readU16bit(src, 0);
89 ByteArray.write16bit(n, dest, 0);
90 int j = 2;
91 for (int i = 0; i < n; ++i) {
92 int innerClass = ByteArray.readU16bit(src, j);
93 int outerClass = ByteArray.readU16bit(src, j + 2);
94 int innerName = ByteArray.readU16bit(src, j + 4);
95 int innerAccess = ByteArray.readU16bit(src, j + 6);
96
97 if (innerClass != 0)
98 innerClass = cp.copy(innerClass, newCp, classnames);
99
100 ByteArray.write16bit(innerClass, dest, j);
101
102 if (outerClass != 0)
103 outerClass = cp.copy(outerClass, newCp, classnames);
104
105 ByteArray.write16bit(outerClass, dest, j + 2);
106
107 if (innerName != 0)
108 innerName = cp.copy(innerName, newCp, classnames);
109
110 ByteArray.write16bit(innerName, dest, j + 4);
111 ByteArray.write16bit(innerAccess, dest, j + 6);
112 j += 8;
113 }
114
115 return attr;
116 }
117 }
118