KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > bytecode > Attributes


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.bytecode;
26
27 import java.io.*;
28 import java.lang.reflect.Modifier JavaDoc;
29
30 class Attributes {
31     //static NoAttributes = new Attributes(new Attribute[0]);
32
Attribute[] data;
33     ClassFile classFile;
34
35     static Attributes createFrom(ClassFile classFile, DataInputStream stream) throws IOException {
36         Attributes attributes = new Attributes();
37         attributes.classFile = classFile;
38         attributes.readFrom(stream);
39         return attributes;
40     }
41
42
43     public Attribute findAttribute(Class JavaDoc ofType) {
44         for(int i=0; i<data.length; i++) {
45             if (ofType.isInstance(data[i])) {
46                 return data[i];
47             }
48         }
49         return null;
50     }
51
52     void readFrom(DataInputStream stream) throws IOException {
53         int attributeCount = stream.readUnsignedShort();
54         //System.err.println("constants: "+constantCount);
55
data = new Attribute[attributeCount];
56         for(int index = 0; index<attributeCount; index++) {
57             int nameIndex = stream.readUnsignedShort();
58             //!!! this can be an unsigned int
59
long attributeLength = (256*256)*stream.readUnsignedShort() +
60                                     stream.readUnsignedShort();
61             String JavaDoc name = classFile.pool.getUTF(nameIndex).value;
62
63             Attribute attribute;
64
65             if (name.equals("SourceFile")) {
66                 attribute = new SourceFileAttribute();
67                 attribute.readFrom(stream);
68             } else if (name.equals("Deprecated")) {
69                 attribute = new DeprecatedAttribute();
70                 attribute.readFrom(stream);
71             } else if (name.equals("Exceptions")) {
72                 attribute = new ExceptionsAttribute();
73                 attribute.readFrom(stream);
74             } else if (name.equals("ConstantValue")) {
75                 attribute = new ConstantValueAttribute();
76                 attribute.readFrom(stream);
77             } else if (name.equals("InnerClasses")) {
78                 attribute = new InnerClassesAttribute();
79                 attribute.readFrom(stream);
80             } else {
81                 attribute = new UnknownAttribute(name, attributeLength);
82                 attribute.readFrom(stream);
83             }
84             data[index] = attribute;
85         }
86     }
87
88     public String JavaDoc toString() {
89         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
90         buf.append("Attributes(");
91         for(int i=0; i<data.length; i++) {
92             if (i > 0) buf.append(", ");
93             buf.append(data[i]);
94         }
95         buf.append(")");
96         return buf.toString();
97     }
98
99     abstract class Attribute {
100         abstract void readFrom(DataInputStream stream) throws IOException;
101     }
102
103     class SourceFileAttribute extends Attribute {
104         ConstantPool.UTF filename;
105
106         void readFrom(DataInputStream stream) throws IOException {
107             filename = classFile.pool.getUTF(stream.readUnsignedShort());
108         }
109
110         public String JavaDoc toString() {
111             return "SourceFile("+filename.value+")";
112         }
113     }
114
115     public class ExceptionsAttribute extends Attribute {
116         public ConstantPool.Class[] exceptions;
117
118         void readFrom(DataInputStream stream) throws IOException {
119             int n = stream.readUnsignedShort();
120             exceptions = new ConstantPool.Class[n];
121             for(int i=0; i<n; i++) {
122                 exceptions[i] = classFile.pool.getClass(stream.readUnsignedShort());
123             }
124         }
125
126         public String JavaDoc toString() {
127             return "Exceptions("+exceptions.length+")";
128         }
129     }
130
131     class DeprecatedAttribute extends Attribute {
132         void readFrom(DataInputStream stream) throws IOException {
133             ;
134         }
135
136         public String JavaDoc toString() {
137             return "Deprecated()";
138         }
139     }
140
141     class ConstantValueAttribute extends Attribute {
142         public ConstantPool.Entry value;
143
144         void readFrom(DataInputStream stream) throws IOException {
145             value = classFile.pool.getEntry(stream.readUnsignedShort());
146         }
147         
148         public String JavaDoc toString() {
149             return "ConstantValue(...)";
150         }
151     }
152
153     class InnerClassesAttribute extends Attribute {
154         public ConstantPool.Class[] innerClasses;
155         public ConstantPool.Class[] outerClasses;
156         public ConstantPool.UTF[] innerNames;
157         public int[] innerAccessFlags;
158             
159         void readFrom(DataInputStream stream) throws IOException {
160             int size = stream.readUnsignedShort();
161             innerClasses = new ConstantPool.Class[size];
162             outerClasses = new ConstantPool.Class[size];
163             innerNames = new ConstantPool.UTF[size];
164             innerAccessFlags = new int[size];
165
166             for (int i = 0; i < size; i++) {
167                 innerClasses[i] = classFile.pool.getClass(stream.readUnsignedShort());
168                 outerClasses[i] = classFile.pool.getClass(stream.readUnsignedShort());
169                 innerNames[i] = classFile.pool.getUTF(stream.readUnsignedShort());
170                 innerAccessFlags[i] = stream.readUnsignedShort();
171             }
172         }
173         
174         public String JavaDoc toString() {
175             return "InnerClassesAttribute(...)";
176         }
177         
178         public boolean isPackageLevel(String JavaDoc str) {
179             int len = innerAccessFlags.length;
180             for (int i = 0; i < len; i++) {
181                 ConstantPool.Class inner = innerClasses[i];
182                 if (str.equals(inner.getName())) return false;
183             }
184             return true;
185         }
186
187         public int getModifiers(String JavaDoc str) {
188             int len = innerAccessFlags.length;
189             for (int i = 0; i < len; i++) {
190                 ConstantPool.Class inner = innerClasses[i];
191                 if (str.equals(inner.getName())) return innerAccessFlags[i];
192             }
193             throw new RuntimeException JavaDoc("Unknown non-package-level class " + str);
194         }
195
196         public java.util.Set JavaDoc getContainedTypeNames(String JavaDoc str) {
197             java.util.HashSet JavaDoc set = new java.util.HashSet JavaDoc();
198             int len = innerAccessFlags.length;
199             for (int i = 0; i < len; i++) {
200                 ConstantPool.Class outer = outerClasses[i];
201                 if (outer == null) continue;
202                 ConstantPool.UTF name = innerNames[i];
203                 if (name == null) continue;
204                 if (str.equals(outer.getName())) {
205                     set.add(name.value);
206                 }
207             }
208             return set;
209         }
210
211         /*
212           // Testing code, clean me out when unnecessary
213         public void print() {
214             System.out.println(" " + innerAccessFlags.length + " inner references");
215             for (int i = 0; i < innerAccessFlags.length; i++) {
216             String inner = getClassName(innerClasses[i]);
217             String outer = getClassName(outerClasses[i]);
218             String name = getUTFValue(innerNames[i]);
219             String mods = getMods(innerAccessFlags[i]);
220             System.out.println(" " + mods + name + " (" + inner + " in " + outer + ")");
221             }
222         }
223         private String getClassName(ConstantPool.Class x) {
224             if (x == null) return "NOWHERE";
225             else return x.getName();
226         }
227         private String getUTFValue(ConstantPool.UTF x) {
228             if (x == null) return "ANONYMOUS";
229             else return x.value;
230         }
231         private String getMods(int x) {
232             if (x == 0) return "";
233             return new org.aspectj.compiler.base.ast.Modifiers(null, x).toString() + " ";
234         }
235         */

236     }
237
238     class UnknownAttribute extends Attribute {
239         String JavaDoc name;
240         long size;
241         public UnknownAttribute(String JavaDoc name, long size) {
242             this.name = name;
243             this.size = size;
244         }
245         void readFrom(DataInputStream stream) throws IOException {
246             stream.skip(size);
247         }
248
249         public String JavaDoc toString() {
250             return "UnknownAttribute("+name+", "+size+")";
251         }
252     }
253 }
254
Popular Tags