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 */ 21package proguard.optimize.peephole; 22 23import proguard.classfile.*; 24import proguard.classfile.util.*; 25import proguard.classfile.visitor.MemberVisitor; 26import proguard.optimize.KeepMarker; 27 28/** 29 * This <code>MemberVisitor</code> makes the program methods that it visits 30 * final, if possible. 31 * 32 * @author Eric Lafortune 33 */ 34public class MethodFinalizer 35extends SimplifiedVisitor 36implements MemberVisitor 37{ 38 private final MemberVisitor extraMemberVisitor; 39 40 private final MemberFinder memberFinder = new MemberFinder(); 41 42 43 /** 44 * Creates a new ClassFinalizer. 45 */ 46 public MethodFinalizer() 47 { 48 this(null); 49 } 50 51 52 /** 53 * Creates a new ClassFinalizer. 54 * @param extraMemberVisitor an optional extra visitor for all finalized 55 * methods. 56 */ 57 public MethodFinalizer(MemberVisitor extraMemberVisitor) 58 { 59 this.extraMemberVisitor = extraMemberVisitor; 60 } 61 62 63 // Implementations for MemberVisitor. 64 65 public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) 66 { 67 String name = programMethod.getName(programClass); 68 69 // If the method is not already private/static/final/abstract, 70 // and it is not a constructor, 71 // and its class is final, 72 // or it is not being kept and it is not overridden, 73 // then make it final. 74 if ((programMethod.u2accessFlags & (ClassConstants.ACC_PRIVATE | 75 ClassConstants.ACC_STATIC | 76 ClassConstants.ACC_FINAL | 77 ClassConstants.ACC_ABSTRACT)) == 0 && 78 !name.equals(ClassConstants.METHOD_NAME_INIT) && 79 ((programClass.u2accessFlags & ClassConstants.ACC_FINAL) != 0 || 80 (!KeepMarker.isKept(programMethod) && 81 (programClass.subClasses == null || 82 !memberFinder.isOverriden(programClass, programMethod))))) 83 { 84 programMethod.u2accessFlags |= ClassConstants.ACC_FINAL; 85 86 // Visit the method, if required. 87 if (extraMemberVisitor != null) 88 { 89 extraMemberVisitor.visitProgramMethod(programClass, programMethod); 90 } 91 } 92 } 93}