From 1ffd501139963f5c867bc28609c40e53927bcd0f Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Fri, 3 Nov 2023 18:47:56 +0300 Subject: [PATCH] feat(#226): fix all qulice suggestions --- .../bytecode/BytecodeInstruction.java | 34 ++++++++++++++++- .../bytecode/BytecodeMethod.java | 6 ++- .../bytecode/CommandInstruction.java | 7 ++-- .../bytecode/MarkLabelInstruction.java | 38 ++++++++++++++++++- .../directives/DirectivesMethod.java | 11 +++++- .../jeo/representation/xmir/XmlCommand.java | 24 ++++++++++++ .../representation/xmir/XmlInstruction.java | 4 +- .../jeo/representation/xmir/XmlLabel.java | 5 ++- .../jeo/representation/xmir/XmlMethod.java | 8 ++++ .../jeo/representation/xmir/XmlNode.java | 6 ++- .../BytecodeRepresentationTest.java | 7 +--- 11 files changed, 129 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeInstruction.java b/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeInstruction.java index 40e423cfd..d7540ec51 100644 --- a/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeInstruction.java +++ b/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeInstruction.java @@ -1,7 +1,39 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2023 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package org.eolang.jeo.representation.bytecode; import org.objectweb.asm.MethodVisitor; +/** + * Bytecode instruction. + * Might be a label, a jump, a method call, etc. + * @since 0.1 + */ public interface BytecodeInstruction { - void generate(final MethodVisitor visitor); + /** + * Write instruction to the method visitor. + * @param visitor Method visitor. + */ + void generate(MethodVisitor visitor); } diff --git a/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeMethod.java b/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeMethod.java index 546dec657..b18e24ea1 100644 --- a/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeMethod.java +++ b/src/main/java/org/eolang/jeo/representation/bytecode/BytecodeMethod.java @@ -99,7 +99,11 @@ public BytecodeClass up() { return this.clazz; } - + /** + * Add label. + * @param label Label. + * @return This object. + */ public BytecodeMethod markLabel(final Label label) { this.instructions.add(new MarkLabelInstruction(label)); return this; diff --git a/src/main/java/org/eolang/jeo/representation/bytecode/CommandInstruction.java b/src/main/java/org/eolang/jeo/representation/bytecode/CommandInstruction.java index b1e5f4497..9d0eaa480 100644 --- a/src/main/java/org/eolang/jeo/representation/bytecode/CommandInstruction.java +++ b/src/main/java/org/eolang/jeo/representation/bytecode/CommandInstruction.java @@ -70,10 +70,6 @@ final class CommandInstruction implements BytecodeInstruction { this.args = args; } - /** - * Generate bytecode. - * @param visitor Method visitor. - */ @Override public void generate(final MethodVisitor visitor) { Instruction.find(this.opcode).generate(visitor, this.args); @@ -222,6 +218,9 @@ private enum Instruction { visitor.visitInsn(Opcodes.DCMPL) ), + /** + * If value is less than or equal to 0, branch to instruction at branchoffset. + */ IFLE(Opcodes.IFLE, (visitor, arguments) -> visitor.visitJumpInsn( Opcodes.IFLE, diff --git a/src/main/java/org/eolang/jeo/representation/bytecode/MarkLabelInstruction.java b/src/main/java/org/eolang/jeo/representation/bytecode/MarkLabelInstruction.java index 7d3d8f8fb..a33cb2b4b 100644 --- a/src/main/java/org/eolang/jeo/representation/bytecode/MarkLabelInstruction.java +++ b/src/main/java/org/eolang/jeo/representation/bytecode/MarkLabelInstruction.java @@ -1,13 +1,47 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2023 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package org.eolang.jeo.representation.bytecode; import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; -public class MarkLabelInstruction implements BytecodeInstruction { +/** + * Mark label instruction. + * @since 0.1 + */ +public final class MarkLabelInstruction implements BytecodeInstruction { + /** + * Label. + */ private final Label label; - public MarkLabelInstruction(final Label label) { + /** + * Constructor. + * @param label Label. + */ + MarkLabelInstruction(final Label label) { this.label = label; } diff --git a/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethod.java b/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethod.java index 3bee63289..197e6dd03 100644 --- a/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethod.java +++ b/src/main/java/org/eolang/jeo/representation/directives/DirectivesMethod.java @@ -42,7 +42,10 @@ @SuppressWarnings("PMD.TooManyMethods") public final class DirectivesMethod extends MethodVisitor implements Iterable { - private final Map labels = new HashMap<>(0); + /** + * All labels inside a method. + */ + private final Map labels; /** * Xembly directives. @@ -60,6 +63,7 @@ public final class DirectivesMethod extends MethodVisitor implements Iterable(0); } @Override @@ -135,12 +139,15 @@ public void visitEnd() { super.visitEnd(); } - @Override public Iterator iterator() { return this.directives.iterator(); } + /** + * Add label to the directives. + * @param id Label id. + */ private void label(final String id) { this.directives.add("o") .attr("base", "label") diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlCommand.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlCommand.java index 68ac2f224..792514774 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlCommand.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlCommand.java @@ -1,9 +1,33 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2023 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package org.eolang.jeo.representation.xmir; import org.eolang.jeo.representation.bytecode.BytecodeMethod; /** * XML representation of bytecode instruction or a label. + * @since 0.1 * @todo #226:90min Rename XmlCommand to XmlInstruction. * XmlCommand is a bad name for this class. It is not a command, it is an instruction in general. * Since instruction could be a label or a bytecode instruction, it is better to rename this class diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlInstruction.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlInstruction.java index 4de5a72f9..91cfc6d41 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlInstruction.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlInstruction.java @@ -40,7 +40,7 @@ * @since 0.1 */ @SuppressWarnings("PMD.TooManyMethods") -public final class XmlInstruction implements XmlCommand{ +public final class XmlInstruction implements XmlCommand { /** * Instruction node. @@ -77,7 +77,7 @@ public int code() { } @Override - public void writeTo(BytecodeMethod method) { + public void writeTo(final BytecodeMethod method) { method.instruction(this.code(), this.arguments()); } diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlLabel.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlLabel.java index 3ca78a83d..1c28b39c4 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlLabel.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlLabel.java @@ -32,7 +32,7 @@ * XML representation of bytecode label. * @since 0.1 */ -public class XmlLabel implements XmlCommand { +public final class XmlLabel implements XmlCommand { /** * All Labels. @@ -41,8 +41,9 @@ public class XmlLabel implements XmlCommand { * transformation is not implemented yet. We need to implement it and add or change integration * tests to check that conditional instructions are correctly transformed. * Moreover, we have to remove static field LABELS and use different approach to find labels. + * @checkstyle StaticVariableNameCheck (3 lines) */ - private final static Map LABELS = new ConcurrentHashMap<>(); + private static final Map LABELS = new ConcurrentHashMap<>(); /** * Label node. diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlMethod.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlMethod.java index d100c26a1..e225cb739 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlMethod.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlMethod.java @@ -128,6 +128,14 @@ public List instructionsWithout(final int... opcodes) { ); } + /** + * All method instructions. + * @return Instructions. + * @todo #226:90min Code duplication with 'instructions' method. + * Currently we have two methods that return instructions. They are 'instructions' and + * 'commads'. We have to remove code duplication between them. Maybe it makes sense to + * leave only one method and remove the other one. + */ public List commands() { return new XmlNode(this.node).child("base", "seq") .children() diff --git a/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java b/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java index 83c4e5949..cfc5a3e47 100644 --- a/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java +++ b/src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java @@ -110,11 +110,13 @@ XmlClass toClass() { * @return Command. */ XmlCommand toCommand() { + final XmlCommand result; if (this.attribute("name").isPresent()) { - return new XmlInstruction(this.node); + result = new XmlInstruction(this.node); } else { - return new XmlLabel(this); + result = new XmlLabel(this); } + return result; } /** diff --git a/src/test/java/org/eolang/jeo/representation/BytecodeRepresentationTest.java b/src/test/java/org/eolang/jeo/representation/BytecodeRepresentationTest.java index a5e16c927..0742a9cb7 100644 --- a/src/test/java/org/eolang/jeo/representation/BytecodeRepresentationTest.java +++ b/src/test/java/org/eolang/jeo/representation/BytecodeRepresentationTest.java @@ -23,7 +23,6 @@ */ package org.eolang.jeo.representation; -import com.jcabi.xml.XML; import org.cactoos.bytes.BytesOf; import org.cactoos.bytes.UncheckedBytes; import org.cactoos.io.ResourceOf; @@ -46,12 +45,10 @@ final class BytecodeRepresentationTest { @Test void parsesBytecode() { - final XML eo = new BytecodeRepresentation( - new ResourceOf(BytecodeRepresentationTest.METHOD_BYTE)) - .toEO(); MatcherAssert.assertThat( "The simplest class should contain the object with MethodByte name", - eo.xpath("/program/@name").get(0), + new BytecodeRepresentation(new ResourceOf(BytecodeRepresentationTest.METHOD_BYTE)) + .toEO().xpath("/program/@name").get(0), Matchers.equalTo("org/eolang/jeo/MethodByte") ); }