KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.*;
28 import proguard.classfile.*;
29 import proguard.classfile.attribute.CodeAttribute;
30 import proguard.optimize.info.MethodOptimizationInfo;
31
32 /**
33  * This InstructionVisitor marks all methods that invoke super methods (other
34  * than initializers) from the instructions that it visits.
35  *
36  * @author Eric Lafortune
37  */

38 public class SuperInvocationMarker
39 extends SimplifiedVisitor
40 implements InstructionVisitor,
41              ConstantVisitor
42 {
43     private boolean invokesSuperMethods;
44
45
46     // Implementations for InstructionVisitor.
47

48     public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
49
50
51     public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
52     {
53         if (constantInstruction.opcode == InstructionConstants.OP_INVOKESPECIAL)
54         {
55             invokesSuperMethods = false;
56
57             clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
58
59             if (invokesSuperMethods)
60             {
61                 setInvokesSuperMethods(method);
62             }
63         }
64     }
65
66
67     // Implementations for ConstantVisitor.
68

69     public void visitAnyMethodrefConstant(Clazz clazz, RefConstant refConstant)
70     {
71         invokesSuperMethods =
72             !clazz.equals(refConstant.referencedClass) &&
73             !refConstant.getName(clazz).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT);
74     }
75
76
77     // Small utility methods.
78

79     private static void setInvokesSuperMethods(Method method)
80     {
81         MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
82         if (info != null)
83         {
84             info.setInvokesSuperMethods();
85         }
86     }
87
88
89     public static boolean invokesSuperMethods(Method method)
90     {
91         MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
92         return info == null || info.invokesSuperMethods();
93     }
94 }
95
Popular Tags