1 package org.apache.bcel.classfile;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
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 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache BCEL" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache BCEL", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import org.apache.bcel.Constants;
58 import java.io.*;
59
60 /***
61 * This class represents a inner class attribute, i.e., the class
62 * indices of the inner and outer classes, the name and the attributes
63 * of the inner class.
64 *
65 * @version $Id: InnerClass.java,v 1.2 2002/03/11 16:16:35 mdahm Exp $
66 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
67 * @see InnerClasses
68 */
69 public final class InnerClass implements Cloneable, Node {
70 private int inner_class_index;
71 private int outer_class_index;
72 private int inner_name_index;
73 private int inner_access_flags;
74
75 /***
76 * Initialize from another object.
77 */
78 public InnerClass(InnerClass c) {
79 this(c.getInnerClassIndex(), c.getOuterClassIndex(), c.getInnerNameIndex(),
80 c.getInnerAccessFlags());
81 }
82
83 /***
84 * Construct object from file stream.
85 * @param file Input stream
86 * @throws IOException
87 */
88 InnerClass(DataInputStream file) throws IOException
89 {
90 this(file.readUnsignedShort(), file.readUnsignedShort(),
91 file.readUnsignedShort(), file.readUnsignedShort());
92 }
93
94 /***
95 * @param inner_class_index Class index in constant pool of inner class
96 * @param outer_class_index Class index in constant pool of outer class
97 * @param inner_name_index Name index in constant pool of inner class
98 * @param inner_access_flags Access flags of inner class
99 */
100 public InnerClass(int inner_class_index, int outer_class_index,
101 int inner_name_index, int inner_access_flags)
102 {
103 this.inner_class_index = inner_class_index;
104 this.outer_class_index = outer_class_index;
105 this.inner_name_index = inner_name_index;
106 this.inner_access_flags = inner_access_flags;
107 }
108
109 /***
110 * Called by objects that are traversing the nodes of the tree implicitely
111 * defined by the contents of a Java class. I.e., the hierarchy of methods,
112 * fields, attributes, etc. spawns a tree of objects.
113 *
114 * @param v Visitor object
115 */
116 public void accept(Visitor v) {
117 v.visitInnerClass(this);
118 }
119 /***
120 * Dump inner class attribute to file stream in binary format.
121 *
122 * @param file Output file stream
123 * @throws IOException
124 */
125 public final void dump(DataOutputStream file) throws IOException
126 {
127 file.writeShort(inner_class_index);
128 file.writeShort(outer_class_index);
129 file.writeShort(inner_name_index);
130 file.writeShort(inner_access_flags);
131 }
132 /***
133 * @return access flags of inner class.
134 */
135 public final int getInnerAccessFlags() { return inner_access_flags; }
136 /***
137 * @return class index of inner class.
138 */
139 public final int getInnerClassIndex() { return inner_class_index; }
140 /***
141 * @return name index of inner class.
142 */
143 public final int getInnerNameIndex() { return inner_name_index; }
144 /***
145 * @return class index of outer class.
146 */
147 public final int getOuterClassIndex() { return outer_class_index; }
148 /***
149 * @param inner_access_flags.
150 */
151 public final void setInnerAccessFlags(int inner_access_flags) {
152 this.inner_access_flags = inner_access_flags;
153 }
154 /***
155 * @param inner_class_index.
156 */
157 public final void setInnerClassIndex(int inner_class_index) {
158 this.inner_class_index = inner_class_index;
159 }
160 /***
161 * @param inner_name_index.
162 */
163 public final void setInnerNameIndex(int inner_name_index) {
164 this.inner_name_index = inner_name_index;
165 }
166 /***
167 * @param outer_class_index.
168 */
169 public final void setOuterClassIndex(int outer_class_index) {
170 this.outer_class_index = outer_class_index;
171 }
172 /***
173 * @return String representation.
174 */
175 public final String toString() {
176 return "InnerClass(" + inner_class_index + ", " + outer_class_index +
177 ", " + inner_name_index + ", " + inner_access_flags + ")";
178 }
179
180 /***
181 * @return Resolved string representation
182 */
183 public final String toString(ConstantPool constant_pool) {
184 String inner_class_name, outer_class_name, inner_name, access;
185
186 inner_class_name = constant_pool.getConstantString(inner_class_index,
187 Constants.CONSTANT_Class);
188 inner_class_name = Utility.compactClassName(inner_class_name);
189
190 if (outer_class_index != 0) {
191 outer_class_name = constant_pool.getConstantString(outer_class_index,
192 Constants.CONSTANT_Class);
193 outer_class_name = Utility.compactClassName(outer_class_name);
194 }
195 else
196 outer_class_name = "<not a member>";
197
198 if(inner_name_index != 0)
199 inner_name = ((ConstantUtf8)constant_pool.
200 getConstant(inner_name_index, Constants.CONSTANT_Utf8)).getBytes();
201 else
202 inner_name = "<anonymous>";
203
204 access = Utility.accessToString(inner_access_flags, true);
205 access = access.equals("")? "" : (access + " ");
206
207 return "InnerClass:" + access + inner_class_name +
208 "(\"" + outer_class_name + "\", \"" + inner_name + "\")";
209 }
210
211 /***
212 * @return deep copy of this object
213 */
214 public InnerClass copy() {
215 try {
216 return (InnerClass)clone();
217 } catch(CloneNotSupportedException e) {}
218
219 return null;
220 }
221 }
This page was automatically generated by Maven