proguard.optimize.DuplicateInitializerInvocationFixer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of proguard-base Show documentation
Show all versions of proguard-base Show documentation
ProGuard is a free shrinker, optimizer, obfuscator, and preverifier for Java bytecode
/*
* ProGuard -- shrinking, optimization, obfuscation, and preverification
* of Java bytecode.
*
* Copyright (c) 2002-2021 Guardsquare NV
*
* 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;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import proguard.classfile.*;
import proguard.classfile.attribute.*;
import proguard.classfile.attribute.visitor.AttributeVisitor;
import proguard.classfile.constant.*;
import proguard.classfile.constant.visitor.ConstantVisitor;
import proguard.classfile.editor.CodeAttributeEditor;
import proguard.classfile.instruction.*;
import proguard.classfile.instruction.visitor.InstructionVisitor;
import proguard.classfile.util.*;
import proguard.classfile.visitor.MemberVisitor;
/**
* This AttributeVisitor adds an additional integer parameter to the tweaked
* initialization method invocations that it visits.
*/
public class DuplicateInitializerInvocationFixer
implements AttributeVisitor,
InstructionVisitor,
ConstantVisitor,
MemberVisitor
{
private static final Logger logger = LogManager.getLogger(DuplicateInitializerInvocationFixer.class);
private final InstructionVisitor extraAddedInstructionVisitor;
private final CodeAttributeEditor codeAttributeEditor = new CodeAttributeEditor();
private String descriptor;
private int descriptorLengthDelta;
/**
* Creates a new DuplicateInitializerInvocationFixer.
*/
public DuplicateInitializerInvocationFixer()
{
this(null);
}
/**
* Creates a new DuplicateInitializerInvocationFixer.
* @param extraAddedInstructionVisitor an optional extra visitor for all
* added instructions.
*/
public DuplicateInitializerInvocationFixer(InstructionVisitor extraAddedInstructionVisitor)
{
this.extraAddedInstructionVisitor = extraAddedInstructionVisitor;
}
// Implementations for AttributeVisitor.
public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
{
// Reset the code changes.
codeAttributeEditor.reset(codeAttribute.u4codeLength);
// Fix any duplicate constructor invocations.
codeAttribute.instructionsAccept(clazz,
method,
this);
// Apply all accumulated changes to the code.
codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);
}
// Implementations for InstructionVisitor.
public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
{
if (constantInstruction.opcode == Instruction.OP_INVOKESPECIAL)
{
descriptorLengthDelta = 0;
clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
if (descriptorLengthDelta > 0)
{
Instruction extraInstruction =
new SimpleInstruction(descriptorLengthDelta == 1 ?
Instruction.OP_ICONST_0 :
Instruction.OP_ACONST_NULL);
codeAttributeEditor.insertBeforeInstruction(offset,
extraInstruction);
logger.debug(" [{}.{}{}] Inserting {} before {}",
clazz.getName(),
method.getName(clazz),
method.getDescriptor(clazz),
extraInstruction.toString(),
constantInstruction.toString(offset)
);
if (extraAddedInstructionVisitor != null)
{
extraInstruction.accept(null, null, null, offset, extraAddedInstructionVisitor);
}
}
}
}
// Implementations for ConstantVisitor.
public void visitAnyMethodrefConstant(Clazz clazz, AnyMethodrefConstant anyMethodrefConstant)
{
// Check the referenced constructor descriptor.
if (anyMethodrefConstant.getName(clazz).equals(ClassConstants.METHOD_NAME_INIT))
{
descriptor = anyMethodrefConstant.getType(clazz);
anyMethodrefConstant.referencedMethodAccept(this);
}
}
// Implementations for MemberVisitor.
public void visitLibraryMethod(LibraryClass libraryClass, LibraryMethod libraryMethod) {}
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
descriptorLengthDelta =
programMethod.getDescriptor(programClass).length() - descriptor.length();
if (descriptorLengthDelta > 0)
{
logger.debug("DuplicateInitializerInvocationFixer:");
logger.debug(" [{}.{}{}] ({}) referenced by:",
programClass.getName(),
programMethod.getName(programClass),
programMethod.getDescriptor(programClass),
ClassUtil.externalClassAccessFlags(programMethod.getAccessFlags())
);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy