1 /* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2014 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.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 41 extends SimplifiedVisitor 42 implements MemberVisitor, 43 AttributeVisitor 44 { 45 private final MemberVisitor extraStaticMemberVisitor; 46 47 48 /** 49 * Creates a new MethodStaticizer. 50 */ MethodStaticizer()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 */ MethodStaticizer(MemberVisitor extraStaticMemberVisitor)62 public MethodStaticizer(MemberVisitor extraStaticMemberVisitor) 63 { 64 this.extraStaticMemberVisitor = extraStaticMemberVisitor; 65 } 66 67 68 // Implementations for MemberVisitor. 69 visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)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.ACC_FINAL) | 78 ClassConstants.ACC_STATIC; 79 80 // Visit the method, if required. 81 if (extraStaticMemberVisitor != null) 82 { 83 extraStaticMemberVisitor.visitProgramMethod(programClass, programMethod); 84 } 85 } 86 } 87 } 88