KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > Code_attribute


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.classreader;
34
35 import java.io.*;
36 import java.util.*;
37
38 import org.apache.log4j.*;
39
40 public class Code_attribute extends Attribute_info {
41     private int maxStack;
42     private int maxLocals;
43     private byte[] code;
44     private Collection exceptionHandlers = new LinkedList();
45     private Collection attributes = new LinkedList();
46
47     public Code_attribute(Classfile classfile, Visitable owner, DataInputStream in) throws IOException {
48         super(classfile, owner);
49
50         int byteCount = in.readInt();
51         Logger.getLogger(getClass()).debug("Attribute length: " + byteCount);
52
53         maxStack = in.readUnsignedShort();
54         Logger.getLogger(getClass()).debug("Code max stack: " + maxStack);
55
56         maxLocals = in.readUnsignedShort();
57         Logger.getLogger(getClass()).debug("Code max locals: " + maxLocals);
58
59         int codeLength = in.readInt();
60         Logger.getLogger(getClass()).debug("Code length: " + codeLength);
61         code = new byte[codeLength];
62         int bytesRead = in.read(code);
63         Logger.getLogger(getClass()).debug("Bytes read: " + bytesRead);
64
65         Iterator ci = iterator();
66         while (ci.hasNext()) {
67             Instruction instr = (Instruction) ci.next();
68             int start = instr.getStart();
69             
70             switch (instr.getOpcode()) {
71                 case 0xb2: // getstatic
72
case 0xb3: // putstatic
73
case 0xb4: // getfield
74
case 0xb5: // putfield
75
case 0xb6: // invokevirtual
76
case 0xb7: // invokespecial
77
case 0xb8: // invokestatic
78
case 0xb9: // invokeinterface
79
case 0xbb: // new
80
case 0xbd: // anewarray
81
case 0xc0: // checkcast
82
case 0xc1: // instanceof
83
case 0xc5: // multianewarray
84
int index = ((code[start+1] & 0xff) << 8) | (code[start+2] & 0xff);
85                     Logger.getLogger(getClass()).debug(" " + start + ": " + instr + " " + index + " (" + getClassfile().getConstantPool().get(index) + ")");
86                     break;
87                 default:
88                     Logger.getLogger(getClass()).debug(" " + start + ": " + instr + " (" + instr.getLength() + " byte(s))");
89                     break;
90             }
91         }
92
93         int exceptionTableLength = in.readUnsignedShort();
94         Logger.getLogger(getClass()).debug("Reading " + exceptionTableLength + " exception handler(s) ...");
95         for (int i=0; i<exceptionTableLength; i++) {
96             Logger.getLogger(getClass()).debug("Exception handler " + i + ":");
97             exceptionHandlers.add(new ExceptionHandler(this, in));
98         }
99
100         int attributeCount = in.readUnsignedShort();
101         Logger.getLogger(getClass()).debug("Reading " + attributeCount + " code attribute(s)");
102         for (int i=0; i<attributeCount; i++) {
103             Logger.getLogger(getClass()).debug("code attribute " + i + ":");
104             attributes.add(AttributeFactory.create(getClassfile(), this, in));
105         }
106     }
107
108     public int getMaxStack() {
109         return maxStack;
110     }
111
112     public int getMaxLocals() {
113         return maxLocals;
114     }
115
116     public byte[] getCode() {
117         return code;
118     }
119
120     public Iterator iterator() {
121         return new CodeIterator(code);
122     }
123
124     public Collection getExceptionHandlers() {
125         return exceptionHandlers;
126     }
127
128     public Collection getAttributes() {
129         return attributes;
130     }
131
132     public String JavaDoc toString() {
133         return "Code";
134     }
135
136     public void accept(Visitor visitor) {
137         visitor.visitCode_attribute(this);
138     }
139 }
140
Popular Tags