1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2009 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 */
21package proguard.optimize;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.visitor.AttributeVisitor;
25import proguard.classfile.editor.MethodInvocationFixer;
26import proguard.classfile.util.SimplifiedVisitor;
27import proguard.classfile.visitor.MemberVisitor;
28import proguard.optimize.info.ParameterUsageMarker;
29import 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 */
40public class MethodStaticizer
41extends      SimplifiedVisitor
42implements   MemberVisitor,
43             AttributeVisitor
44{
45    private final MemberVisitor extraStaticMemberVisitor;
46
47
48    /**
49     * Creates a new MethodStaticizer.
50     */
51    public MethodStaticizer()
52    {
53        this(null);
54    }
55
56
57    /**
58     * Creates a new MethodStaticizer with an extra visitor.
59     * @param extraStaticMemberVisitor an optional extra visitor for all
60     *                                 methods that have been made static.
61     */
62    public MethodStaticizer(MemberVisitor extraStaticMemberVisitor)
63    {
64        this.extraStaticMemberVisitor = extraStaticMemberVisitor;
65    }
66
67
68    // Implementations for MemberVisitor.
69
70    public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
71    {
72        // Is the 'this' parameter being used?
73        if (!ParameterUsageMarker.isParameterUsed(programMethod, 0))
74        {
75            // Make the method static.
76            programMethod.u2accessFlags =
77                (programMethod.getAccessFlags() & ~ClassConstants.INTERNAL_ACC_FINAL) |
78                ClassConstants.INTERNAL_ACC_STATIC;
79
80            // Visit the method, if required.
81            if (extraStaticMemberVisitor != null)
82            {
83                extraStaticMemberVisitor.visitProgramMethod(programClass, programMethod);
84            }
85        }
86    }
87}
88