Skip to content

Commit

Permalink
feat(objectionary#528): remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Apr 3, 2024
1 parent 1e4442f commit 64f5331
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,30 +242,19 @@ public String testCode() {
@SuppressWarnings("PMD.AvoidCatchingGenericException")
void write() {
try {
if (this.stack == 0 && this.locals == 0) {
final MethodVisitor mvisitor = this.properties.writeCustomMethodWithComputation(
this.visitor);
this.annotations.forEach(annotation -> annotation.write(mvisitor));
this.defvalues.forEach(defvalue -> defvalue.writeTo(mvisitor));
if (!this.properties.isAbstract()) {
mvisitor.visitCode();
this.tryblocks.forEach(block -> block.writeTo(mvisitor));
this.instructions.forEach(instruction -> instruction.writeTo(mvisitor));
mvisitor.visitMaxs(this.stack, this.locals);
}
mvisitor.visitEnd();
} else {
final MethodVisitor mvisitor = this.properties.writeMethod(this.visitor);
this.annotations.forEach(annotation -> annotation.write(mvisitor));
this.defvalues.forEach(defvalue -> defvalue.writeTo(mvisitor));
if (!this.properties.isAbstract()) {
mvisitor.visitCode();
this.tryblocks.forEach(block -> block.writeTo(mvisitor));
this.instructions.forEach(instruction -> instruction.writeTo(mvisitor));
mvisitor.visitMaxs(this.stack, this.locals);
}
mvisitor.visitEnd();
final MethodVisitor mvisitor = this.properties.writeMethod(
this.visitor,
this.stack == 0 && this.locals == 0
);
this.annotations.forEach(annotation -> annotation.write(mvisitor));
this.defvalues.forEach(defvalue -> defvalue.writeTo(mvisitor));
if (!this.properties.isAbstract()) {
mvisitor.visitCode();
this.tryblocks.forEach(block -> block.writeTo(mvisitor));
this.instructions.forEach(instruction -> instruction.writeTo(mvisitor));
mvisitor.visitMaxs(this.stack, this.locals);
}
mvisitor.visitEnd();
} catch (final NegativeArraySizeException exception) {
throw new IllegalStateException(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,12 @@ public boolean isAbstract() {
return (this.access & Opcodes.ACC_ABSTRACT) != 0;
}


/**
* Add method to class writer.
* @param writer Class writer.
* @return Method visitor.
*/
MethodVisitor writeCustomMethodWithComputation(final CustomClassVisitor writer) {
Logger.debug(
this,
String.format("Creating method visitor with the following properties %s", this)
);
return writer.visitCustomMethodWithComputation(
this.access,
new JavaName(this.name).decode(),
this.descriptor,
this.signature,
this.exceptions
);
}

/**
* Add method to class writer.
* @param writer Class writer.
* @return Method visitor.
*/
MethodVisitor writeMethod(final CustomClassVisitor writer) {
MethodVisitor writeMethod(final CustomClassVisitor writer, final boolean compute) {
Logger.debug(
this,
String.format("Creating method visitor with the following properties %s", this)
Expand All @@ -167,7 +147,8 @@ MethodVisitor writeMethod(final CustomClassVisitor writer) {
new JavaName(this.name).decode(),
this.descriptor,
this.signature,
this.exceptions
this.exceptions,
compute
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,26 @@ public CustomClassVisitor(final int api, final CustomClassWriter writer) {
this.writer = writer;
}

public MethodVisitor visitCustomMethodWithComputation(
public MethodVisitor visitMethod(
final int access,
final String name,
final String descriptor,
final String signature,
final String[] exceptions,
final boolean compute
) {
MethodVisitor result;
if (compute) {
result = this.visitCustomMethodWithComputation(
access, name, descriptor, signature, exceptions
);
} else {
result = super.visitMethod(access, name, descriptor, signature, exceptions);
}
return result;
}

private MethodVisitor visitCustomMethodWithComputation(
final int access,
final String name,
final String descriptor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@
*/
public final class VerifiedClassWriter extends CustomClassWriter {

/**
* Default constructor.
*/
public VerifiedClassWriter() {
this(ClassWriter.COMPUTE_FRAMES);
}

/**
* Constructor.
* @param flags Flags. See {@link ClassWriter#COMPUTE_FRAMES} for more info.
Expand Down

0 comments on commit 64f5331

Please sign in to comment.