KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > classfile > attribute > CodeAttribute


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.classfile.attribute;
22
23 import proguard.classfile.*;
24 import proguard.classfile.attribute.visitor.*;
25 import proguard.classfile.instruction.*;
26 import proguard.classfile.instruction.visitor.InstructionVisitor;
27
28 /**
29  * This Attribute represents a code attribute.
30  *
31  * @author Eric Lafortune
32  */

33 public class CodeAttribute extends Attribute
34 {
35     public int u2maxStack;
36     public int u2maxLocals;
37     public int u4codeLength;
38     public byte[] code;
39     public int u2exceptionTableLength;
40     public ExceptionInfo[] exceptionTable;
41     public int u2attributesCount;
42     public Attribute[] attributes;
43
44
45     /**
46      * Creates an uninitialized CodeAttribute.
47      */

48     public CodeAttribute()
49     {
50     }
51
52
53     /**
54      * Returns the (first) attribute with the given name.
55      */

56     public Attribute getAttribute(Clazz clazz, String JavaDoc name)
57     {
58         for (int index = 0; index < u2attributesCount; index++)
59         {
60             Attribute attribute = attributes[index];
61             if (attribute.getAttributeName(clazz).equals(name))
62             {
63                 return attribute;
64             }
65         }
66
67         return null;
68     }
69
70
71     // Implementations for Attribute.
72

73     public void accept(Clazz clazz, Method method, AttributeVisitor attributeVisitor)
74     {
75         attributeVisitor.visitCodeAttribute(clazz, method, this);
76     }
77
78
79     /**
80      * Applies the given instruction visitor to all instructions.
81      */

82     public void instructionsAccept(Clazz clazz, Method method, InstructionVisitor instructionVisitor)
83     {
84         instructionsAccept(clazz, method, 0, u4codeLength, instructionVisitor);
85     }
86
87
88     /**
89      * Applies the given instruction visitor to the instruction at the specified
90      * offset.
91      */

92     public void instructionAccept(Clazz clazz, Method method, int offset, InstructionVisitor instructionVisitor)
93     {
94         Instruction instruction = InstructionFactory.create(code, offset);
95         instruction.accept(clazz, method, this, offset, instructionVisitor);
96     }
97
98
99     /**
100      * Applies the given instruction visitor to all instructions in the
101      * specified range of offsets.
102      */

103     public void instructionsAccept(Clazz clazz, Method method, int startOffset, int endOffset, InstructionVisitor instructionVisitor)
104     {
105         int offset = startOffset;
106
107         while (offset < endOffset)
108         {
109             // Note that the instruction is only volatile.
110
Instruction instruction = InstructionFactory.create(code, offset);
111             int instructionLength = instruction.length(offset);
112             instruction.accept(clazz, method, this, offset, instructionVisitor);
113             offset += instructionLength;
114         }
115     }
116
117
118     /**
119      * Applies the given exception visitor to all exceptions.
120      */

121     public void exceptionsAccept(Clazz clazz, Method method, ExceptionInfoVisitor exceptionInfoVisitor)
122     {
123         for (int index = 0; index < u2exceptionTableLength; index++)
124         {
125             // We don't need double dispatching here, since there is only one
126
// type of ExceptionInfo.
127
exceptionInfoVisitor.visitExceptionInfo(clazz, method, this, exceptionTable[index]);
128         }
129     }
130
131
132     /**
133      * Applies the given exception visitor to all exceptions that are applicable
134      * to the instruction at the specified offset.
135      */

136     public void exceptionsAccept(Clazz clazz, Method method, int offset, ExceptionInfoVisitor exceptionInfoVisitor)
137     {
138         for (int index = 0; index < u2exceptionTableLength; index++)
139         {
140             ExceptionInfo exceptionInfo = exceptionTable[index];
141             if (exceptionInfo.isApplicable(offset))
142             {
143                 exceptionInfoVisitor.visitExceptionInfo(clazz, method, this, exceptionInfo);
144             }
145         }
146     }
147
148
149     /**
150      * Applies the given exception visitor to all exceptions that are applicable
151      * to any of the instructions in the specified range of offsets.
152      */

153     public void exceptionsAccept(Clazz clazz, Method method, int startOffset, int endOffset, ExceptionInfoVisitor exceptionInfoVisitor)
154     {
155         for (int index = 0; index < u2exceptionTableLength; index++)
156         {
157             ExceptionInfo exceptionInfo = exceptionTable[index];
158             if (exceptionInfo.isApplicable(startOffset, endOffset))
159             {
160                 exceptionInfoVisitor.visitExceptionInfo(clazz, method, this, exceptionInfo);
161             }
162         }
163     }
164
165
166     /**
167      * Applies the given attribute visitor to all attributes.
168      */

169     public void attributesAccept(Clazz clazz, Method method, AttributeVisitor attributeVisitor)
170     {
171         for (int index = 0; index < u2attributesCount; index++)
172         {
173             attributes[index].accept(clazz, method, this, attributeVisitor);
174         }
175     }
176 }
177
Free Books   Free Magazines  
Popular Tags