KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > MethodStaticizer


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;
22
23 import proguard.classfile.*;
24 import proguard.classfile.attribute.visitor.AttributeVisitor;
25 import proguard.classfile.editor.*;
26 import proguard.classfile.util.*;
27 import proguard.classfile.visitor.MemberVisitor;
28 import proguard.optimize.info.ParameterUsageMarker;
29
30 /**
31  * This MemberVisitor makes all methods that it visits static, if their 'this'
32  * parameters are unused.
33  *
34  * @see ParameterUsageMarker
35  * @see MethodInvocationFixer
36  * @see VariableShrinker
37  * @author Eric Lafortune
38  */

39 public class MethodStaticizer
40 extends SimplifiedVisitor
41 implements MemberVisitor,
42              AttributeVisitor
43 {
44     private MemberVisitor extraStaticMemberVisitor;
45
46
47     /**
48      * Creates a new MethodStaticizer.
49      */

50     public MethodStaticizer()
51     {
52         this(null);
53     }
54
55
56     /**
57      * Creates a new MethodStaticizer with an extra visitor.
58      * @param extraStaticMemberVisitor an optional extra visitor for all
59      * methods that have been made static.
60      */

61     public MethodStaticizer(MemberVisitor extraStaticMemberVisitor)
62     {
63         this.extraStaticMemberVisitor = extraStaticMemberVisitor;
64     }
65
66
67     // Implementations for MemberVisitor.
68

69     public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
70     {
71         // Is the 'this' parameter being used?
72
if (!ParameterUsageMarker.isParameterUsed(programMethod, 0))
73         {
74             // Make the method static.
75
programMethod.u2accessFlags =
76                 (programMethod.getAccessFlags() & ~ClassConstants.INTERNAL_ACC_FINAL) |
77                 ClassConstants.INTERNAL_ACC_STATIC;
78
79             // Visit the method, if required.
80
if (extraStaticMemberVisitor != null)
81             {
82                 extraStaticMemberVisitor.visitProgramMethod(programClass, programMethod);
83             }
84         }
85     }
86 }
87
Popular Tags