📄 memberreferencefixer.java
字号:
/* * ProGuard -- shrinking, optimization, obfuscation, and preverification * of Java bytecode. * * Copyright (c) 2002-2007 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.attribute.annotation.visitor.*;import proguard.classfile.attribute.visitor.AttributeVisitor;import proguard.classfile.constant.*;import proguard.classfile.constant.visitor.ConstantVisitor;import proguard.classfile.util.*;import proguard.classfile.visitor.*;/** * This ClassVisitor fixes constant pool field and method references to * fields and methods whose names or descriptors have changed. * * @author Eric Lafortune */public class MemberReferenceFixerextends SimplifiedVisitorimplements ClassVisitor, ConstantVisitor, MemberVisitor, AttributeVisitor, AnnotationVisitor, ElementValueVisitor{ private static final boolean DEBUG = false; private ConstantPoolEditor constantPoolEditor = new ConstantPoolEditor(); private StackSizeUpdater stackSizeUpdater = new StackSizeUpdater(); // Parameter for the visitor methods. private int constantIndex; // Return values for the visitor methods. private boolean isInterfaceMethod; private boolean stackSizesMayHaveChanged; // Implementations for ClassVisitor. public void visitProgramClass(ProgramClass programClass) { stackSizesMayHaveChanged = false; // Fix the constant pool entries. for (int index = 1; index < programClass.u2constantPoolCount; index++) { Constant constant = programClass.constantPool[index]; if (constant != null) { // Fix the entry, replacing it entirely if needed. this.constantIndex = index; constant.accept(programClass, this); } } // Fix class members. programClass.fieldsAccept(this); programClass.methodsAccept(this); // Fix the attributes. programClass.attributesAccept(this); } // Implementations for ConstantVisitor. public void visitAnyConstant(Clazz clazz, Constant constant) {} public void visitFieldrefConstant(Clazz clazz, FieldrefConstant fieldrefConstant) { // Do we know the referenced field? Member referencedMember = fieldrefConstant.referencedMember; if (referencedMember != null) { Clazz referencedClass = fieldrefConstant.referencedClass; // Does it have a new name or type? String newName = referencedMember.getName(referencedClass); String newType = referencedMember.getDescriptor(referencedClass); if (!fieldrefConstant.getName(clazz).equals(newName) || !fieldrefConstant.getType(clazz).equals(newType)) { if (DEBUG) { debug(clazz, fieldrefConstant, referencedClass, referencedMember); } // Update the name and type index. fieldrefConstant.u2nameAndTypeIndex = constantPoolEditor.addNameAndTypeConstant((ProgramClass)clazz, newName, newType); } } } public void visitInterfaceMethodrefConstant(Clazz clazz, InterfaceMethodrefConstant interfaceMethodrefConstant) { // Do we know the referenced interface method? Member referencedMember = interfaceMethodrefConstant.referencedMember; if (referencedMember != null) { Clazz referencedClass = interfaceMethodrefConstant.referencedClass; // Does it have a new name or type? String newName = referencedMember.getName(referencedClass); String newType = referencedMember.getDescriptor(referencedClass); if (!interfaceMethodrefConstant.getName(clazz).equals(newName) || !interfaceMethodrefConstant.getType(clazz).equals(newType)) { if (DEBUG) { debug(clazz, interfaceMethodrefConstant, referencedClass, referencedMember); } // Update the name and type index. interfaceMethodrefConstant.u2nameAndTypeIndex = constantPoolEditor.addNameAndTypeConstant((ProgramClass)clazz, 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; clazz.constantPoolEntryAccept(interfaceMethodrefConstant.u2classIndex, this); // Has the method become a non-interface method? if (!isInterfaceMethod) { if (DEBUG) { System.out.println("MemberReferenceFixer:"); System.out.println(" Class file = "+clazz.getName()); System.out.println(" Ref class = "+referencedClass.getName()); System.out.println(" Ref method = "+interfaceMethodrefConstant.getName(clazz)+interfaceMethodrefConstant.getType(clazz)); System.out.println(" -> ordinary method"); } // Replace the interface method reference by a method reference. ((ProgramClass)clazz).constantPool[this.constantIndex] = new MethodrefConstant(interfaceMethodrefConstant.u2classIndex, interfaceMethodrefConstant.u2nameAndTypeIndex, referencedClass, referencedMember); } } } public void visitMethodrefConstant(Clazz clazz, MethodrefConstant methodrefConstant) { // Do we know the referenced method? Member referencedMember = methodrefConstant.referencedMember; if (referencedMember != null) { Clazz referencedClass = methodrefConstant.referencedClass; // Does it have a new name or type? String newName = referencedMember.getName(referencedClass); String newType = referencedMember.getDescriptor(referencedClass); if (!methodrefConstant.getName(clazz).equals(newName) || !methodrefConstant.getType(clazz).equals(newType)) { if (DEBUG) { debug(clazz, methodrefConstant, referencedClass, referencedMember); } // Update the name and type index. methodrefConstant.u2nameAndTypeIndex = constantPoolEditor.addNameAndTypeConstant((ProgramClass)clazz, 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -