📄 memberreferencefixer.java
字号:
/* $Id: MemberReferenceFixer.java,v 1.4.2.1 2006/01/16 22:57:55 eric Exp $ * * ProGuard -- shrinking, optimization, and obfuscation of Java class files. * * Copyright (c) 2002-2006 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.classfile.editor;import proguard.classfile.*;import proguard.classfile.attribute.*;import proguard.classfile.attribute.annotation.*;import proguard.classfile.visitor.*;/** * This ClassFileVisitor fixes constant pool field and method references to * fields and methods whose names or descriptors have changed. * * @author Eric Lafortune */public class MemberReferenceFixerimplements ClassFileVisitor, CpInfoVisitor, MemberInfoVisitor, AttrInfoVisitor, AnnotationVisitor, ElementValueVisitor{ private static final boolean DEBUG = false; private ConstantPoolEditor constantPoolEditor = new ConstantPoolEditor(); private StackSizeUpdater stackSizeUpdater; // Parameter for the visitor methods. private int cpIndex; // Return values for the visitor methods. private boolean isInterfaceMethod; private boolean stackSizesMayHaveChanged; /** * Creates a new MemberReferenceFixer. * @param codeLength an estimate of the maximum length of all the code that * will be edited. */ public MemberReferenceFixer(int codeLength) { stackSizeUpdater = new StackSizeUpdater(codeLength); } // Implementations for ClassFileVisitor. public void visitProgramClassFile(ProgramClassFile programClassFile) { stackSizesMayHaveChanged = false; // Fix the constant pool entries. for (int index = 1; index < programClassFile.u2constantPoolCount; index++) { CpInfo cpInfo = programClassFile.constantPool[index]; if (cpInfo != null) { // Fix the entry, replacing it entirely if needed. this.cpIndex = index; cpInfo.accept(programClassFile, this); } } // Fix class members. programClassFile.fieldsAccept(this); programClassFile.methodsAccept(this); // Fix the attributes. programClassFile.attributesAccept(this); } public void visitLibraryClassFile(LibraryClassFile libraryClassFile) { } // 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 visitNameAndTypeCpInfo(ClassFile classFile, NameAndTypeCpInfo nameAndTypeCpInfo) {} public void visitUtf8CpInfo(ClassFile classFile, Utf8CpInfo utf8CpInfo) {} public void visitFieldrefCpInfo(ClassFile classFile, FieldrefCpInfo fieldrefCpInfo) { // Do we know the referenced field? MemberInfo referencedMemberInfo = fieldrefCpInfo.referencedMemberInfo; if (referencedMemberInfo != null) { ClassFile referencedClassFile = fieldrefCpInfo.referencedClassFile; // Does it have a new name or type? String newName = referencedMemberInfo.getName(referencedClassFile); String newType = referencedMemberInfo.getDescriptor(referencedClassFile); if (!fieldrefCpInfo.getName(classFile).equals(newName) || !fieldrefCpInfo.getType(classFile).equals(newType)) { if (DEBUG) { debug(classFile, fieldrefCpInfo, referencedClassFile, referencedMemberInfo); } // Update the name and type index. fieldrefCpInfo.u2nameAndTypeIndex = constantPoolEditor.addNameAndTypeCpInfo((ProgramClassFile)classFile, newName, newType); } } } public void visitInterfaceMethodrefCpInfo(ClassFile classFile, InterfaceMethodrefCpInfo interfaceMethodrefCpInfo) { // Do we know the referenced interface method? MemberInfo referencedMemberInfo = interfaceMethodrefCpInfo.referencedMemberInfo; if (referencedMemberInfo != null) { ClassFile referencedClassFile = interfaceMethodrefCpInfo.referencedClassFile; // Does it have a new name or type? String newName = referencedMemberInfo.getName(referencedClassFile); String newType = referencedMemberInfo.getDescriptor(referencedClassFile); if (!interfaceMethodrefCpInfo.getName(classFile).equals(newName) || !interfaceMethodrefCpInfo.getType(classFile).equals(newType)) { if (DEBUG) { debug(classFile, interfaceMethodrefCpInfo, referencedClassFile, referencedMemberInfo); } // Update the name and type index. interfaceMethodrefCpInfo.u2nameAndTypeIndex = constantPoolEditor.addNameAndTypeCpInfo((ProgramClassFile)classFile, newName, newType); // Remember that the stack sizes of the methods in this class // may have changed. stackSizesMayHaveChanged = true; } // Check if this is an interface method. isInterfaceMethod = true; classFile.constantPoolEntryAccept(interfaceMethodrefCpInfo.u2classIndex, this); // Has the method become a non-interface method? if (!isInterfaceMethod) { if (DEBUG) { System.out.println("MemberReferenceFixer:"); System.out.println(" Class file = "+classFile.getName()); System.out.println(" Ref class file = "+referencedClassFile.getName()); System.out.println(" Ref method = "+interfaceMethodrefCpInfo.getName(classFile)+interfaceMethodrefCpInfo.getType(classFile)); System.out.println(" -> ordinary method"); } // Replace the interface method reference by a method reference. ((ProgramClassFile)classFile).constantPool[this.cpIndex] = new MethodrefCpInfo(interfaceMethodrefCpInfo.u2classIndex, interfaceMethodrefCpInfo.u2nameAndTypeIndex, referencedClassFile, referencedMemberInfo); } } } public void visitMethodrefCpInfo(ClassFile classFile, MethodrefCpInfo methodrefCpInfo) { // Do we know the referenced method? MemberInfo referencedMemberInfo = methodrefCpInfo.referencedMemberInfo; if (referencedMemberInfo != null) { ClassFile referencedClassFile = methodrefCpInfo.referencedClassFile; // Does it have a new name or type? String newName = referencedMemberInfo.getName(referencedClassFile); String newType = referencedMemberInfo.getDescriptor(referencedClassFile); if (!methodrefCpInfo.getName(classFile).equals(newName) || !methodrefCpInfo.getType(classFile).equals(newType)) { if (DEBUG) { debug(classFile, methodrefCpInfo, referencedClassFile, referencedMemberInfo); } // Update the name and type index. methodrefCpInfo.u2nameAndTypeIndex = constantPoolEditor.addNameAndTypeCpInfo((ProgramClassFile)classFile, newName, newType); // Remember that the stack sizes of the methods in this class // may have changed. stackSizesMayHaveChanged = true; } // Check if this is an interface method. isInterfaceMethod = false; classFile.constantPoolEntryAccept(methodrefCpInfo.u2classIndex, this); // Has the method become an interface method? if (isInterfaceMethod)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -