Skip to content

Commit

Permalink
feat(objectionary#528): fix all linter suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Apr 3, 2024
1 parent 92758fa commit 1324d4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public BytecodeMethodProperties(
this.name = name;
this.descriptor = descriptor;
this.signature = signature;
this.exceptions = exceptions;
this.exceptions = exceptions.clone();
}

@Override
Expand All @@ -126,15 +126,16 @@ public String testCode() {

/**
* Is method abstract.
* @return True if method is abstract.
* @return True if the method is abstract.
*/
public boolean isAbstract() {
return (this.access & Opcodes.ACC_ABSTRACT) != 0;
}

/**
* Add method to class writer.
* Add method to a class writer.
* @param writer Class writer.
* @param compute If frames should be computed.
* @return Method visitor.
*/
MethodVisitor writeMethod(final CustomClassWriter writer, final boolean compute) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
import org.objectweb.asm.tree.analysis.SimpleVerifier;
import org.objectweb.asm.util.CheckClassAdapter;

/**
* Custom class writer.
*
* @since 0.3
*/
public final class CustomClassWriter extends ClassVisitor {

/**
Expand All @@ -54,7 +59,7 @@ public final class CustomClassWriter extends ClassVisitor {
* @param verify Verify bytecode.
*/
CustomClassWriter(final boolean verify) {
this(CustomClassWriter.writer(verify));
this(CustomClassWriter.prestructor(verify));
}

/**
Expand Down Expand Up @@ -92,6 +97,7 @@ public Bytecode bytecode() {
* @param exceptions Method exceptions.
* @param compute If frames should be computed.
* @return Method visitor.
* @checkstyle ParameterNumberCheck (5 lines)
*/
MethodVisitor visitMethod(
final int access,
Expand All @@ -118,41 +124,44 @@ MethodVisitor visitMethod(
* @param signature Method signature.
* @param exceptions Method exceptions.
* @return Method visitor.
* @checkstyle ParameterNumberCheck (5 lines)
*/
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
private MethodVisitor visitMethodWithoutFrames(
final int access,
final String name,
final String descriptor,
final String signature,
final String[] exceptions
final String... exceptions
) {
final ClassVisitor delegate = this.getDelegate();
try {
final Field field = ClassWriter.class.getDeclaredField("compute");
field.setAccessible(true);
field.setInt(this.writer, 4);
final int previous = field.getInt(delegate);
field.setInt(delegate, 4);
final MethodVisitor original = this.visitMethod(
access, name, descriptor, signature, exceptions
);
field.setInt(this.writer, 0);
field.setInt(delegate, previous);
return original;
} catch (final NoSuchFieldException | IllegalAccessException exception) {
throw new IllegalStateException(
String.format(
"Can't set compute field for ASM ClassWriter '%s' and change the computation mode to COMPUTE_ALL_FRAMES",
this.writer
delegate
),
exception
);
}
}


/**
* Which class writer to use.
* @param verify Verify bytecode.
* @return A verified class writer if verify is true, otherwise custom class writer.
*/
private static ClassesAwareWriter writer(final boolean verify) {
private static ClassesAwareWriter prestructor(final boolean verify) {
final ClassesAwareWriter result;
if (verify) {
result = new VerifiedClassWriter();
Expand Down

0 comments on commit 1324d4e

Please sign in to comment.