Skip to content

Commit

Permalink
feat(#537): fix some of the code offences
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Sep 12, 2024
1 parent 12e9337 commit a003b73
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.cactoos.scalar.Unchecked;
import org.eolang.jeo.Details;
import org.eolang.jeo.Representation;
import org.eolang.jeo.representation.asm.ASMProgram;
import org.eolang.jeo.representation.asm.AsmProgram;
import org.eolang.jeo.representation.bytecode.Bytecode;
import org.eolang.jeo.representation.directives.DirectivesProgram;
import org.objectweb.asm.ClassReader;
Expand Down Expand Up @@ -119,7 +119,7 @@ public XML toEO() {
* @return XML representation of bytecode.
*/
public XML toEO(final boolean count) {
final DirectivesProgram directives = new ASMProgram(this.input.value())
final DirectivesProgram directives = new AsmProgram(this.input.value())
.bytecode()
.directives(new Base64Bytecode(this.input.value()).asString(), count);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,37 @@
* The recent version with the Visitor pattern is still available in the history:
* <a href="https://github.com/objectionary/jeo-maven-plugin/tree/29daa0a167b5c2ba4caaceafb6e6bafc381ac05c">github</a>
* @since 0.6
* @todo #537:60min Refactor {@link ASMProgram} class.
* @todo #537:60min Refactor {@link AsmProgram} class.
* It's too big and contains a lot of methods.
* We need to refactor it to make it more readable and maintainable.
* Maybe it's worth splitting it into several classes.
* Don't forget to add/update the tests.
* Don't forget to remove PMD and Checkstyle suppressions.
* @checkstyle CyclomaticComplexityCheck (500 lines)
* @checkstyle JavaNCSSCheck (500 lines)
* @checkstyle ClassFanOutComplexityCheck (500 lines)
* @checkstyle MethodLengthCheck (500 lines)
* @checkstyle AnonInnerLengthCheck (500 lines)
*/
public final class ASMProgram {
@SuppressWarnings({
"PMD.CouplingBetweenObjects",
"PMD.TooManyMethods",
"PMD.NcssCount",
"PMD.UncommentedEmptyMethodBody"
})
public final class AsmProgram {

/**
* Bytecode as plain bytes.
*/
private final byte[] bytecode;
private final byte[] bytes;

/**
* Constructor.
* @param bytes Bytes.
*/
public ASMProgram(final byte... bytes) {
this.bytecode = bytes.clone();
public AsmProgram(final byte... bytes) {
this.bytes = bytes.clone();
}

/**
Expand All @@ -116,11 +128,11 @@ public ASMProgram(final byte... bytes) {
*/
public BytecodeProgram bytecode() {
final ClassNode node = new ClassNode();
new ClassReader(this.bytecode).accept(node, 0);
new ClassReader(this.bytes).accept(node, 0);
final ClassName full = new ClassName(node.name);
return new BytecodeProgram(
full.pckg(),
ASMProgram.clazz(node)
AsmProgram.clazz(node)
);
}

Expand All @@ -133,10 +145,10 @@ private static BytecodeClass clazz(final ClassNode node) {
final ClassName full = new ClassName(node.name);
return new BytecodeClass(
full.name(),
ASMProgram.methods(node),
ASMProgram.fields(node),
ASMProgram.annotations(node),
ASMProgram.innerClasses(node),
AsmProgram.methods(node),
AsmProgram.fields(node),
AsmProgram.annotations(node),
AsmProgram.innerClasses(node),
new BytecodeClassProperties(
node.version,
node.access,
Expand All @@ -153,7 +165,7 @@ private static BytecodeClass clazz(final ClassNode node) {
* @return Domain field.
*/
private static Collection<BytecodeField> fields(final ClassNode node) {
return node.fields.stream().map(ASMProgram::field).collect(Collectors.toList());
return node.fields.stream().map(AsmProgram::field).collect(Collectors.toList());
}

/**
Expand All @@ -168,7 +180,7 @@ private static BytecodeField field(final FieldNode node) {
node.signature,
node.value,
node.access,
ASMProgram.annotations(node)
AsmProgram.annotations(node)
);
}

Expand All @@ -178,7 +190,7 @@ private static BytecodeField field(final FieldNode node) {
* @return Domain methods.
*/
private static Collection<BytecodeMethod> methods(final ClassNode node) {
return node.methods.stream().map(ASMProgram::method).collect(Collectors.toList());
return node.methods.stream().map(AsmProgram::method).collect(Collectors.toList());
}

/**
Expand All @@ -188,19 +200,19 @@ private static Collection<BytecodeMethod> methods(final ClassNode node) {
*/
private static BytecodeMethod method(final MethodNode node) {
return new BytecodeMethod(
ASMProgram.tryblocks(node),
ASMProgram.instructions(node),
ASMProgram.annotations(node),
AsmProgram.tryblocks(node),
AsmProgram.instructions(node),
AsmProgram.annotations(node),
new BytecodeMethodProperties(
node.access,
node.name,
node.desc,
node.signature,
ASMProgram.parameters(node),
AsmProgram.parameters(node),
node.exceptions.toArray(new String[0])
),
ASMProgram.defvalues(node),
ASMProgram.maxs(node)
AsmProgram.defvalues(node),
AsmProgram.maxs(node)
);
}

Expand Down Expand Up @@ -232,8 +244,8 @@ private static BytecodeParameters parameters(final MethodNode node) {
index,
new BytecodeAnnotations(
Stream.concat(
ASMProgram.safe(visible[index], true),
ASMProgram.safe(invisible[index], false)
AsmProgram.safe(visible[index], true),
AsmProgram.safe(invisible[index], false)
)
).annotations()
)
Expand All @@ -256,7 +268,7 @@ private static BytecodeMaxs maxs(final MethodNode node) {
* @return Domain method tryblocks.
*/
private static List<BytecodeEntry> tryblocks(final MethodNode node) {
return node.tryCatchBlocks.stream().map(ASMProgram::tryblock).collect(Collectors.toList());
return node.tryCatchBlocks.stream().map(AsmProgram::tryblock).collect(Collectors.toList());
}

/**
Expand All @@ -280,7 +292,7 @@ private static BytecodeEntry tryblock(final TryCatchBlockNode node) {
*/
private static Collection<BytecodeAttribute> innerClasses(final ClassNode node) {
return node.innerClasses.stream()
.map(ASMProgram::innerClass)
.map(AsmProgram::innerClass)
.collect(Collectors.toList());
}

Expand All @@ -305,7 +317,7 @@ private static BytecodeAttribute innerClass(final InnerClassNode node) {
*/
private static List<BytecodeEntry> instructions(final MethodNode node) {
return Arrays.stream(node.instructions.toArray())
.map(ASMProgram::instruction)
.map(AsmProgram::instruction)
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -341,22 +353,22 @@ private static BytecodeEntry instruction(final AbstractInsnNode node) {
);
break;
case AbstractInsnNode.FIELD_INSN:
final FieldInsnNode fieldInsnNode = FieldInsnNode.class.cast(node);
final FieldInsnNode field = FieldInsnNode.class.cast(node);
result = new BytecodeInstructionEntry(
fieldInsnNode.getOpcode(),
fieldInsnNode.owner,
fieldInsnNode.name,
fieldInsnNode.desc
field.getOpcode(),
field.owner,
field.name,
field.desc
);
break;
case AbstractInsnNode.METHOD_INSN:
final MethodInsnNode methodInsnNode = MethodInsnNode.class.cast(node);
final MethodInsnNode method = MethodInsnNode.class.cast(node);
result = new BytecodeInstructionEntry(
methodInsnNode.getOpcode(),
methodInsnNode.owner,
methodInsnNode.name,
methodInsnNode.desc,
methodInsnNode.itf
method.getOpcode(),
method.owner,
method.name,
method.desc,
method.itf
);
break;
case AbstractInsnNode.INVOKE_DYNAMIC_INSN:
Expand Down Expand Up @@ -440,8 +452,7 @@ private static BytecodeEntry instruction(final AbstractInsnNode node) {
case AbstractInsnNode.LINE:
result = new BytecodeEntry() {
@Override
public void writeTo(final MethodVisitor visitor) {

public void writeTo(final MethodVisitor ignore) {
}

@Override
Expand Down Expand Up @@ -480,8 +491,8 @@ public String testCode() {
*/
private static Collection<BytecodeAnnotation> annotations(final ClassNode node) {
return Stream.concat(
ASMProgram.safe(node.visibleAnnotations, true),
ASMProgram.safe(node.invisibleAnnotations, false)
AsmProgram.safe(node.visibleAnnotations, true),
AsmProgram.safe(node.invisibleAnnotations, false)
).collect(Collectors.toList());
}

Expand All @@ -492,8 +503,8 @@ private static Collection<BytecodeAnnotation> annotations(final ClassNode node)
*/
private static List<BytecodeAnnotation> annotations(final MethodNode node) {
return Stream.concat(
ASMProgram.safe(node.visibleAnnotations, true),
ASMProgram.safe(node.invisibleAnnotations, false)
AsmProgram.safe(node.visibleAnnotations, true),
AsmProgram.safe(node.invisibleAnnotations, false)
).collect(Collectors.toList());
}

Expand All @@ -505,8 +516,8 @@ private static List<BytecodeAnnotation> annotations(final MethodNode node) {
private static BytecodeAnnotations annotations(final FieldNode node) {
return new BytecodeAnnotations(
Stream.concat(
ASMProgram.safe(node.visibleAnnotations, true),
ASMProgram.safe(node.invisibleAnnotations, false)
AsmProgram.safe(node.visibleAnnotations, true),
AsmProgram.safe(node.invisibleAnnotations, false)
)
);
}
Expand All @@ -517,11 +528,13 @@ private static BytecodeAnnotations annotations(final FieldNode node) {
* @param visible Is it visible?
* @return Annotations.
*/
private static Stream<BytecodeAnnotation> safe(List<AnnotationNode> nodes, boolean visible) {
private static Stream<BytecodeAnnotation> safe(
final List<AnnotationNode> nodes, final boolean visible
) {
return Optional.ofNullable(nodes)
.orElse(new ArrayList<>(0))
.stream()
.map(ann -> ASMProgram.annotation(ann, visible));
.map(ann -> AsmProgram.annotation(ann, visible));
}

/**
Expand All @@ -531,12 +544,12 @@ private static Stream<BytecodeAnnotation> safe(List<AnnotationNode> nodes, boole
* @return Domain annotation.
*/
private static BytecodeAnnotation annotation(final AnnotationNode node, final boolean visible) {
List<BytecodeAnnotationValue> properties = new ArrayList<>(0);
final List<BytecodeAnnotationValue> properties = new ArrayList<>(0);
final List<Object> values = Optional.ofNullable(node.values)
.orElse(new ArrayList<>(0));
for (int index = 0; index < values.size(); index += 2) {
properties.add(
ASMProgram.annotationProperty(
AsmProgram.annotationProperty(
(String) values.get(index),
values.get(index + 1)
)
Expand Down Expand Up @@ -564,14 +577,14 @@ private static BytecodeAnnotationValue annotationProperty(
name,
cast.desc,
cast.values.stream().map(
val -> ASMProgram.annotationProperty("", val)
val -> AsmProgram.annotationProperty("", val)
).collect(Collectors.toList())
);
} else if (value instanceof List) {
result = BytecodeAnnotationProperty.array(
name,
((Collection<?>) value).stream()
.map(val -> ASMProgram.annotationProperty("", val))
.map(val -> AsmProgram.annotationProperty("", val))
.collect(Collectors.toList())
);
} else {
Expand All @@ -592,8 +605,7 @@ private static List<BytecodeDefaultValue> defvalues(final MethodNode node) {
} else {
result = Collections.singletonList(
new BytecodeDefaultValue(
ASMProgram.annotationProperty(
null, node.annotationDefault)
AsmProgram.annotationProperty(null, node.annotationDefault)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ public void writeTo(final AnnotationVisitor avisitor) {
avisitor.visitArray(null).visitEnd();
} else {
final AnnotationVisitor array = avisitor.visitArray(
Optional.ofNullable(this.params.get(0)
).map(String.class::cast)
Optional.ofNullable(this.params.get(0))
.map(String.class::cast)
.orElse(null)
);
for (final Object param : this.params.subList(1, this.params.size())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,29 @@ public String testCode() {
return res.toString();
}

/**
* Generate directives.
* @param counting Whether to count opcodes.
* @return Directives.
*/
public DirectivesMethod directives(final boolean counting) {
return new DirectivesMethod(
new Signature(
new MethodName(this.properties.name()).xmir(), this.properties.descriptor()
),
this.properties.directives(this.maxs),
this.instructions.stream().map(entry -> entry.directives(counting))
.collect(Collectors.toList()),
this.tryblocks.stream().map(entry -> entry.directives(counting))
.collect(Collectors.toList()),
new BytecodeAnnotations(this.annotations).directives(),
this.defvalues.stream()
.map(BytecodeDefaultValue::directives)
.collect(Collectors.toList()),
counting
);
}

/**
* Generate bytecode.
* @param visitor Visitor.
Expand Down Expand Up @@ -308,28 +331,6 @@ void write(final CustomClassWriter visitor) {
}
}

/**
* Generate directives.
* @param counting Whether to count opcodes.
* @return Directives.
*/
public DirectivesMethod directives(final boolean counting) {
return new DirectivesMethod(
new Signature(
new MethodName(this.properties.name()).xmir(), this.properties.descriptor()),
this.properties.directives(this.maxs),
this.instructions.stream().map(entry -> entry.directives(counting))
.collect(Collectors.toList()),
this.tryblocks.stream().map(entry -> entry.directives(counting))
.collect(Collectors.toList()),
new BytecodeAnnotations(this.annotations).directives(),
this.defvalues.stream()
.map(BytecodeDefaultValue::directives)
.collect(Collectors.toList()),
counting
);
}

/**
* Add instruction.
* @param opcode Opcode.
Expand Down
Loading

0 comments on commit a003b73

Please sign in to comment.