Skip to content

Commit

Permalink
feat(#226): fix all qulice suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Nov 3, 2023
1 parent 37fc087 commit 1ffd501
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
@SuppressWarnings("PMD.TooManyMethods")
public final class DirectivesMethod extends MethodVisitor implements Iterable<Directive> {

private final Map<Label, String> labels = new HashMap<>(0);
/**
* All labels inside a method.
*/
private final Map<Label, String> labels;

/**
* Xembly directives.
Expand All @@ -60,6 +63,7 @@ public final class DirectivesMethod extends MethodVisitor implements Iterable<Di
) {
super(new DefaultVersion().api(), visitor);
this.directives = directives;
this.labels = new HashMap<>(0);
}

@Override
Expand Down Expand Up @@ -135,12 +139,15 @@ public void visitEnd() {
super.visitEnd();
}


@Override
public Iterator<Directive> 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")
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/eolang/jeo/representation/xmir/XmlCommand.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @since 0.1
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class XmlInstruction implements XmlCommand{
public final class XmlInstruction implements XmlCommand {

/**
* Instruction node.
Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<String, Label> LABELS = new ConcurrentHashMap<>();
private static final Map<String, Label> LABELS = new ConcurrentHashMap<>();

/**
* Label node.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ public List<XmlInstruction> 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<XmlCommand> commands() {
return new XmlNode(this.node).child("base", "seq")
.children()
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/eolang/jeo/representation/xmir/XmlNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
);
}
Expand Down

6 comments on commit 1ffd501

@0pdd
Copy link

@0pdd 0pdd commented on 1ffd501 Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 217-e6d26aba disappeared from src/main/java/org/eolang/jeo/representation/directives/DirectivesMethod.java), that's why I closed #226. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on 1ffd501 Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 226-921967db discovered in src/main/java/org/eolang/jeo/representation/directives/DirectivesMethod.java) and submitted as #232. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 1ffd501 Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 226-a35de1fc discovered in src/main/java/org/eolang/jeo/representation/xmir/XmlCommand.java) and submitted as #233. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 1ffd501 Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 226-c0291c63 discovered in src/main/java/org/eolang/jeo/representation/xmir/XmlLabel.java) and submitted as #234. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 1ffd501 Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 226-c5fc1ccd discovered in src/main/java/org/eolang/jeo/representation/xmir/XmlMethod.java) and submitted as #235. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 1ffd501 Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 226-b1e41680 discovered in src/test/java/org/eolang/jeo/representation/directives/DirectivesMethodTest.java) and submitted as #236. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.