KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > info > BackwardBranchMarker


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 program 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 program 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 General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.optimize.info;
22
23 import proguard.classfile.util.SimplifiedVisitor;
24 import proguard.classfile.instruction.visitor.InstructionVisitor;
25 import proguard.classfile.instruction.*;
26 import proguard.classfile.constant.visitor.ConstantVisitor;
27 import proguard.classfile.constant.RefConstant;
28 import proguard.classfile.*;
29 import proguard.classfile.attribute.CodeAttribute;
30
31 /**
32  * This InstructionVisitor marks all methods that branch backward in any of the
33  * instructions that it visits.
34  *
35  * @author Eric Lafortune
36  */

37 public class BackwardBranchMarker
38 extends SimplifiedVisitor
39 implements InstructionVisitor
40 {
41     // Implementations for InstructionVisitor.
42

43     public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
44
45
46     public void visitBranchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, BranchInstruction branchInstruction)
47     {
48         markBackwardBranch(method, branchInstruction.branchOffset);
49     }
50
51
52     public void visitAnySwitchInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, SwitchInstruction switchInstruction)
53     {
54         markBackwardBranch(method, switchInstruction.defaultOffset);
55
56         for (int index = 0; index < switchInstruction.jumpOffsets.length; index++)
57         {
58             markBackwardBranch(method, switchInstruction.jumpOffsets[index]);
59         }
60     }
61
62
63     // Small utility methods.
64

65     /**
66      * Marks the given method if the given branch offset is negative.
67      */

68     private void markBackwardBranch(Method method, int branchOffset)
69     {
70         if (branchOffset < 0)
71         {
72             setBranchesBackward(method);
73         }
74     }
75
76
77     private static void setBranchesBackward(Method method)
78     {
79         MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
80         if (info != null)
81         {
82             info.setBranchesBackward();
83         }
84     }
85
86
87     public static boolean branchesBackward(Method method)
88     {
89         MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
90         return info == null || info.branchesBackward();
91     }
92 }
93
Popular Tags