Skip to content

Commit

Permalink
Lint with spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
gnemyk committed Dec 15, 2024
1 parent 0d142ee commit 392a1f1
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 364 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,92 @@
import proguard.classfile.visitor.MemberVisitor;

/**
* This utility class finds the offset of the invocation to the current or super class constructor after visiting
* an <init> method.
* This utility class finds the offset of the invocation to the current or super class constructor
* after visiting an <init> method.
*
* @author Kymeng Tang
*/

public class ConstructorInvocationOffsetFinder implements MemberVisitor {
private int initOffset = -1;
private int initOffset = -1;

public int getConstructorCallOffset() {
assert initOffset != -1 :
"The constructor invocation offset is being requested before visiting any <init> member " +
"after instantiation or resetting.";
return initOffset;
}
public int getConstructorCallOffset() {
assert initOffset != -1
: "The constructor invocation offset is being requested before visiting any <init> member "
+ "after instantiation or resetting.";
return initOffset;
}

public void reset() {
this.initOffset = -1;
}
public void reset() {
this.initOffset = -1;
}

// MemberVisitor implementation
@Override
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) {
assert programMethod.getName(programClass).equals(ClassConstants.METHOD_NAME_INIT) :
this.getClass().getName() + " only supports constructor but " + programClass.getName() +
"." + programMethod.getName(programClass) + programMethod.getDescriptor(programClass) +
" is being visited.";
// MemberVisitor implementation
@Override
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod) {
assert programMethod.getName(programClass).equals(ClassConstants.METHOD_NAME_INIT)
: this.getClass().getName()
+ " only supports constructor but "
+ programClass.getName()
+ "."
+ programMethod.getName(programClass)
+ programMethod.getDescriptor(programClass)
+ " is being visited.";

assert initOffset == -1 :
"This instance of " + this.getClass().getName() + " has already visited an <init> member; " +
"To avoid overriding the previously found offset, please store the return value of " +
"getConstructorCallOffset(), and call reset() method.";
assert initOffset == -1
: "This instance of "
+ this.getClass().getName()
+ " has already visited an <init> member; "
+ "To avoid overriding the previously found offset, please store the return value of "
+ "getConstructorCallOffset(), and call reset() method.";

programMethod.attributesAccept(programClass,
new AttributeNameFilter(Attribute.CODE,
new ConstructorOffsetFinderImpl()));
}
programMethod.attributesAccept(
programClass, new AttributeNameFilter(Attribute.CODE, new ConstructorOffsetFinderImpl()));
}

private class ConstructorOffsetFinderImpl implements AttributeVisitor, InstructionVisitor, ConstantVisitor {
// AttributeVisitor implementation
@Override
public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
private class ConstructorOffsetFinderImpl
implements AttributeVisitor, InstructionVisitor, ConstantVisitor {
// AttributeVisitor implementation
@Override
public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}

@Override
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) {
codeAttribute.accept(clazz, method,
new AllInstructionVisitor(
new InstructionOpCodeFilter(new int[] {Instruction.OP_INVOKESPECIAL}, this)));
}
@Override
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) {
codeAttribute.accept(
clazz,
method,
new AllInstructionVisitor(
new InstructionOpCodeFilter(new int[] {Instruction.OP_INVOKESPECIAL}, this)));
}

// InstructionVisitor implementation
@Override
public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
// InstructionVisitor implementation
@Override
public void visitAnyInstruction(
Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
Instruction instruction) {}

@Override
public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction) {
clazz.constantPoolEntryAccept(constantInstruction.constantIndex, new ConstantVisitor() {
@Override
public void visitAnyConstant(Clazz clazz, Constant constant) {}
@Override
public void visitConstantInstruction(
Clazz clazz,
Method method,
CodeAttribute codeAttribute,
int offset,
ConstantInstruction constantInstruction) {
clazz.constantPoolEntryAccept(
constantInstruction.constantIndex,
new ConstantVisitor() {
@Override
public void visitAnyConstant(Clazz clazz, Constant constant) {}

@Override
public void visitMethodrefConstant(Clazz clazz, MethodrefConstant methodrefConstant) {
if (methodrefConstant.getName(clazz).equals(ClassConstants.METHOD_NAME_INIT)) {
initOffset = offset;
}
}
});
}
@Override
public void visitMethodrefConstant(Clazz clazz, MethodrefConstant methodrefConstant) {
if (methodrefConstant.getName(clazz).equals(ClassConstants.METHOD_NAME_INIT)) {
initOffset = offset;
}
}
});
}
}
}
}
Loading

0 comments on commit 392a1f1

Please sign in to comment.