📄 gettersetterinliner.java
字号:
/* $Id: GetterSetterInliner.java,v 1.14 2004/12/18 20:22:42 eric Exp $ * * ProGuard -- shrinking, optimization, and obfuscation of Java class files. * * Copyright (c) 2002-2004 Eric Lafortune (eric@graphics.cornell.edu) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package proguard.optimize.peephole;import proguard.classfile.*;import proguard.classfile.attribute.*;import proguard.classfile.attribute.annotation.*;import proguard.classfile.util.*;import proguard.classfile.editor.*;import proguard.classfile.instruction.*;import proguard.classfile.visitor.*;/** * This InstructionVisitor inlines simple getter and setter methods. * * @author Eric Lafortune */public class GetterSetterInlinerimplements InstructionVisitor, CpInfoVisitor{ private static final String SETTER_RETURN_TYPE = "V"; private ConstantPoolEditor constantPoolEditor = new ConstantPoolEditor(); private MyGetterSetterChecker getterSetterChecker = new MyGetterSetterChecker(); private MemberFinder memberFinder = new MemberFinder(); private CodeAttrInfoEditor codeAttrInfoEditor; private boolean allowAccessModification; // Return values of the getter/setter checker. private byte getFieldPutFieldOpcode; private int referencedClassIndex; private int referencedFieldIndex; private ClassFile referencedClassFile; private MemberInfo referencedFieldInfo; private ClassFile[] typeReferencedClassFiles; /** * Creates a new GetterSetterInliner. * @param codeAttrInfoEditor a code editor that can be used for * accumulating changes to the code. * @param allowAccessModification indicates whether the access modifiers of * a field can be changed in order to inline * its getter or setter. */ public GetterSetterInliner(CodeAttrInfoEditor codeAttrInfoEditor, boolean allowAccessModification) { this.codeAttrInfoEditor = codeAttrInfoEditor; this.allowAccessModification = allowAccessModification; } // Implementations for InstructionVisitor. public void visitSimpleInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, SimpleInstruction simpleInstruction) {} public void visitVariableInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, VariableInstruction variableInstruction) {} public void visitBranchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, BranchInstruction branchInstruction) {} public void visitTableSwitchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, TableSwitchInstruction tableSwitchInstruction) {} public void visitLookUpSwitchInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, LookUpSwitchInstruction lookUpSwitchInstruction) {} public void visitCpInstruction(ClassFile classFile, MethodInfo methodInfo, CodeAttrInfo codeAttrInfo, int offset, CpInstruction cpInstruction) { byte opcode = cpInstruction.opcode; // Is this instruction a non-static invoke instruction? if (opcode == InstructionConstants.OP_INVOKEVIRTUAL || opcode == InstructionConstants.OP_INVOKESPECIAL) { // Check if it's a getter or setter that can be inlined. getFieldPutFieldOpcode = 0; classFile.constantPoolEntryAccept(cpInstruction.cpIndex, this); // Do we have a getfield or putfield instruction to inline? if (getFieldPutFieldOpcode != 0) { // Reuse or create the field reference in this class. int fieldrefCpInfoIndex = classFile.equals(referencedClassFile) ? referencedFieldIndex : constantPoolEditor.addFieldrefCpInfo((ProgramClassFile)classFile, referencedClassIndex, referencedFieldInfo.getName(referencedClassFile), referencedFieldInfo.getDescriptor(referencedClassFile), referencedClassFile, referencedFieldInfo, typeReferencedClassFiles); // Inline the getfield or putfield instruction. Instruction replacementInstruction = new CpInstruction(getFieldPutFieldOpcode, fieldrefCpInfoIndex).shrink(); codeAttrInfoEditor.replaceInstruction(offset, replacementInstruction); } } } // Implementations for CpInfoVisitor. public void visitIntegerCpInfo(ClassFile classFile, IntegerCpInfo integerCpInfo) {} public void visitLongCpInfo(ClassFile classFile, LongCpInfo longCpInfo) {} public void visitFloatCpInfo(ClassFile classFile, FloatCpInfo floatCpInfo) {} public void visitDoubleCpInfo(ClassFile classFile, DoubleCpInfo doubleCpInfo) {} public void visitStringCpInfo(ClassFile classFile, StringCpInfo stringCpInfo) {} public void visitUtf8CpInfo(ClassFile classFile, Utf8CpInfo utf8CpInfo) {} public void visitFieldrefCpInfo(ClassFile classFile, FieldrefCpInfo fieldrefCpInfo) {} public void visitInterfaceMethodrefCpInfo(ClassFile classFile, InterfaceMethodrefCpInfo interfaceMethodrefCpInfo) {} public void visitClassCpInfo(ClassFile classFile, ClassCpInfo classCpInfo) {} public void visitNameAndTypeCpInfo(ClassFile classFile, NameAndTypeCpInfo nameAndTypeCpInfo) {} public void visitMethodrefCpInfo(ClassFile classFile, MethodrefCpInfo methodrefCpInfo) { String descriptor = methodrefCpInfo.getType(classFile); // A getter or a setter can't have more than one parameter. int parameterCount = ClassUtil.internalMethodParameterCount(descriptor); if (parameterCount > 1) { return; } // A getter must return a value, a setter must return void. String returnType = ClassUtil.internalMethodReturnType(descriptor); if ((parameterCount > 0) ^ returnType.equals(SETTER_RETURN_TYPE)) { return; } // The referenced method must be present and private or final. MemberInfo referencedMethodInfo = methodrefCpInfo.referencedMemberInfo; if (referencedMethodInfo == null || (referencedMethodInfo.getAccessFlags() & (ClassConstants.INTERNAL_ACC_PRIVATE | ClassConstants.INTERNAL_ACC_FINAL)) == 0) { return; } // Check if the method can be inlined. referencedMethodInfo.accept(methodrefCpInfo.referencedClassFile, getterSetterChecker); // Do we have a getfield or putfield instruction to inline? if (getFieldPutFieldOpcode == 0) { return; } // Remember the constant pool index of the referenced class. referencedClassIndex = methodrefCpInfo.u2classIndex; // Doesn't the field allow at least the same access as the getter or // setter? if (AccessUtil.accessLevel(referencedFieldInfo.getAccessFlags()) < AccessUtil.accessLevel(referencedMethodInfo.getAccessFlags())) { // Are we allowed to fix the access? if (allowAccessModification) { // Is the field access private, and is the field shadowed by // a field in a subclass? if (AccessUtil.accessLevel(referencedFieldInfo.getAccessFlags()) == AccessUtil.PRIVATE && memberFinder.isShadowed(referencedClassFile, (FieldInfo)referencedFieldInfo)) { // Cancel the inlining. getFieldPutFieldOpcode = 0; } if (getFieldPutFieldOpcode != 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -