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.peephole; 22 23 import proguard.classfile.*; 24 import proguard.classfile.util.SimplifiedVisitor; 25 import proguard.classfile.visitor.ClassVisitor; 26 import proguard.optimize.KeepMarker; 27 28 /** 29 * This <code>ClassVisitor</code> makes the program classes that it visits 30 * final, if possible. 31 * 32 * @author Eric Lafortune 33 */ 34 public class ClassFinalizer 35 extends SimplifiedVisitor 36 implements ClassVisitor 37 { 38 private final ClassVisitor extraClassVisitor; 39 40 41 /** 42 * Creates a new ClassFinalizer. 43 */ ClassFinalizer()44 public ClassFinalizer() 45 { 46 this(null); 47 } 48 49 50 /** 51 * Creates a new ClassFinalizer. 52 * @param extraClassVisitor an optional extra visitor for all finalized 53 * classes. 54 */ ClassFinalizer(ClassVisitor extraClassVisitor)55 public ClassFinalizer(ClassVisitor extraClassVisitor) 56 { 57 this.extraClassVisitor = extraClassVisitor; 58 } 59 60 61 // Implementations for ClassVisitor. 62 visitProgramClass(ProgramClass programClass)63 public void visitProgramClass(ProgramClass programClass) 64 { 65 // If the class is not final/interface/abstract, 66 // and it is not being kept, 67 // and it doesn't have any subclasses, 68 // then make it final. 69 if ((programClass.u2accessFlags & (ClassConstants.ACC_FINAL | 70 ClassConstants.ACC_INTERFACE | 71 ClassConstants.ACC_ABSTRACT)) == 0 && 72 !KeepMarker.isKept(programClass) && 73 programClass.subClasses == null) 74 { 75 programClass.u2accessFlags |= ClassConstants.ACC_FINAL; 76 77 // Visit the class, if required. 78 if (extraClassVisitor != null) 79 { 80 extraClassVisitor.visitProgramClass(programClass); 81 } 82 } 83 } 84 } 85