01: /*
02: * ProGuard -- shrinking, optimization, obfuscation, and preverification
03: * of Java bytecode.
04: *
05: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the Free
09: * 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.MethodInvocationFixer;
26: import proguard.classfile.util.SimplifiedVisitor;
27: import proguard.classfile.visitor.MemberVisitor;
28: import proguard.optimize.info.ParameterUsageMarker;
29: import proguard.optimize.peephole.VariableShrinker;
30:
31: /**
32: * This MemberVisitor makes all methods that it visits static, if their 'this'
33: * parameters are unused.
34: *
35: * @see ParameterUsageMarker
36: * @see MethodInvocationFixer
37: * @see VariableShrinker
38: * @author Eric Lafortune
39: */
40: public class MethodStaticizer extends SimplifiedVisitor implements
41: MemberVisitor, AttributeVisitor {
42: private final MemberVisitor extraStaticMemberVisitor;
43:
44: /**
45: * Creates a new MethodStaticizer.
46: */
47: public MethodStaticizer() {
48: this (null);
49: }
50:
51: /**
52: * Creates a new MethodStaticizer with an extra visitor.
53: * @param extraStaticMemberVisitor an optional extra visitor for all
54: * methods that have been made static.
55: */
56: public MethodStaticizer(MemberVisitor extraStaticMemberVisitor) {
57: this .extraStaticMemberVisitor = extraStaticMemberVisitor;
58: }
59:
60: // Implementations for MemberVisitor.
61:
62: public void visitProgramMethod(ProgramClass programClass,
63: ProgramMethod programMethod) {
64: // Is the 'this' parameter being used?
65: if (!ParameterUsageMarker.isParameterUsed(programMethod, 0)) {
66: // Make the method static.
67: programMethod.u2accessFlags = (programMethod
68: .getAccessFlags() & ~ClassConstants.INTERNAL_ACC_FINAL)
69: | ClassConstants.INTERNAL_ACC_STATIC;
70:
71: // Visit the method, if required.
72: if (extraStaticMemberVisitor != null) {
73: extraStaticMemberVisitor.visitProgramMethod(
74: programClass, programMethod);
75: }
76: }
77: }
78: }
|