KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > InnerClassInfo


1 /*
2  * JBET - Java Binary Enhancement Tool
3  * Copyright (c) 2003 Networks Associates Technology, Inc.
4  *
5  * This software was developed under DARPA/SPAWAR contract
6  * N66001-00-C-8602 "SPMA" as part of the
7  * DARPA OASIS research program.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */

30
31 package jbet;
32 import java.io.*;
33 import java.util.*;
34
35 /**
36  * A record from the InnerClasses (IC) attribute. Seeing as said attribute
37  * has ABSOLUTELY NO RHYME OR REASON as to what is included in it,
38  * this really only serves to make perfect copies.
39  *
40  * $Id: InnerClassInfo.java,v 1.8 2003/09/09 17:31:54 areisse Exp $
41  *
42  * @author Larry D'Anna
43  * @version 0.1
44  * @since JDK 1.1.8
45  */

46
47 class InnerClassInfo implements Cloneable JavaDoc {
48     boolean good; // used by ClassInfo.resolveConstants to mark
49
// elements of Vector innerClasses
50
int accessFlags; // ACC_PUBLIC, ACC_PRIVATE, etc. JVM spec 4.1
51

52     String JavaDoc inner = null; // name of the IC's inner class
53
String JavaDoc outer = null; // name of the IC's outer class
54
String JavaDoc simpleName = null; // the IC's simple name
55
/*
56      * The corresponding indexes into the constant pool
57      * (readwrite stuff, can be stale)
58      */

59     int innerIndex;
60     int outerIndex;
61     int simpleIndex = 0;
62
63     // Constructors
64

65     /**
66      * This constructor is used by resolveConstants() when it encounters
67      * a new innner class in the constantPool.
68      *
69      * @see ClassInfo.resolveConstants
70      */

71     InnerClassInfo() { }
72
73     /**
74      * This constructor is used by ClassInfo() when processing JBET
75      * assembler text.
76      * @see ClassInfo.ClassInfo(ClassPathElement cpElement, Lexer lexer)
77      *
78      * @param lexer the lexer used to process the assembler code
79      */

80     public InnerClassInfo(Lexer lexer) {
81     lexer.push(Lexer.ST_ASM);
82     if (!lexer.match(Token.TAG).text.equals(".inner"))
83         lexer.unexpected(lexer.justread());
84     lexer.state = Lexer.ST_ASM_ARG;
85     inner = lexer.parse_name();
86     outer = lexer.parse_name();
87     simpleName = lexer.parse_name();
88     accessFlags = lexer.parse_flags(ClassInfo.ACC_ALL_INNER_CFLAGS);
89     lexer.term();
90     lexer.pop();
91     }
92
93     public InnerClassInfo(String JavaDoc i, String JavaDoc o, String JavaDoc s, int a) {
94     inner = i;
95     outer = o;
96     simpleName = s;
97     accessFlags = a;
98     }
99
100     public InnerClassInfo (String JavaDoc i, int a) {
101     inner = i;
102     outer = i.substring (0, i.lastIndexOf ('$'));
103     simpleName = i.substring (i.lastIndexOf ('$') + 1);
104     accessFlags = a;
105     }
106
107     /**
108      * This constructor is used by readFile()
109      *
110      * @see ClassInfo.readFile(DataInputStream dataIn, boolean recursive)
111      */

112     public InnerClassInfo (DataInputStream dataIn,
113              ConstantPool constantPool) throws IOException,
114              ClassFileException {
115     innerIndex = dataIn.readUnsignedShort();
116     outerIndex = dataIn.readUnsignedShort();
117     simpleIndex = dataIn.readUnsignedShort();
118
119     if (innerIndex != 0)
120         inner = constantPool.cpClassAt(innerIndex).string();
121
122     if (outerIndex != 0)
123         outer = constantPool.cpClassAt(outerIndex).string();
124
125     if (simpleIndex != 0)
126         simpleName = constantPool.utf8At(simpleIndex);
127
128     accessFlags = dataIn.readUnsignedShort();
129     }
130
131     /**/
132
133     InnerClassInfo dup() {
134     try {
135         return (InnerClassInfo) clone();
136     } catch (Exception JavaDoc e) {
137         return null;
138     }
139     }
140
141
142     /**
143      * Replace the inner and outer class names of this inner class
144      * if necessary. (Called directly by ClassInfo.relocate())
145      *
146      * @param subs A table of string substitution pairs.
147      * @see ClassInfo.relocate
148      */

149     void relocate (Hashtable subs) {
150     inner = Util.relocate(inner, subs);
151     outer = Util.relocate(outer, subs);
152     }
153
154     
155     public void disassemble(LineWriter out, String JavaDoc prefix) {
156     out.println(prefix + ".inner " + inner + " " + outer + " " + simpleName +
157             " " + Util.flags2str(accessFlags, true));
158     }
159
160     void writeFile (DataOutputStream dataOut) throws IOException {
161     dataOut.writeShort ( inner == null ? 0 : innerIndex );
162     dataOut.writeShort ( outer == null ? 0 : outerIndex );
163     dataOut.writeShort ( simpleIndex );
164     dataOut.writeShort ( accessFlags & ClassInfo.ACC_ALL_INNER_CFLAGS);
165     }
166
167     void resolveConstants (ConstantPool constantPool) {
168     if (inner != null)
169         innerIndex = constantPool.internClass(inner);
170     if (outer != null)
171         outerIndex = constantPool.internClass(outer);
172     if (simpleName != null)
173         simpleIndex = constantPool.internUtf8 (simpleName);
174     }
175
176     String JavaDoc recString() {
177     return "inner(" + inner + ") outer(" + outer +
178         ") simple(" + simpleName + ")";
179     }
180
181 }
182     
183     
184
Popular Tags