From 43a4b88cb7a35c539c6ac17689aee9caeb91a158 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 12 Aug 2024 11:01:55 +0300 Subject: [PATCH 01/11] feat(#376): rename Add node to Addition --- .../opeo/ast/{Add.java => Addition.java} | 6 ++-- .../eolang/opeo/compilation/XmirParser.java | 4 +-- .../decompilation/handlers/AddHandler.java | 4 +-- .../ast/{AddTest.java => AdditionTest.java} | 28 +++++++++---------- .../eolang/opeo/ast/ArrayConstructorTest.java | 8 +++--- .../opeo/compilation/XmirParserTest.java | 10 +++---- .../decompilation/DecompilerMachineTest.java | 6 ++-- 7 files changed, 33 insertions(+), 33 deletions(-) rename src/main/java/org/eolang/opeo/ast/{Add.java => Addition.java} (94%) rename src/test/java/org/eolang/opeo/ast/{AddTest.java => AdditionTest.java} (86%) diff --git a/src/main/java/org/eolang/opeo/ast/Add.java b/src/main/java/org/eolang/opeo/ast/Addition.java similarity index 94% rename from src/main/java/org/eolang/opeo/ast/Add.java rename to src/main/java/org/eolang/opeo/ast/Addition.java index 906c97c1..8b1783d6 100644 --- a/src/main/java/org/eolang/opeo/ast/Add.java +++ b/src/main/java/org/eolang/opeo/ast/Addition.java @@ -41,7 +41,7 @@ */ @EqualsAndHashCode @ToString -public final class Add implements AstNode, Typed { +public final class Addition implements AstNode, Typed { /** * Left operand. @@ -58,7 +58,7 @@ public final class Add implements AstNode, Typed { * @param node XML node * @param parser Parser */ - public Add(final XmlNode node, final Function parser) { + public Addition(final XmlNode node, final Function parser) { this( parser.apply(node.children().collect(Collectors.toList()).get(0)), parser.apply(node.children().collect(Collectors.toList()).get(1)) @@ -70,7 +70,7 @@ public Add(final XmlNode node, final Function parser) { * @param left Left operand * @param right Right operand */ - public Add(final AstNode left, final AstNode right) { + public Addition(final AstNode left, final AstNode right) { this.left = left; this.right = right; } diff --git a/src/main/java/org/eolang/opeo/compilation/XmirParser.java b/src/main/java/org/eolang/opeo/compilation/XmirParser.java index b713a855..64044f02 100644 --- a/src/main/java/org/eolang/opeo/compilation/XmirParser.java +++ b/src/main/java/org/eolang/opeo/compilation/XmirParser.java @@ -27,7 +27,7 @@ import java.util.List; import java.util.stream.Collectors; import org.eolang.jeo.representation.xmir.XmlNode; -import org.eolang.opeo.ast.Add; +import org.eolang.opeo.ast.Addition; import org.eolang.opeo.ast.ArrayConstructor; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Attributes; @@ -136,7 +136,7 @@ public AstNode parse(final XmlNode node) { } else if ("duplicated".equals(base)) { result = new Duplicate(this.parse(node.firstChild())); } else if (".plus".equals(base)) { - result = new Add(node, this::parse); + result = new Addition(node, this::parse); } else if (".minus".equals(base)) { result = new Substraction(node, this::parse); } else if ("cast".equals(base)) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/AddHandler.java b/src/main/java/org/eolang/opeo/decompilation/handlers/AddHandler.java index 7dd81fdc..006b52c9 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/AddHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/handlers/AddHandler.java @@ -23,7 +23,7 @@ */ package org.eolang.opeo.decompilation.handlers; -import org.eolang.opeo.ast.Add; +import org.eolang.opeo.ast.Addition; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.InstructionHandler; @@ -38,7 +38,7 @@ public final class AddHandler implements InstructionHandler { public void handle(final DecompilerState state) { final AstNode right = state.stack().pop(); final AstNode left = state.stack().pop(); - state.stack().push(new Add(left, right)); + state.stack().push(new Addition(left, right)); } } diff --git a/src/test/java/org/eolang/opeo/ast/AddTest.java b/src/test/java/org/eolang/opeo/ast/AdditionTest.java similarity index 86% rename from src/test/java/org/eolang/opeo/ast/AddTest.java rename to src/test/java/org/eolang/opeo/ast/AdditionTest.java index 94aca802..cca090a9 100644 --- a/src/test/java/org/eolang/opeo/ast/AddTest.java +++ b/src/test/java/org/eolang/opeo/ast/AdditionTest.java @@ -34,14 +34,14 @@ import org.xembly.Xembler; /** - * Test case for {@link Add}. + * Test case for {@link Addition}. * @since 0.1 */ -final class AddTest { +final class AdditionTest { @Test void convertsToXmir() throws ImpossibleModificationException { - final String res = new Xembler(new Add(new Literal(1), new Literal(2)).toXmir()).xml(); + final String res = new Xembler(new Addition(new Literal(1), new Literal(2)).toXmir()).xml(); MatcherAssert.assertThat( String.format( "Can't convert to correct XMIR, result is : %n%s%n", @@ -60,42 +60,42 @@ void convertsToXmir() throws ImpossibleModificationException { void determinesPrimitiveTypesCorrectly() { MatcherAssert.assertThat( "Can't determine the type of Add with two integer literals", - new Add(new Literal(1), new Literal(2)).type(), + new Addition(new Literal(1), new Literal(2)).type(), Matchers.equalTo(Type.INT_TYPE) ); MatcherAssert.assertThat( "Can't determine the type of Add with two long literals", - new Add(new Literal(1L), new Literal(2L)).type(), + new Addition(new Literal(1L), new Literal(2L)).type(), Matchers.equalTo(Type.LONG_TYPE) ); MatcherAssert.assertThat( "Can't determine the type of Add with two float literals", - new Add(new Literal(1.0f), new Literal(2.0f)).type(), + new Addition(new Literal(1.0f), new Literal(2.0f)).type(), Matchers.equalTo(Type.FLOAT_TYPE) ); MatcherAssert.assertThat( "Can't determine the type of Add with two double literals", - new Add(new Literal(1.0), new Literal(2.0)).type(), + new Addition(new Literal(1.0), new Literal(2.0)).type(), Matchers.equalTo(Type.DOUBLE_TYPE) ); MatcherAssert.assertThat( "Can't determine the type of Add with two integer and long literals", - new Add(new Literal(1), new Literal(2L)).type(), + new Addition(new Literal(1), new Literal(2L)).type(), Matchers.equalTo(Type.LONG_TYPE) ); MatcherAssert.assertThat( "Can't determine the type of Add with two integer and float literals", - new Add(new Literal(1), new Literal(2.0f)).type(), + new Addition(new Literal(1), new Literal(2.0f)).type(), Matchers.equalTo(Type.FLOAT_TYPE) ); MatcherAssert.assertThat( "Can't determine the type of Add with two integer and double literals", - new Add(new Literal(1), new Literal(2.0)).type(), + new Addition(new Literal(1), new Literal(2.0)).type(), Matchers.equalTo(Type.DOUBLE_TYPE) ); MatcherAssert.assertThat( "Can't determine the type of Add with two long and float literals", - new Add(new Literal(1L), new Literal(2.0f)).type(), + new Addition(new Literal(1L), new Literal(2.0f)).type(), Matchers.equalTo(Type.FLOAT_TYPE) ); } @@ -105,7 +105,7 @@ void retrievesOpcodesWithLeftAndRightNodesWithTheSameType() { MatcherAssert.assertThat( "Can't retrieve opcodes from Add with two literals", new OpcodeNodes( - new Add( + new Addition( new Literal(1), new Literal(2) ) @@ -123,7 +123,7 @@ void retrievesOpcodesWithLeftAndRightNodesWithDifferentTypes() { MatcherAssert.assertThat( "Can't retrieve opcodes from Add with two literals of different types", new OpcodeNodes( - new Add( + new Addition( new Literal(1L), new Literal(1) ) @@ -141,7 +141,7 @@ void retrievesOpcodesWithLeftAndRightNodesWithDoubleType() { MatcherAssert.assertThat( "Can't retrieve opcodes from Add with where one of the operands is double", new OpcodeNodes( - new Add( + new Addition( new Literal(1.0), new Literal(1) ) diff --git a/src/test/java/org/eolang/opeo/ast/ArrayConstructorTest.java b/src/test/java/org/eolang/opeo/ast/ArrayConstructorTest.java index c9f39145..dfbaa515 100644 --- a/src/test/java/org/eolang/opeo/ast/ArrayConstructorTest.java +++ b/src/test/java/org/eolang/opeo/ast/ArrayConstructorTest.java @@ -59,10 +59,10 @@ void createsArrayConstructorFromXmir() { "Can't create array constructor from XMIR", new ArrayConstructor( new XmlNode(ArrayConstructorTest.XMIR), - node -> new Add(new Literal(1), new Literal(2)) + node -> new Addition(new Literal(1), new Literal(2)) ), Matchers.equalTo( - new ArrayConstructor(new Add(new Literal(1), new Literal(2)), "java/lang/Integer") + new ArrayConstructor(new Addition(new Literal(1), new Literal(2)), "java/lang/Integer") ) ); } @@ -90,7 +90,7 @@ void compilesArrayWithComplexLength() { "Can't compile array constructor with complex undefined length", new OpcodeNodes( new ArrayConstructor( - new Add(new Literal(1), new Literal(2)), + new Addition(new Literal(1), new Literal(2)), type ) ).opcodes(), @@ -109,7 +109,7 @@ void convertsToXmir() throws ImpossibleModificationException { final String type = "java/lang/Integer"; final String xmir = new Xembler( new ArrayConstructor( - new Add(new Literal(1), new Literal(2)), + new Addition(new Literal(1), new Literal(2)), type ).toXmir() ).xml(); diff --git a/src/test/java/org/eolang/opeo/compilation/XmirParserTest.java b/src/test/java/org/eolang/opeo/compilation/XmirParserTest.java index db8f67d3..9d46d8e3 100644 --- a/src/test/java/org/eolang/opeo/compilation/XmirParserTest.java +++ b/src/test/java/org/eolang/opeo/compilation/XmirParserTest.java @@ -27,7 +27,7 @@ import java.util.List; import org.eolang.jeo.matchers.SameXml; import org.eolang.jeo.representation.xmir.XmlNode; -import org.eolang.opeo.ast.Add; +import org.eolang.opeo.ast.Addition; import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.Field; import org.eolang.opeo.ast.FieldAssignment; @@ -70,7 +70,7 @@ void convertsOpcodesAsIs() { @Test void convertsAddition() { final List nodes = new XmirParser( - new Add(new Literal(1), new Literal(2)) + new Addition(new Literal(1), new Literal(2)) ).toJeoNodes(); MatcherAssert.assertThat( String.format( @@ -96,12 +96,12 @@ void convertsDeepAddition() { MatcherAssert.assertThat( "We expect to retrieve 7 opcodes, but got something else instead", new XmirParser( - new Add( - new Add( + new Addition( + new Addition( new Literal(1), new Literal(2) ), - new Add( + new Addition( new Literal(3), new Literal(4) ) diff --git a/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java b/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java index e24ffeb7..5c253d2b 100644 --- a/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java +++ b/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java @@ -29,7 +29,7 @@ import org.eolang.jeo.representation.xmir.AllLabels; import org.eolang.opeo.LabelInstruction; import org.eolang.opeo.OpcodeInstruction; -import org.eolang.opeo.ast.Add; +import org.eolang.opeo.ast.Addition; import org.eolang.opeo.ast.ArrayConstructor; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Attributes; @@ -258,7 +258,7 @@ void decompilesArrayCreation() throws ImpossibleModificationException { new Xembler( new Root( new ArrayConstructor( - new Add(new Literal(2), new Literal(3)), + new Addition(new Literal(2), new Literal(3)), type ) ).toXmir() @@ -317,7 +317,7 @@ void decompilesVariableAssignment() { ), new VariableAssignment( new LocalVariable(2, Type.INT_TYPE), - new Add( + new Addition( new LocalVariable(2, Type.INT_TYPE), new Literal(2) ) From 3d85e72b0543ddc5add8944c8b2ec815ab6c93aa Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 12 Aug 2024 11:08:46 +0300 Subject: [PATCH 02/11] feat(#376) handlers -> agents --- .../org/eolang/opeo/SelectiveDecompiler.java | 8 +- .../java/org/eolang/opeo/ast/Labeled.java | 3 +- .../opeo/compilation/SelectiveCompiler.java | 4 +- ...onHandler.java => DecompilationAgent.java} | 8 +- .../opeo/decompilation/DecompilerMachine.java | 6 +- .../AddHandler.java => agents/AddAgent.java} | 6 +- .../BipushAgent.java} | 6 +- .../CastAgent.java} | 8 +- .../CheckCastAgent.java} | 6 +- .../ConstAgent.java} | 16 +- .../DupHandler.java => agents/DupAgent.java} | 6 +- .../GetFieldAgent.java} | 6 +- .../GetStaticAgent.java} | 6 +- .../IfHandler.java => agents/IfAgent.java} | 6 +- .../InvokedynamicAgent.java} | 6 +- .../InvokeinterfaceAgent.java} | 6 +- .../InvokespecialAgent.java} | 14 +- .../InvokestaticAgent.java} | 6 +- .../InvokevirtualAgent.java} | 6 +- .../LabelAgent.java} | 6 +- .../LdcHandler.java => agents/LdcAgent.java} | 6 +- .../LoadAgent.java} | 8 +- .../MulHandler.java => agents/MulAgent.java} | 8 +- .../NewHandler.java => agents/NewAgent.java} | 6 +- .../NewArrayAgent.java} | 6 +- .../PopHandler.java => agents/PopAgent.java} | 6 +- .../PutFieldAgent.java} | 6 +- .../ReturnAgent.java} | 6 +- .../decompilation/agents/RouterAgent.java | 200 ++++++++++++++++++ .../StoreAgent.java} | 8 +- .../StoreToArrayAgent.java} | 8 +- .../SubstractionAgent.java} | 6 +- .../{handlers => agents}/package-info.java | 2 +- .../decompilation/handlers/RouterHandler.java | 200 ------------------ .../SelectiveDecompilerTest.java | 4 +- 35 files changed, 310 insertions(+), 309 deletions(-) rename src/main/java/org/eolang/opeo/decompilation/{InstructionHandler.java => DecompilationAgent.java} (83%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/AddHandler.java => agents/AddAgent.java} (90%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/BipushHandler.java => agents/BipushAgent.java} (89%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/CastHandler.java => agents/CastAgent.java} (89%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/CheckCastHandler.java => agents/CheckCastAgent.java} (90%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/ConstHandler.java => agents/ConstAgent.java} (93%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/DupHandler.java => agents/DupAgent.java} (90%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/GetFieldHandler.java => agents/GetFieldAgent.java} (91%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/GetStaticHnadler.java => agents/GetStaticAgent.java} (90%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/IfHandler.java => agents/IfAgent.java} (93%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/InvokedynamicHandler.java => agents/InvokedynamicAgent.java} (92%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/InvokeinterfaceHandler.java => agents/InvokeinterfaceAgent.java} (93%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/InvokespecialHandler.java => agents/InvokespecialAgent.java} (92%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/InvokestaticHander.java => agents/InvokestaticAgent.java} (92%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/InvokevirtualHandler.java => agents/InvokevirtualAgent.java} (93%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/LabelHandler.java => agents/LabelAgent.java} (90%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/LdcHandler.java => agents/LdcAgent.java} (89%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/LoadHandler.java => agents/LoadAgent.java} (90%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/MulHandler.java => agents/MulAgent.java} (91%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/NewHandler.java => agents/NewAgent.java} (89%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/NewArrayHandler.java => agents/NewArrayAgent.java} (90%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/PopHandler.java => agents/PopAgent.java} (90%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/PutFieldHnadler.java => agents/PutFieldAgent.java} (92%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/ReturnHandler.java => agents/ReturnAgent.java} (93%) create mode 100644 src/main/java/org/eolang/opeo/decompilation/agents/RouterAgent.java rename src/main/java/org/eolang/opeo/decompilation/{handlers/StoreHandler.java => agents/StoreAgent.java} (91%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/StoreToArrayHandler.java => agents/StoreToArrayAgent.java} (93%) rename src/main/java/org/eolang/opeo/decompilation/{handlers/SubstractionHandler.java => agents/SubstractionAgent.java} (90%) rename src/main/java/org/eolang/opeo/decompilation/{handlers => agents}/package-info.java (96%) delete mode 100644 src/main/java/org/eolang/opeo/decompilation/handlers/RouterHandler.java diff --git a/src/main/java/org/eolang/opeo/SelectiveDecompiler.java b/src/main/java/org/eolang/opeo/SelectiveDecompiler.java index 8f3d1400..b366304c 100644 --- a/src/main/java/org/eolang/opeo/SelectiveDecompiler.java +++ b/src/main/java/org/eolang/opeo/SelectiveDecompiler.java @@ -31,7 +31,7 @@ import java.util.stream.Collectors; import org.eolang.opeo.decompilation.Decompiler; import org.eolang.opeo.decompilation.WithoutAliases; -import org.eolang.opeo.decompilation.handlers.RouterHandler; +import org.eolang.opeo.decompilation.agents.RouterAgent; import org.eolang.opeo.jeo.JeoDecompiler; import org.eolang.opeo.storage.FileStorage; import org.eolang.opeo.storage.Storage; @@ -41,7 +41,7 @@ * Selective decompiler. * Decompiler that decompiles ONLY fully understandable methods. * These methods contain only instructions that are - * supported by {@link org.eolang.opeo.decompilation.handlers.RouterHandler}. + * supported by {@link RouterAgent}. * * @since 0.1 */ @@ -69,7 +69,7 @@ public final class SelectiveDecompiler implements Decompiler { * @param modified Folder where to save the modified XMIRs. */ public SelectiveDecompiler(final Path input, final Path output, final Path modified) { - this(input, output, modified, new RouterHandler(false).supportedOpcodes()); + this(input, output, modified, new RouterAgent(false).supportedOpcodes()); } /** @@ -97,7 +97,7 @@ public SelectiveDecompiler( public SelectiveDecompiler( final Storage storage, final Storage modified ) { - this(storage, modified, new RouterHandler(false).supportedOpcodes()); + this(storage, modified, new RouterAgent(false).supportedOpcodes()); } /** diff --git a/src/main/java/org/eolang/opeo/ast/Labeled.java b/src/main/java/org/eolang/opeo/ast/Labeled.java index 7e039e66..cb7bc593 100644 --- a/src/main/java/org/eolang/opeo/ast/Labeled.java +++ b/src/main/java/org/eolang/opeo/ast/Labeled.java @@ -30,6 +30,7 @@ import lombok.EqualsAndHashCode; import lombok.ToString; import org.eolang.jeo.representation.xmir.XmlNode; +import org.eolang.opeo.decompilation.agents.InvokespecialAgent; import org.objectweb.asm.Type; import org.xembly.Directive; import org.xembly.Directives; @@ -39,7 +40,7 @@ * This class needed to avoid considering labels as separate nodes. * Maybe it's wrong to do so, but it's easier to implement this way, at least for now. * Pay attention, that {@link Labeled} class violates class hierarchy. - * It is the most visible within {@link org.eolang.opeo.decompilation.handlers.InvokespecialHandler} + * It is the most visible within {@link InvokespecialAgent} * implementation. * @since 0.2 */ diff --git a/src/main/java/org/eolang/opeo/compilation/SelectiveCompiler.java b/src/main/java/org/eolang/opeo/compilation/SelectiveCompiler.java index e163470d..3855d156 100644 --- a/src/main/java/org/eolang/opeo/compilation/SelectiveCompiler.java +++ b/src/main/java/org/eolang/opeo/compilation/SelectiveCompiler.java @@ -27,7 +27,7 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.stream.Collectors; -import org.eolang.opeo.decompilation.handlers.RouterHandler; +import org.eolang.opeo.decompilation.agents.RouterAgent; import org.eolang.opeo.storage.CompilationStorage; import org.eolang.opeo.storage.Storage; import org.eolang.opeo.storage.XmirEntry; @@ -65,7 +65,7 @@ public SelectiveCompiler(final Path xmirs, final Path output) { */ public SelectiveCompiler(final Storage storage) { this.storage = storage; - this.supported = new RouterHandler(false).supportedOpcodes(); + this.supported = new RouterAgent(false).supportedOpcodes(); } @Override diff --git a/src/main/java/org/eolang/opeo/decompilation/InstructionHandler.java b/src/main/java/org/eolang/opeo/decompilation/DecompilationAgent.java similarity index 83% rename from src/main/java/org/eolang/opeo/decompilation/InstructionHandler.java rename to src/main/java/org/eolang/opeo/decompilation/DecompilationAgent.java index 43cd23d5..ed253708 100644 --- a/src/main/java/org/eolang/opeo/decompilation/InstructionHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/DecompilationAgent.java @@ -24,15 +24,15 @@ package org.eolang.opeo.decompilation; /** - * Instruction handler. + * An agent that tries to understand the current decompilation state and apply some changes to it. * @since 0.1 */ @FunctionalInterface -public interface InstructionHandler { +public interface DecompilationAgent { /** - * Handle instruction. - * @param state Current instruction to handle together with operand stack and variables. + * Handle the current state. + * @param state Current state to handle together with operand stack and variables. */ void handle(DecompilerState state); } diff --git a/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java b/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java index 79c91dbb..bc5cb600 100644 --- a/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java +++ b/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java @@ -29,7 +29,7 @@ import org.cactoos.list.ListOf; import org.eolang.opeo.Instruction; import org.eolang.opeo.ast.Root; -import org.eolang.opeo.decompilation.handlers.RouterHandler; +import org.eolang.opeo.decompilation.agents.RouterAgent; import org.xembly.Directive; /** @@ -47,7 +47,7 @@ public final class DecompilerMachine { /** * Handler that redirects instructions. */ - private final RouterHandler router; + private final RouterAgent router; /** * Constructor. @@ -73,7 +73,7 @@ public final class DecompilerMachine { */ public DecompilerMachine(final LocalVariables locals, final Map arguments) { this.locals = locals; - this.router = new RouterHandler( + this.router = new RouterAgent( "true".equals(arguments.getOrDefault("counting", "true")) ); } diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/AddHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java similarity index 90% rename from src/main/java/org/eolang/opeo/decompilation/handlers/AddHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java index 006b52c9..248cbcd2 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/AddHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java @@ -21,18 +21,18 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.Addition; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; /** * Add instruction handler. * @since 0.1 */ -public final class AddHandler implements InstructionHandler { +public final class AddAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/BipushHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java similarity index 89% rename from src/main/java/org/eolang/opeo/decompilation/handlers/BipushHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java index e7a46414..3a9f62b0 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/BipushHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java @@ -21,17 +21,17 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.Literal; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; /** * Bipush instruction handler. * @since 0.1 */ -public final class BipushHandler implements InstructionHandler { +public final class BipushAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/CastHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java similarity index 89% rename from src/main/java/org/eolang/opeo/decompilation/handlers/CastHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java index d151c433..6c5228ad 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/CastHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java @@ -21,18 +21,18 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.Cast; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Type; /** * Cast instruction handler. * @since 0.2 */ -public final class CastHandler implements InstructionHandler { +public final class CastAgent implements DecompilationAgent { /** * Target type. @@ -43,7 +43,7 @@ public final class CastHandler implements InstructionHandler { * Constructor. * @param target Target type */ - CastHandler(final Type target) { + CastAgent(final Type target) { this.target = target; } diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/CheckCastHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java similarity index 90% rename from src/main/java/org/eolang/opeo/decompilation/handlers/CheckCastHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java index 65f439e1..5d7cf152 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/CheckCastHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java @@ -21,12 +21,12 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.CheckCast; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Type; /** @@ -34,7 +34,7 @@ * * @since 0.5 */ -public final class CheckCastHandler implements InstructionHandler { +public final class CheckCastAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/ConstHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java similarity index 93% rename from src/main/java/org/eolang/opeo/decompilation/handlers/ConstHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java index ecc40e89..2d1f42db 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/ConstHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java @@ -21,12 +21,12 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Literal; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; @@ -34,7 +34,7 @@ * Iconst instruction handler. * @since 0.1 */ -public final class ConstHandler implements InstructionHandler { +public final class ConstAgent implements DecompilationAgent { /** * Type of constant. @@ -45,7 +45,7 @@ public final class ConstHandler implements InstructionHandler { * Constructor. * @param type Type of constant */ - ConstHandler(final Type type) { + ConstAgent(final Type type) { this.type = type; } @@ -54,13 +54,13 @@ public void handle(final DecompilerState state) { final int opcode = state.instruction().opcode(); final AstNode res; if (this.type.equals(Type.INT_TYPE)) { - res = ConstHandler.intConstant(opcode); + res = ConstAgent.intConstant(opcode); } else if (this.type.equals(Type.LONG_TYPE)) { - res = ConstHandler.longConstant(opcode); + res = ConstAgent.longConstant(opcode); } else if (this.type.equals(Type.FLOAT_TYPE)) { - res = ConstHandler.floatConstant(opcode); + res = ConstAgent.floatConstant(opcode); } else if (this.type.equals(Type.DOUBLE_TYPE)) { - res = ConstHandler.doubleConstant(opcode); + res = ConstAgent.doubleConstant(opcode); } else { throw new UnsupportedOperationException( String.format("Type %s is not supported yet", this.type) diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/DupHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java similarity index 90% rename from src/main/java/org/eolang/opeo/decompilation/handlers/DupHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java index b9d681c3..3259bf98 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/DupHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java @@ -21,18 +21,18 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.Duplicate; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.OperandStack; /** * Dup instruction handler. * @since 0.1 */ -public final class DupHandler implements InstructionHandler { +public final class DupAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/GetFieldHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java similarity index 91% rename from src/main/java/org/eolang/opeo/decompilation/handlers/GetFieldHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java index 3f2544f2..0d98afef 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/GetFieldHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java @@ -21,18 +21,18 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.FieldRetrieval; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; /** * Getfield instruction handler. * @since 0.1 */ -public final class GetFieldHandler implements InstructionHandler { +public final class GetFieldAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/GetStaticHnadler.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java similarity index 90% rename from src/main/java/org/eolang/opeo/decompilation/handlers/GetStaticHnadler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java index 329a832d..ba1712bb 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/GetStaticHnadler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java @@ -21,17 +21,17 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.ClassField; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; /** * Getstatic instruction handler. * @since 0.1 */ -public final class GetStaticHnadler implements InstructionHandler { +public final class GetStaticAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { final String klass = (String) state.operand(0); diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/IfHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java similarity index 93% rename from src/main/java/org/eolang/opeo/decompilation/handlers/IfHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java index edf49385..2f0dbdcc 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/IfHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java @@ -21,12 +21,12 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.If; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.OperandStack; import org.objectweb.asm.Label; import org.objectweb.asm.Opcodes; @@ -38,7 +38,7 @@ * (signed short constructed from unsigned bytes branchbyte1 << 8 | branchbyte2) * @since 0.2 */ -public final class IfHandler implements InstructionHandler { +public final class IfAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/InvokedynamicHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java similarity index 92% rename from src/main/java/org/eolang/opeo/decompilation/handlers/InvokedynamicHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java index 81e2c8c2..4e055876 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/InvokedynamicHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java @@ -21,14 +21,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import java.util.Collections; import java.util.List; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.DynamicInvocation; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Handle; import org.objectweb.asm.Type; @@ -36,7 +36,7 @@ * Invokedynamic instruction handler. * @since 0.5 */ -public final class InvokedynamicHandler implements InstructionHandler { +public final class InvokedynamicAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/InvokeinterfaceHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java similarity index 93% rename from src/main/java/org/eolang/opeo/decompilation/handlers/InvokeinterfaceHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java index bd29b549..d8fd1d36 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/InvokeinterfaceHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import java.util.Collections; import java.util.List; @@ -29,7 +29,7 @@ import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.InterfaceInvocation; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Type; /** @@ -48,7 +48,7 @@ *

* @since 0.2 */ -public final class InvokeinterfaceHandler implements InstructionHandler { +public final class InvokeinterfaceAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { final String owner = (String) state.operand(0); diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/InvokespecialHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java similarity index 92% rename from src/main/java/org/eolang/opeo/decompilation/handlers/InvokespecialHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java index d85e423e..977dbcc4 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/InvokespecialHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import java.util.Collections; import java.util.List; @@ -34,7 +34,7 @@ import org.eolang.opeo.ast.Super; import org.eolang.opeo.ast.This; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Type; /** @@ -42,14 +42,14 @@ * * @since 0.1 * @todo #229:90min Is Labeled Class an Abstraction Failure? - * As you can see in {@link InvokespecialHandler} we use many 'instanceof' checks. + * As you can see in {@link InvokespecialAgent} we use many 'instanceof' checks. * This is a clear sign that the class hierarchy is not well designed. * The original problem lies in the {@link Labeled} class. * We need to find more elegant solutions to handle labels in AST. * @checkstyle NoJavadocForOverriddenMethodsCheck (500 lines) * @checkstyle MultilineJavadocTagsCheck (500 lines) */ -public final class InvokespecialHandler implements InstructionHandler { +public final class InvokespecialAgent implements DecompilationAgent { /** * Handle invokespecial instruction. @@ -84,7 +84,7 @@ public void handle(final DecompilerState state) { ); Collections.reverse(args); final AstNode target = state.stack().pop(); - if (InvokespecialHandler.isThis(target)) { + if (InvokespecialAgent.isThis(target)) { state.stack().push( new Super(target, args, descriptor, type, name) ); @@ -135,9 +135,9 @@ private static boolean isThis(final AstNode candidate) { if (candidate instanceof This) { result = true; } else if (candidate instanceof Labeled) { - result = InvokespecialHandler.isThis(((Labeled) candidate).origin()); + result = InvokespecialAgent.isThis(((Labeled) candidate).origin()); } else if (candidate instanceof Duplicate) { - result = InvokespecialHandler.isThis(((Duplicate) candidate).origin()); + result = InvokespecialAgent.isThis(((Duplicate) candidate).origin()); } else { result = false; } diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/InvokestaticHander.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java similarity index 92% rename from src/main/java/org/eolang/opeo/decompilation/handlers/InvokestaticHander.java rename to src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java index 00a8ced5..654c700f 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/InvokestaticHander.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import java.util.Collections; import java.util.List; @@ -30,14 +30,14 @@ import org.eolang.opeo.ast.Owner; import org.eolang.opeo.ast.StaticInvocation; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Type; /** * Invokestatic instruction handler. * @since 0.1 */ -public final class InvokestaticHander implements InstructionHandler { +public final class InvokestaticAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/InvokevirtualHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java similarity index 93% rename from src/main/java/org/eolang/opeo/decompilation/handlers/InvokevirtualHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java index 31fa25fa..7e937de3 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/InvokevirtualHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import java.util.Collections; import java.util.List; @@ -29,7 +29,7 @@ import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.Invocation; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Type; /** @@ -47,7 +47,7 @@ *

* @since 0.1 */ -public final class InvokevirtualHandler implements InstructionHandler { +public final class InvokevirtualAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/LabelHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java similarity index 90% rename from src/main/java/org/eolang/opeo/decompilation/handlers/LabelHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java index 5be17b5a..3d10b406 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/LabelHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java @@ -21,19 +21,19 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Label; import org.eolang.opeo.ast.Labeled; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; /** * Label instruction handler. * @since 0.1 */ -public final class LabelHandler implements InstructionHandler { +public final class LabelAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { final Labeled node = new Labeled( diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/LdcHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java similarity index 89% rename from src/main/java/org/eolang/opeo/decompilation/handlers/LdcHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java index 479e2a06..01861883 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/LdcHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java @@ -21,17 +21,17 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.Constant; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; /** * Ldc instruction handler. * @since 0.1 */ -public final class LdcHandler implements InstructionHandler { +public final class LdcAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/LoadHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java similarity index 90% rename from src/main/java/org/eolang/opeo/decompilation/handlers/LoadHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java index 1703958c..5c8e49d3 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/LoadHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java @@ -21,10 +21,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Type; /** @@ -35,7 +35,7 @@ * ... * @since 0.1 */ -public final class LoadHandler implements InstructionHandler { +public final class LoadAgent implements DecompilationAgent { /** * Type of the variable. @@ -46,7 +46,7 @@ public final class LoadHandler implements InstructionHandler { * Constructor. * @param type Type of the variable. */ - public LoadHandler(final Type type) { + public LoadAgent(final Type type) { this.type = type; } diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/MulHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java similarity index 91% rename from src/main/java/org/eolang/opeo/decompilation/handlers/MulHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java index 280bb6f4..dd2c487f 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/MulHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java @@ -21,20 +21,20 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Multiplication; import org.eolang.opeo.ast.Opcode; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Opcodes; /** * Mul instruction handler. * @since 0.1 */ -public final class MulHandler implements InstructionHandler { +public final class MulAgent implements DecompilationAgent { /** * Do we put numbers to opcodes? @@ -45,7 +45,7 @@ public final class MulHandler implements InstructionHandler { * Constructor. * @param counting Do we put numbers to opcodes? */ - MulHandler(final boolean counting) { + MulAgent(final boolean counting) { this.counting = counting; } diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/NewHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java similarity index 89% rename from src/main/java/org/eolang/opeo/decompilation/handlers/NewHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java index 9cf78c66..544f5ce2 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/NewHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java @@ -21,17 +21,17 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.NewAddress; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; /** * New instruction handler. * @since 0.1 */ -public final class NewHandler implements InstructionHandler { +public final class NewAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/NewArrayHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java similarity index 90% rename from src/main/java/org/eolang/opeo/decompilation/handlers/NewArrayHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java index e6d5ab31..bc767e3e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/NewArrayHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java @@ -21,20 +21,20 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.ArrayConstructor; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Reference; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.OperandStack; /** * New array instruction handler. * @since 0.1 */ -public final class NewArrayHandler implements InstructionHandler { +public final class NewArrayAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/PopHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java similarity index 90% rename from src/main/java/org/eolang/opeo/decompilation/handlers/PopHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java index cb114de5..97cf73a7 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/PopHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java @@ -21,18 +21,18 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.Popped; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.OperandStack; /** * Pop instruction handler. * @since 0.1 */ -public final class PopHandler implements InstructionHandler { +public final class PopAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/PutFieldHnadler.java b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java similarity index 92% rename from src/main/java/org/eolang/opeo/decompilation/handlers/PutFieldHnadler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java index b89060b3..2849d3a5 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/PutFieldHnadler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java @@ -21,21 +21,21 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.Field; import org.eolang.opeo.ast.FieldAssignment; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; /** * Putfield instruction handler. * Stack [before]->[after]: "objectref, value →" * @since 0.1 */ -public final class PutFieldHnadler implements InstructionHandler { +public final class PutFieldAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/ReturnHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java similarity index 93% rename from src/main/java/org/eolang/opeo/decompilation/handlers/ReturnHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java index fa3d7f00..eb56509d 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/ReturnHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java @@ -21,11 +21,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.Return; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.OperandStack; import org.objectweb.asm.Opcodes; @@ -34,7 +34,7 @@ * @since 0.1 * @checkstyle ClassFanOutComplexityCheck (500 lines) */ -public final class ReturnHandler implements InstructionHandler { +public final class ReturnAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/RouterAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/RouterAgent.java new file mode 100644 index 00000000..c57090a1 --- /dev/null +++ b/src/main/java/org/eolang/opeo/decompilation/agents/RouterAgent.java @@ -0,0 +1,200 @@ +/* + * 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.opeo.decompilation.agents; + +import java.util.Map; +import org.cactoos.map.MapEntry; +import org.cactoos.map.MapOf; +import org.eolang.opeo.LabelInstruction; +import org.eolang.opeo.ast.Opcode; +import org.eolang.opeo.ast.OpcodeName; +import org.eolang.opeo.decompilation.DecompilerState; +import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; + +/** + * General Instruction Handler. + * This handler redirects handling of instructions depending on an incoming instruction. + * @since 0.2 + * @checkstyle ClassFanOutComplexityCheck (500 lines) + */ +public final class RouterAgent implements DecompilationAgent { + + /** + * Index of unimplemented handler. + */ + private static final int UNIMPLEMENTED = -1; + + /** + * ALl instruction handlers. + */ + private final Map handlers; + + /** + * Constructor. + * @param counting Do we put numbers to opcodes? + */ + public RouterAgent(final boolean counting) { + this( + new MapOf( + new MapEntry<>(Opcodes.ICONST_M1, new ConstAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.ICONST_0, new ConstAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.ICONST_1, new ConstAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.ICONST_2, new ConstAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.ICONST_3, new ConstAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.ICONST_4, new ConstAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.ICONST_5, new ConstAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.LCONST_0, new ConstAgent(Type.LONG_TYPE)), + new MapEntry<>(Opcodes.LCONST_1, new ConstAgent(Type.LONG_TYPE)), + new MapEntry<>(Opcodes.FCONST_0, new ConstAgent(Type.FLOAT_TYPE)), + new MapEntry<>(Opcodes.FCONST_1, new ConstAgent(Type.FLOAT_TYPE)), + new MapEntry<>(Opcodes.FCONST_2, new ConstAgent(Type.FLOAT_TYPE)), + new MapEntry<>(Opcodes.DCONST_0, new ConstAgent(Type.DOUBLE_TYPE)), + new MapEntry<>(Opcodes.DCONST_1, new ConstAgent(Type.DOUBLE_TYPE)), + new MapEntry<>(Opcodes.IADD, new AddAgent()), + new MapEntry<>(Opcodes.LADD, new AddAgent()), + new MapEntry<>(Opcodes.FADD, new AddAgent()), + new MapEntry<>(Opcodes.DADD, new AddAgent()), + new MapEntry<>(Opcodes.ISUB, new SubstractionAgent()), + new MapEntry<>(Opcodes.LSUB, new SubstractionAgent()), + new MapEntry<>(Opcodes.FSUB, new SubstractionAgent()), + new MapEntry<>(Opcodes.DSUB, new SubstractionAgent()), + new MapEntry<>(Opcodes.IMUL, new MulAgent(counting)), + new MapEntry<>(Opcodes.IF_ICMPGT, new IfAgent()), + new MapEntry<>(Opcodes.I2B, new CastAgent(Type.BYTE_TYPE)), + new MapEntry<>(Opcodes.I2C, new CastAgent(Type.CHAR_TYPE)), + new MapEntry<>(Opcodes.I2S, new CastAgent(Type.SHORT_TYPE)), + new MapEntry<>(Opcodes.I2L, new CastAgent(Type.LONG_TYPE)), + new MapEntry<>(Opcodes.I2F, new CastAgent(Type.FLOAT_TYPE)), + new MapEntry<>(Opcodes.I2D, new CastAgent(Type.DOUBLE_TYPE)), + new MapEntry<>(Opcodes.L2I, new CastAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.L2F, new CastAgent(Type.FLOAT_TYPE)), + new MapEntry<>(Opcodes.L2D, new CastAgent(Type.DOUBLE_TYPE)), + new MapEntry<>(Opcodes.F2I, new CastAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.F2L, new CastAgent(Type.LONG_TYPE)), + new MapEntry<>(Opcodes.F2D, new CastAgent(Type.DOUBLE_TYPE)), + new MapEntry<>(Opcodes.D2I, new CastAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.D2L, new CastAgent(Type.LONG_TYPE)), + new MapEntry<>(Opcodes.D2F, new CastAgent(Type.FLOAT_TYPE)), + new MapEntry<>(Opcodes.ILOAD, new LoadAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.LLOAD, new LoadAgent(Type.LONG_TYPE)), + new MapEntry<>(Opcodes.FLOAD, new LoadAgent(Type.FLOAT_TYPE)), + new MapEntry<>(Opcodes.DLOAD, new LoadAgent(Type.DOUBLE_TYPE)), + new MapEntry<>(Opcodes.ALOAD, new LoadAgent(Type.getType(Object.class))), + new MapEntry<>(Opcodes.ISTORE, new StoreAgent(Type.INT_TYPE)), + new MapEntry<>(Opcodes.LSTORE, new StoreAgent(Type.LONG_TYPE)), + new MapEntry<>(Opcodes.FSTORE, new StoreAgent(Type.FLOAT_TYPE)), + new MapEntry<>(Opcodes.DSTORE, new StoreAgent(Type.DOUBLE_TYPE)), + new MapEntry<>(Opcodes.ASTORE, new StoreAgent(Type.getType(Object.class))), + new MapEntry<>(Opcodes.AASTORE, new StoreToArrayAgent()), + new MapEntry<>(Opcodes.ANEWARRAY, new NewArrayAgent()), + new MapEntry<>(Opcodes.CHECKCAST, new CheckCastAgent()), + new MapEntry<>(Opcodes.NEW, new NewAgent()), + new MapEntry<>(Opcodes.DUP, new DupAgent()), + new MapEntry<>(Opcodes.BIPUSH, new BipushAgent()), + new MapEntry<>(Opcodes.INVOKESPECIAL, new InvokespecialAgent()), + new MapEntry<>(Opcodes.INVOKEVIRTUAL, new InvokevirtualAgent()), + new MapEntry<>(Opcodes.INVOKESTATIC, new InvokestaticAgent()), + new MapEntry<>(Opcodes.INVOKEINTERFACE, new InvokeinterfaceAgent()), + new MapEntry<>(Opcodes.INVOKEDYNAMIC, new InvokedynamicAgent()), + new MapEntry<>(Opcodes.GETFIELD, new GetFieldAgent()), + new MapEntry<>(Opcodes.PUTFIELD, new PutFieldAgent()), + new MapEntry<>(Opcodes.GETSTATIC, new GetStaticAgent()), + new MapEntry<>(Opcodes.LDC, new LdcAgent()), + new MapEntry<>(Opcodes.POP, new PopAgent()), + new MapEntry<>(Opcodes.RETURN, new ReturnAgent()), + new MapEntry<>(Opcodes.IRETURN, new ReturnAgent()), + new MapEntry<>(Opcodes.ARETURN, new ReturnAgent()), + new MapEntry<>(LabelInstruction.LABEL_OPCODE, new LabelAgent()), + new MapEntry<>(RouterAgent.UNIMPLEMENTED, new UnimplementedHandler(counting)) + ) + ); + } + + /** + * Constructor. + * @param handlers All handlers that will try to handle incoming instructions. + */ + private RouterAgent(final Map handlers) { + this.handlers = handlers; + } + + @Override + public void handle(final DecompilerState state) { + this.handler(state.instruction().opcode()).handle(state); + } + + /** + * Get supported opcodes. + * @return Supported opcodes. + */ + public String[] supportedOpcodes() { + return this.handlers.keySet() + .stream() + .map(OpcodeName::new) + .map(OpcodeName::simplified) + .toArray(String[]::new); + } + + /** + * Get instruction handler. + * @param opcode Instruction opcode. + * @return Instruction handler. + */ + private DecompilationAgent handler(final int opcode) { + return this.handlers.getOrDefault(opcode, this.handlers.get(RouterAgent.UNIMPLEMENTED)); + } + + /** + * Unimplemented instruction handler. + * @since 0.1 + */ + private static final class UnimplementedHandler implements DecompilationAgent { + + /** + * Do we put numbers to opcodes? + */ + private final boolean counting; + + /** + * Constructor. + * @param counting Flag which decides if we need to count opcodes. + */ + private UnimplementedHandler(final boolean counting) { + this.counting = counting; + } + + @Override + public void handle(final DecompilerState state) { + state.stack().push( + new Opcode( + state.instruction().opcode(), + state.instruction().operands(), + this.counting + ) + ); + } + } +} diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/StoreHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java similarity index 91% rename from src/main/java/org/eolang/opeo/decompilation/handlers/StoreHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java index 59cd86bc..63534e27 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/StoreHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java @@ -21,14 +21,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.LocalVariable; import org.eolang.opeo.ast.Typed; import org.eolang.opeo.ast.VariableAssignment; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.OperandStack; import org.objectweb.asm.Type; @@ -36,7 +36,7 @@ * Store instruction handler. * @since 0.1 */ -public final class StoreHandler implements InstructionHandler { +public final class StoreAgent implements DecompilationAgent { /** * Type of the variable. @@ -47,7 +47,7 @@ public final class StoreHandler implements InstructionHandler { * Constructor. * @param type Type of the variable. */ - public StoreHandler(final Type type) { + public StoreAgent(final Type type) { this.type = type; } diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/StoreToArrayHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java similarity index 93% rename from src/main/java/org/eolang/opeo/decompilation/handlers/StoreToArrayHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java index ec13b8d9..069c111d 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/StoreToArrayHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Duplicate; @@ -30,7 +30,7 @@ import org.eolang.opeo.ast.Reference; import org.eolang.opeo.ast.StoreArray; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; /** * Store to array instruction handler. @@ -38,12 +38,12 @@ * Opcodes: aastore * Stack [before]->[after]: "arrayref, index, value →" * @since 0.1 - * @todo #329:90min Avoid using 'instance of' in {@link StoreToArrayHandler#findRef(AstNode)}. + * @todo #329:90min Avoid using 'instance of' in {@link StoreToArrayAgent#findRef(AstNode)}. * Here we use 'instance of' statement to find a Reference. * The solution related to Reference looks incorrect, in general. * We should invent a proper solution without the use of this statement. */ -public final class StoreToArrayHandler implements InstructionHandler { +public final class StoreToArrayAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/SubstractionHandler.java b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java similarity index 90% rename from src/main/java/org/eolang/opeo/decompilation/handlers/SubstractionHandler.java rename to src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java index f5b66c0d..f3a841cf 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/SubstractionHandler.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java @@ -21,18 +21,18 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Substraction; import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; +import org.eolang.opeo.decompilation.DecompilationAgent; /** * Substraction instruction handler. * @since 0.1 */ -public final class SubstractionHandler implements InstructionHandler { +public final class SubstractionAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/package-info.java b/src/main/java/org/eolang/opeo/decompilation/agents/package-info.java similarity index 96% rename from src/main/java/org/eolang/opeo/decompilation/handlers/package-info.java rename to src/main/java/org/eolang/opeo/decompilation/agents/package-info.java index a520f234..0eaf15e0 100644 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/package-info.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/package-info.java @@ -29,4 +29,4 @@ * It would also be good to generalize some of the handlers. For example, AddHandler might * handle all the types, not only Integer and Long. */ -package org.eolang.opeo.decompilation.handlers; +package org.eolang.opeo.decompilation.agents; diff --git a/src/main/java/org/eolang/opeo/decompilation/handlers/RouterHandler.java b/src/main/java/org/eolang/opeo/decompilation/handlers/RouterHandler.java deleted file mode 100644 index 81e031a1..00000000 --- a/src/main/java/org/eolang/opeo/decompilation/handlers/RouterHandler.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * 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.opeo.decompilation.handlers; - -import java.util.Map; -import org.cactoos.map.MapEntry; -import org.cactoos.map.MapOf; -import org.eolang.opeo.LabelInstruction; -import org.eolang.opeo.ast.Opcode; -import org.eolang.opeo.ast.OpcodeName; -import org.eolang.opeo.decompilation.DecompilerState; -import org.eolang.opeo.decompilation.InstructionHandler; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; - -/** - * General Instruction Handler. - * This handler redirects handling of instructions depending on an incoming instruction. - * @since 0.2 - * @checkstyle ClassFanOutComplexityCheck (500 lines) - */ -public final class RouterHandler implements InstructionHandler { - - /** - * Index of unimplemented handler. - */ - private static final int UNIMPLEMENTED = -1; - - /** - * ALl instruction handlers. - */ - private final Map handlers; - - /** - * Constructor. - * @param counting Do we put numbers to opcodes? - */ - public RouterHandler(final boolean counting) { - this( - new MapOf( - new MapEntry<>(Opcodes.ICONST_M1, new ConstHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_0, new ConstHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_1, new ConstHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_2, new ConstHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_3, new ConstHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_4, new ConstHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_5, new ConstHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.LCONST_0, new ConstHandler(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.LCONST_1, new ConstHandler(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.FCONST_0, new ConstHandler(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.FCONST_1, new ConstHandler(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.FCONST_2, new ConstHandler(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.DCONST_0, new ConstHandler(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.DCONST_1, new ConstHandler(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.IADD, new AddHandler()), - new MapEntry<>(Opcodes.LADD, new AddHandler()), - new MapEntry<>(Opcodes.FADD, new AddHandler()), - new MapEntry<>(Opcodes.DADD, new AddHandler()), - new MapEntry<>(Opcodes.ISUB, new SubstractionHandler()), - new MapEntry<>(Opcodes.LSUB, new SubstractionHandler()), - new MapEntry<>(Opcodes.FSUB, new SubstractionHandler()), - new MapEntry<>(Opcodes.DSUB, new SubstractionHandler()), - new MapEntry<>(Opcodes.IMUL, new MulHandler(counting)), - new MapEntry<>(Opcodes.IF_ICMPGT, new IfHandler()), - new MapEntry<>(Opcodes.I2B, new CastHandler(Type.BYTE_TYPE)), - new MapEntry<>(Opcodes.I2C, new CastHandler(Type.CHAR_TYPE)), - new MapEntry<>(Opcodes.I2S, new CastHandler(Type.SHORT_TYPE)), - new MapEntry<>(Opcodes.I2L, new CastHandler(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.I2F, new CastHandler(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.I2D, new CastHandler(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.L2I, new CastHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.L2F, new CastHandler(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.L2D, new CastHandler(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.F2I, new CastHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.F2L, new CastHandler(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.F2D, new CastHandler(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.D2I, new CastHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.D2L, new CastHandler(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.D2F, new CastHandler(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.ILOAD, new LoadHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.LLOAD, new LoadHandler(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.FLOAD, new LoadHandler(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.DLOAD, new LoadHandler(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.ALOAD, new LoadHandler(Type.getType(Object.class))), - new MapEntry<>(Opcodes.ISTORE, new StoreHandler(Type.INT_TYPE)), - new MapEntry<>(Opcodes.LSTORE, new StoreHandler(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.FSTORE, new StoreHandler(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.DSTORE, new StoreHandler(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.ASTORE, new StoreHandler(Type.getType(Object.class))), - new MapEntry<>(Opcodes.AASTORE, new StoreToArrayHandler()), - new MapEntry<>(Opcodes.ANEWARRAY, new NewArrayHandler()), - new MapEntry<>(Opcodes.CHECKCAST, new CheckCastHandler()), - new MapEntry<>(Opcodes.NEW, new NewHandler()), - new MapEntry<>(Opcodes.DUP, new DupHandler()), - new MapEntry<>(Opcodes.BIPUSH, new BipushHandler()), - new MapEntry<>(Opcodes.INVOKESPECIAL, new InvokespecialHandler()), - new MapEntry<>(Opcodes.INVOKEVIRTUAL, new InvokevirtualHandler()), - new MapEntry<>(Opcodes.INVOKESTATIC, new InvokestaticHander()), - new MapEntry<>(Opcodes.INVOKEINTERFACE, new InvokeinterfaceHandler()), - new MapEntry<>(Opcodes.INVOKEDYNAMIC, new InvokedynamicHandler()), - new MapEntry<>(Opcodes.GETFIELD, new GetFieldHandler()), - new MapEntry<>(Opcodes.PUTFIELD, new PutFieldHnadler()), - new MapEntry<>(Opcodes.GETSTATIC, new GetStaticHnadler()), - new MapEntry<>(Opcodes.LDC, new LdcHandler()), - new MapEntry<>(Opcodes.POP, new PopHandler()), - new MapEntry<>(Opcodes.RETURN, new ReturnHandler()), - new MapEntry<>(Opcodes.IRETURN, new ReturnHandler()), - new MapEntry<>(Opcodes.ARETURN, new ReturnHandler()), - new MapEntry<>(LabelInstruction.LABEL_OPCODE, new LabelHandler()), - new MapEntry<>(RouterHandler.UNIMPLEMENTED, new UnimplementedHandler(counting)) - ) - ); - } - - /** - * Constructor. - * @param handlers All handlers that will try to handle incoming instructions. - */ - private RouterHandler(final Map handlers) { - this.handlers = handlers; - } - - @Override - public void handle(final DecompilerState state) { - this.handler(state.instruction().opcode()).handle(state); - } - - /** - * Get supported opcodes. - * @return Supported opcodes. - */ - public String[] supportedOpcodes() { - return this.handlers.keySet() - .stream() - .map(OpcodeName::new) - .map(OpcodeName::simplified) - .toArray(String[]::new); - } - - /** - * Get instruction handler. - * @param opcode Instruction opcode. - * @return Instruction handler. - */ - private InstructionHandler handler(final int opcode) { - return this.handlers.getOrDefault(opcode, this.handlers.get(RouterHandler.UNIMPLEMENTED)); - } - - /** - * Unimplemented instruction handler. - * @since 0.1 - */ - private static final class UnimplementedHandler implements InstructionHandler { - - /** - * Do we put numbers to opcodes? - */ - private final boolean counting; - - /** - * Constructor. - * @param counting Flag which decides if we need to count opcodes. - */ - private UnimplementedHandler(final boolean counting) { - this.counting = counting; - } - - @Override - public void handle(final DecompilerState state) { - state.stack().push( - new Opcode( - state.instruction().opcode(), - state.instruction().operands(), - this.counting - ) - ); - } - } -} diff --git a/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java b/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java index d00db47b..38325c88 100644 --- a/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java +++ b/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java @@ -26,7 +26,7 @@ import java.util.stream.Collectors; import org.cactoos.io.ResourceOf; import org.eolang.opeo.SelectiveDecompiler; -import org.eolang.opeo.decompilation.handlers.RouterHandler; +import org.eolang.opeo.decompilation.agents.RouterAgent; import org.eolang.opeo.storage.InMemoryStorage; import org.eolang.opeo.storage.XmirEntry; import org.hamcrest.MatcherAssert; @@ -161,7 +161,7 @@ void avoidsDecompileLargeFileWithUnknownDependencies() { void identifiesUnsupportedOpcodes() { MatcherAssert.assertThat( "We expect that the supported opcodes won't contain the 'GOTO' opcode since we don't support it yet.", - new RouterHandler(false).supportedOpcodes(), + new RouterAgent(false).supportedOpcodes(), Matchers.not(Matchers.arrayContaining("GOTO")) ); } From cc7e05baaafee8c8fd2b2b8f6f1d4cdebc6e68f0 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 12 Aug 2024 11:23:24 +0300 Subject: [PATCH 03/11] feat(#376): add several useful functions --- .../org/eolang/opeo/SelectiveDecompiler.java | 8 +++--- .../opeo/compilation/SelectiveCompiler.java | 4 +-- .../opeo/decompilation/DecompilerMachine.java | 8 +++--- .../opeo/decompilation/OperandStack.java | 8 ++++++ .../{RouterAgent.java => AllAgents.java} | 27 +++++++++---------- .../SelectiveDecompilerTest.java | 4 +-- 6 files changed, 33 insertions(+), 26 deletions(-) rename src/main/java/org/eolang/opeo/decompilation/agents/{RouterAgent.java => AllAgents.java} (90%) diff --git a/src/main/java/org/eolang/opeo/SelectiveDecompiler.java b/src/main/java/org/eolang/opeo/SelectiveDecompiler.java index b366304c..7f9dbebc 100644 --- a/src/main/java/org/eolang/opeo/SelectiveDecompiler.java +++ b/src/main/java/org/eolang/opeo/SelectiveDecompiler.java @@ -31,7 +31,7 @@ import java.util.stream.Collectors; import org.eolang.opeo.decompilation.Decompiler; import org.eolang.opeo.decompilation.WithoutAliases; -import org.eolang.opeo.decompilation.agents.RouterAgent; +import org.eolang.opeo.decompilation.agents.AllAgents; import org.eolang.opeo.jeo.JeoDecompiler; import org.eolang.opeo.storage.FileStorage; import org.eolang.opeo.storage.Storage; @@ -41,7 +41,7 @@ * Selective decompiler. * Decompiler that decompiles ONLY fully understandable methods. * These methods contain only instructions that are - * supported by {@link RouterAgent}. + * supported by {@link AllAgents}. * * @since 0.1 */ @@ -69,7 +69,7 @@ public final class SelectiveDecompiler implements Decompiler { * @param modified Folder where to save the modified XMIRs. */ public SelectiveDecompiler(final Path input, final Path output, final Path modified) { - this(input, output, modified, new RouterAgent(false).supportedOpcodes()); + this(input, output, modified, new AllAgents(false).supportedOpcodes()); } /** @@ -97,7 +97,7 @@ public SelectiveDecompiler( public SelectiveDecompiler( final Storage storage, final Storage modified ) { - this(storage, modified, new RouterAgent(false).supportedOpcodes()); + this(storage, modified, new AllAgents(false).supportedOpcodes()); } /** diff --git a/src/main/java/org/eolang/opeo/compilation/SelectiveCompiler.java b/src/main/java/org/eolang/opeo/compilation/SelectiveCompiler.java index 3855d156..dfed49b0 100644 --- a/src/main/java/org/eolang/opeo/compilation/SelectiveCompiler.java +++ b/src/main/java/org/eolang/opeo/compilation/SelectiveCompiler.java @@ -27,7 +27,7 @@ import java.nio.file.Path; import java.util.Arrays; import java.util.stream.Collectors; -import org.eolang.opeo.decompilation.agents.RouterAgent; +import org.eolang.opeo.decompilation.agents.AllAgents; import org.eolang.opeo.storage.CompilationStorage; import org.eolang.opeo.storage.Storage; import org.eolang.opeo.storage.XmirEntry; @@ -65,7 +65,7 @@ public SelectiveCompiler(final Path xmirs, final Path output) { */ public SelectiveCompiler(final Storage storage) { this.storage = storage; - this.supported = new RouterAgent(false).supportedOpcodes(); + this.supported = new AllAgents(false).supportedOpcodes(); } @Override diff --git a/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java b/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java index bc5cb600..18e6a905 100644 --- a/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java +++ b/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java @@ -29,7 +29,7 @@ import org.cactoos.list.ListOf; import org.eolang.opeo.Instruction; import org.eolang.opeo.ast.Root; -import org.eolang.opeo.decompilation.agents.RouterAgent; +import org.eolang.opeo.decompilation.agents.AllAgents; import org.xembly.Directive; /** @@ -47,7 +47,7 @@ public final class DecompilerMachine { /** * Handler that redirects instructions. */ - private final RouterAgent router; + private final AllAgents agents; /** * Constructor. @@ -73,7 +73,7 @@ public final class DecompilerMachine { */ public DecompilerMachine(final LocalVariables locals, final Map arguments) { this.locals = locals; - this.router = new RouterAgent( + this.agents = new AllAgents( "true".equals(arguments.getOrDefault("counting", "true")) ); } @@ -87,7 +87,7 @@ public DecompilerMachine(final LocalVariables locals, final Map public Iterable decompile(final Instruction... instructions) { final DecompilerState state = new DecompilerState(this.locals); Arrays.stream(instructions) - .forEach(inst -> this.router.handle(state.next(inst))); + .forEach(inst -> this.agents.handle(state.next(inst))); return new Root(new ListOf<>(state.stack().descendingIterator())).toXmir(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/OperandStack.java b/src/main/java/org/eolang/opeo/decompilation/OperandStack.java index f5d90d72..b96d9455 100644 --- a/src/main/java/org/eolang/opeo/decompilation/OperandStack.java +++ b/src/main/java/org/eolang/opeo/decompilation/OperandStack.java @@ -108,6 +108,14 @@ public List pop(final int number) { return args; } + /** + * Peek the higher value on the stack. + * @return Node. + */ + public AstNode peek() { + return this.stack.peek(); + } + /** * Push one more node to the stack. * @param node Node to add to the stack. diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/RouterAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java similarity index 90% rename from src/main/java/org/eolang/opeo/decompilation/agents/RouterAgent.java rename to src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java index c57090a1..98ca7109 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/RouterAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java @@ -35,15 +35,14 @@ import org.objectweb.asm.Type; /** - * General Instruction Handler. - * This handler redirects handling of instructions depending on an incoming instruction. + * All agents that try to decompile incoming instructions. * @since 0.2 * @checkstyle ClassFanOutComplexityCheck (500 lines) */ -public final class RouterAgent implements DecompilationAgent { +public final class AllAgents implements DecompilationAgent { /** - * Index of unimplemented handler. + * Index of unimplemented agent. */ private static final int UNIMPLEMENTED = -1; @@ -56,7 +55,7 @@ public final class RouterAgent implements DecompilationAgent { * Constructor. * @param counting Do we put numbers to opcodes? */ - public RouterAgent(final boolean counting) { + public AllAgents(final boolean counting) { this( new MapOf( new MapEntry<>(Opcodes.ICONST_M1, new ConstAgent(Type.INT_TYPE)), @@ -128,22 +127,22 @@ public RouterAgent(final boolean counting) { new MapEntry<>(Opcodes.IRETURN, new ReturnAgent()), new MapEntry<>(Opcodes.ARETURN, new ReturnAgent()), new MapEntry<>(LabelInstruction.LABEL_OPCODE, new LabelAgent()), - new MapEntry<>(RouterAgent.UNIMPLEMENTED, new UnimplementedHandler(counting)) + new MapEntry<>(AllAgents.UNIMPLEMENTED, new UnimplementedAgent(counting)) ) ); } /** * Constructor. - * @param handlers All handlers that will try to handle incoming instructions. + * @param agents All handlers that will try to handle incoming instructions. */ - private RouterAgent(final Map handlers) { - this.handlers = handlers; + private AllAgents(final Map agents) { + this.handlers = agents; } @Override public void handle(final DecompilerState state) { - this.handler(state.instruction().opcode()).handle(state); + this.agent(state.instruction().opcode()).handle(state); } /** @@ -163,15 +162,15 @@ public String[] supportedOpcodes() { * @param opcode Instruction opcode. * @return Instruction handler. */ - private DecompilationAgent handler(final int opcode) { - return this.handlers.getOrDefault(opcode, this.handlers.get(RouterAgent.UNIMPLEMENTED)); + private DecompilationAgent agent(final int opcode) { + return this.handlers.getOrDefault(opcode, this.handlers.get(AllAgents.UNIMPLEMENTED)); } /** * Unimplemented instruction handler. * @since 0.1 */ - private static final class UnimplementedHandler implements DecompilationAgent { + private static final class UnimplementedAgent implements DecompilationAgent { /** * Do we put numbers to opcodes? @@ -182,7 +181,7 @@ private static final class UnimplementedHandler implements DecompilationAgent { * Constructor. * @param counting Flag which decides if we need to count opcodes. */ - private UnimplementedHandler(final boolean counting) { + private UnimplementedAgent(final boolean counting) { this.counting = counting; } diff --git a/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java b/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java index 38325c88..bb9753da 100644 --- a/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java +++ b/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java @@ -26,7 +26,7 @@ import java.util.stream.Collectors; import org.cactoos.io.ResourceOf; import org.eolang.opeo.SelectiveDecompiler; -import org.eolang.opeo.decompilation.agents.RouterAgent; +import org.eolang.opeo.decompilation.agents.AllAgents; import org.eolang.opeo.storage.InMemoryStorage; import org.eolang.opeo.storage.XmirEntry; import org.hamcrest.MatcherAssert; @@ -161,7 +161,7 @@ void avoidsDecompileLargeFileWithUnknownDependencies() { void identifiesUnsupportedOpcodes() { MatcherAssert.assertThat( "We expect that the supported opcodes won't contain the 'GOTO' opcode since we don't support it yet.", - new RouterAgent(false).supportedOpcodes(), + new AllAgents(false).supportedOpcodes(), Matchers.not(Matchers.arrayContaining("GOTO")) ); } From 71c0739e00b8a5ae64fa2bfb69cdb8f09035f76d Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 12 Aug 2024 13:55:05 +0300 Subject: [PATCH 04/11] feat(#376): try to modify all agents to be independent --- src/main/java/org/eolang/opeo/ast/Opcode.java | 39 ++++++++ .../opeo/decompilation/DecompilerMachine.java | 18 +++- .../opeo/decompilation/DecompilerState.java | 58 ++++------- .../opeo/decompilation/OperandStack.java | 2 +- .../opeo/decompilation/agents/AddAgent.java | 22 ++++- .../opeo/decompilation/agents/AllAgents.java | 98 ++++++++++--------- .../decompilation/agents/BipushAgent.java | 14 ++- .../opeo/decompilation/agents/CastAgent.java | 83 ++++++++++++---- .../decompilation/agents/CheckCastAgent.java | 18 +++- .../opeo/decompilation/agents/ConstAgent.java | 53 +++++----- .../opeo/decompilation/agents/DupAgent.java | 16 ++- .../decompilation/agents/GetFieldAgent.java | 29 +++--- .../decompilation/agents/GetStaticAgent.java | 11 ++- .../opeo/decompilation/agents/IfAgent.java | 18 ++-- .../agents/InvokedynamicAgent.java | 27 ++--- .../agents/InvokeinterfaceAgent.java | 45 +++++---- .../agents/InvokespecialAgent.java | 51 +++++----- .../agents/InvokestaticAgent.java | 37 +++---- .../agents/InvokevirtualAgent.java | 43 ++++---- .../opeo/decompilation/agents/LabelAgent.java | 13 ++- .../opeo/decompilation/agents/LdcAgent.java | 7 +- .../opeo/decompilation/agents/LoadAgent.java | 55 ++++++++--- .../opeo/decompilation/agents/MulAgent.java | 36 +++---- .../opeo/decompilation/agents/NewAgent.java | 15 ++- .../decompilation/agents/NewArrayAgent.java | 22 ++++- .../opeo/decompilation/agents/PopAgent.java | 7 +- .../decompilation/agents/PutFieldAgent.java | 36 +++---- .../opeo/decompilation/agents/StoreAgent.java | 70 ++++++++----- .../agents/StoreToArrayAgent.java | 29 ++++-- .../agents/SubstractionAgent.java | 22 ++++- 30 files changed, 627 insertions(+), 367 deletions(-) diff --git a/src/main/java/org/eolang/opeo/ast/Opcode.java b/src/main/java/org/eolang/opeo/ast/Opcode.java index 334fec66..6609eb6c 100644 --- a/src/main/java/org/eolang/opeo/ast/Opcode.java +++ b/src/main/java/org/eolang/opeo/ast/Opcode.java @@ -35,6 +35,7 @@ import org.eolang.jeo.representation.xmir.XmlInstruction; import org.eolang.jeo.representation.xmir.XmlNode; import org.eolang.jeo.representation.xmir.XmlOperand; +import org.eolang.opeo.Instruction; import org.xembly.Directive; /** @@ -119,6 +120,10 @@ public Opcode(final XmlInstruction instruction) { ); } + public Opcode(final Instruction instruction) { + this(instruction.opcode(), instruction.operands()); + } + /** * Constructor. * @param bytecode Bytecode @@ -141,6 +146,40 @@ public List opcodes() { return Arrays.asList(this); } + /** + * Opcode number. + * @return Opcode number. + */ + public int opcode() { + return this.bytecode; + } + + /** + * Opcode operands. + * @return Opcode operands. + */ + public List params() { + return this.operands; + } + + /** + * Instruction operand. + * @param index Operand index. + * @return Instruction operand. + */ + public Object operand(final int index) { + if (this.operands.size() <= index) { + throw new IllegalStateException( + String.format( + "Instruction '%s' doesn't have operand at index '%d'", + this, + index + ) + ); + } + return this.operands.get(index); + } + /** * Disable opcodes counting. * It is useful for tests. diff --git a/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java b/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java index 18e6a905..57f27cea 100644 --- a/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java +++ b/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java @@ -24,10 +24,16 @@ package org.eolang.opeo.decompilation; import java.util.Arrays; +import java.util.Deque; import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.cactoos.list.ListOf; import org.eolang.opeo.Instruction; +import org.eolang.opeo.ast.AstNode; +import org.eolang.opeo.ast.Opcode; import org.eolang.opeo.ast.Root; import org.eolang.opeo.decompilation.agents.AllAgents; import org.xembly.Directive; @@ -85,10 +91,14 @@ public DecompilerMachine(final LocalVariables locals, final Map * @return Decompiled instructions. */ public Iterable decompile(final Instruction... instructions) { - final DecompilerState state = new DecompilerState(this.locals); - Arrays.stream(instructions) - .forEach(inst -> this.agents.handle(state.next(inst))); - return new Root(new ListOf<>(state.stack().descendingIterator())).toXmir(); + final DecompilerState initial = new DecompilerState( + new OperandStack(Arrays.stream(instructions) + .map(Opcode::new) + .collect(Collectors.toCollection(LinkedList::new))), + this.locals + ); + this.agents.handle(initial); + return new Root(new ListOf<>(initial.stack().descendingIterator())).toXmir(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java index ab204b27..6dfe44d8 100644 --- a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java +++ b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java @@ -26,6 +26,7 @@ import lombok.ToString; import org.eolang.opeo.Instruction; import org.eolang.opeo.ast.AstNode; +import org.eolang.opeo.ast.Opcode; import org.objectweb.asm.Type; /** @@ -36,11 +37,6 @@ @ToString public final class DecompilerState { - /** - * Current instruction. - */ - private final Instruction current; - /** * Current operand stack. * It might have some values inside. @@ -57,31 +53,25 @@ public final class DecompilerState { * @param vars Method local variables. */ public DecompilerState(final LocalVariables vars) { - this(new Instruction.Nop(), new OperandStack(), vars); + this(new OperandStack(), vars); } /** * Constructor. - * @param current Current instruction. - * @param stack Operand stack. - * @param variables Method local variables. + * @param operands Operand stack. + * @param vars Method local variables. */ - private DecompilerState( - final Instruction current, - final OperandStack stack, - final LocalVariables variables - ) { - this.current = current; - this.operands = stack; - this.vars = variables; + public DecompilerState(final OperandStack operands, final LocalVariables vars) { + this.operands = operands; + this.vars = vars; } /** * Retrieve current bytecode instruction. * @return Current bytecode instruction. */ - public Instruction instruction() { - return this.current; + public Opcode instruction() { + return (Opcode) this.stack().peek(); } /** @@ -90,16 +80,17 @@ public Instruction instruction() { * @return Instruction operand. */ public Object operand(final int index) { - if (this.current.operands().size() <= index) { - throw new IllegalStateException( - String.format( - "Instruction '%s' doesn't have operand at index '%d'", - this.current, - index - ) - ); - } - return this.current.operand(index); + return this.instruction().operand(index); +// if (this.current.operands().size() <= index) { +// throw new IllegalStateException( +// String.format( +// "Instruction '%s' doesn't have operand at index '%d'", +// this.current, +// index +// ) +// ); +// } +// return this.current.operand(index); } /** @@ -119,13 +110,4 @@ public AstNode variable(final int index, final Type type) { public OperandStack stack() { return this.operands; } - - /** - * Move the state to the next instruction. - * @param instruction Next instruction. - * @return New decompiler state with the next instruction. - */ - DecompilerState next(final Instruction instruction) { - return new DecompilerState(instruction, this.operands, this.vars); - } } diff --git a/src/main/java/org/eolang/opeo/decompilation/OperandStack.java b/src/main/java/org/eolang/opeo/decompilation/OperandStack.java index b96d9455..680199a4 100644 --- a/src/main/java/org/eolang/opeo/decompilation/OperandStack.java +++ b/src/main/java/org/eolang/opeo/decompilation/OperandStack.java @@ -62,7 +62,7 @@ public final class OperandStack { * Constructor. * @param original Initial stack collection. */ - private OperandStack(final Deque original) { + OperandStack(final Deque original) { this.stack = original; } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java index 248cbcd2..84b002e6 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java @@ -23,10 +23,14 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.Addition; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; /** * Add instruction handler. @@ -34,11 +38,23 @@ */ public final class AddAgent implements DecompilationAgent { + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.IADD, + Opcodes.LADD, + Opcodes.FADD, + Opcodes.DADD + ) + ); + @Override public void handle(final DecompilerState state) { - final AstNode right = state.stack().pop(); - final AstNode left = state.stack().pop(); - state.stack().push(new Addition(left, right)); + final int opcode = state.instruction().opcode(); + if (AddAgent.SUPPORTED.contains(opcode)) { + final AstNode right = state.stack().pop(); + final AstNode left = state.stack().pop(); + state.stack().push(new Addition(left, right)); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java index 98ca7109..e588966f 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java @@ -49,7 +49,7 @@ public final class AllAgents implements DecompilationAgent { /** * ALl instruction handlers. */ - private final Map handlers; + private final Map agents; /** * Constructor. @@ -58,20 +58,20 @@ public final class AllAgents implements DecompilationAgent { public AllAgents(final boolean counting) { this( new MapOf( - new MapEntry<>(Opcodes.ICONST_M1, new ConstAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_0, new ConstAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_1, new ConstAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_2, new ConstAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_3, new ConstAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_4, new ConstAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.ICONST_5, new ConstAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.LCONST_0, new ConstAgent(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.LCONST_1, new ConstAgent(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.FCONST_0, new ConstAgent(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.FCONST_1, new ConstAgent(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.FCONST_2, new ConstAgent(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.DCONST_0, new ConstAgent(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.DCONST_1, new ConstAgent(Type.DOUBLE_TYPE)), + new MapEntry<>(Opcodes.ICONST_M1, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_0, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_1, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_2, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_3, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_4, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_5, new ConstAgent()), + new MapEntry<>(Opcodes.LCONST_0, new ConstAgent()), + new MapEntry<>(Opcodes.LCONST_1, new ConstAgent()), + new MapEntry<>(Opcodes.FCONST_0, new ConstAgent()), + new MapEntry<>(Opcodes.FCONST_1, new ConstAgent()), + new MapEntry<>(Opcodes.FCONST_2, new ConstAgent()), + new MapEntry<>(Opcodes.DCONST_0, new ConstAgent()), + new MapEntry<>(Opcodes.DCONST_1, new ConstAgent()), new MapEntry<>(Opcodes.IADD, new AddAgent()), new MapEntry<>(Opcodes.LADD, new AddAgent()), new MapEntry<>(Opcodes.FADD, new AddAgent()), @@ -80,33 +80,33 @@ public AllAgents(final boolean counting) { new MapEntry<>(Opcodes.LSUB, new SubstractionAgent()), new MapEntry<>(Opcodes.FSUB, new SubstractionAgent()), new MapEntry<>(Opcodes.DSUB, new SubstractionAgent()), - new MapEntry<>(Opcodes.IMUL, new MulAgent(counting)), + new MapEntry<>(Opcodes.IMUL, new MulAgent()), new MapEntry<>(Opcodes.IF_ICMPGT, new IfAgent()), - new MapEntry<>(Opcodes.I2B, new CastAgent(Type.BYTE_TYPE)), - new MapEntry<>(Opcodes.I2C, new CastAgent(Type.CHAR_TYPE)), - new MapEntry<>(Opcodes.I2S, new CastAgent(Type.SHORT_TYPE)), - new MapEntry<>(Opcodes.I2L, new CastAgent(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.I2F, new CastAgent(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.I2D, new CastAgent(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.L2I, new CastAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.L2F, new CastAgent(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.L2D, new CastAgent(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.F2I, new CastAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.F2L, new CastAgent(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.F2D, new CastAgent(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.D2I, new CastAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.D2L, new CastAgent(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.D2F, new CastAgent(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.ILOAD, new LoadAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.LLOAD, new LoadAgent(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.FLOAD, new LoadAgent(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.DLOAD, new LoadAgent(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.ALOAD, new LoadAgent(Type.getType(Object.class))), - new MapEntry<>(Opcodes.ISTORE, new StoreAgent(Type.INT_TYPE)), - new MapEntry<>(Opcodes.LSTORE, new StoreAgent(Type.LONG_TYPE)), - new MapEntry<>(Opcodes.FSTORE, new StoreAgent(Type.FLOAT_TYPE)), - new MapEntry<>(Opcodes.DSTORE, new StoreAgent(Type.DOUBLE_TYPE)), - new MapEntry<>(Opcodes.ASTORE, new StoreAgent(Type.getType(Object.class))), + new MapEntry<>(Opcodes.I2B, new CastAgent()), + new MapEntry<>(Opcodes.I2C, new CastAgent()), + new MapEntry<>(Opcodes.I2S, new CastAgent()), + new MapEntry<>(Opcodes.I2L, new CastAgent()), + new MapEntry<>(Opcodes.I2F, new CastAgent()), + new MapEntry<>(Opcodes.I2D, new CastAgent()), + new MapEntry<>(Opcodes.L2I, new CastAgent()), + new MapEntry<>(Opcodes.L2F, new CastAgent()), + new MapEntry<>(Opcodes.L2D, new CastAgent()), + new MapEntry<>(Opcodes.F2I, new CastAgent()), + new MapEntry<>(Opcodes.F2L, new CastAgent()), + new MapEntry<>(Opcodes.F2D, new CastAgent()), + new MapEntry<>(Opcodes.D2I, new CastAgent()), + new MapEntry<>(Opcodes.D2L, new CastAgent()), + new MapEntry<>(Opcodes.D2F, new CastAgent()), + new MapEntry<>(Opcodes.ILOAD, new LoadAgent()), + new MapEntry<>(Opcodes.LLOAD, new LoadAgent()), + new MapEntry<>(Opcodes.FLOAD, new LoadAgent()), + new MapEntry<>(Opcodes.DLOAD, new LoadAgent()), + new MapEntry<>(Opcodes.ALOAD, new LoadAgent()), + new MapEntry<>(Opcodes.ISTORE, new StoreAgent()), + new MapEntry<>(Opcodes.LSTORE, new StoreAgent()), + new MapEntry<>(Opcodes.FSTORE, new StoreAgent()), + new MapEntry<>(Opcodes.DSTORE, new StoreAgent()), + new MapEntry<>(Opcodes.ASTORE, new StoreAgent()), new MapEntry<>(Opcodes.AASTORE, new StoreToArrayAgent()), new MapEntry<>(Opcodes.ANEWARRAY, new NewArrayAgent()), new MapEntry<>(Opcodes.CHECKCAST, new CheckCastAgent()), @@ -137,12 +137,14 @@ public AllAgents(final boolean counting) { * @param agents All handlers that will try to handle incoming instructions. */ private AllAgents(final Map agents) { - this.handlers = agents; + this.agents = agents; } @Override public void handle(final DecompilerState state) { - this.agent(state.instruction().opcode()).handle(state); + //todo: until? + this.agents.values().forEach(agent -> agent.handle(state)); +// this.agent(state.instruction().opcode()).handle(state); } /** @@ -150,7 +152,7 @@ public void handle(final DecompilerState state) { * @return Supported opcodes. */ public String[] supportedOpcodes() { - return this.handlers.keySet() + return this.agents.keySet() .stream() .map(OpcodeName::new) .map(OpcodeName::simplified) @@ -162,9 +164,9 @@ public String[] supportedOpcodes() { * @param opcode Instruction opcode. * @return Instruction handler. */ - private DecompilationAgent agent(final int opcode) { - return this.handlers.getOrDefault(opcode, this.handlers.get(AllAgents.UNIMPLEMENTED)); - } +// private DecompilationAgent agent(final int opcode) { +// return this.agents.getOrDefault(opcode, this.agents.get(AllAgents.UNIMPLEMENTED)); +// } /** * Unimplemented instruction handler. @@ -190,7 +192,7 @@ public void handle(final DecompilerState state) { state.stack().push( new Opcode( state.instruction().opcode(), - state.instruction().operands(), + state.instruction().params(), this.counting ) ); diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java index 3a9f62b0..d10e7b8a 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java @@ -23,9 +23,13 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.Literal; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; /** * Bipush instruction handler. @@ -33,9 +37,17 @@ */ public final class BipushAgent implements DecompilationAgent { + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.BIPUSH + ) + ); + @Override public void handle(final DecompilerState state) { - state.stack().push(new Literal(state.operand(0))); + if (BipushAgent.SUPPORTED.contains(state.instruction().opcode())) { + state.stack().push(new Literal(state.operand(0))); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java index 6c5228ad..dddd6ca1 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java @@ -23,6 +23,9 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.Cast; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; @@ -34,26 +37,70 @@ */ public final class CastAgent implements DecompilationAgent { - /** - * Target type. - */ - private final Type target; - - /** - * Constructor. - * @param target Target type - */ - CastAgent(final Type target) { - this.target = target; - } + private static final Set SUPPROTED = new HashSet<>( + Arrays.asList( + org.objectweb.asm.Opcodes.I2B, + org.objectweb.asm.Opcodes.I2C, + org.objectweb.asm.Opcodes.I2S, + org.objectweb.asm.Opcodes.I2L, + org.objectweb.asm.Opcodes.I2F, + org.objectweb.asm.Opcodes.I2D, + org.objectweb.asm.Opcodes.L2I, + org.objectweb.asm.Opcodes.L2F, + org.objectweb.asm.Opcodes.L2D, + org.objectweb.asm.Opcodes.F2I, + org.objectweb.asm.Opcodes.F2L, + org.objectweb.asm.Opcodes.F2D, + org.objectweb.asm.Opcodes.D2I, + org.objectweb.asm.Opcodes.D2L, + org.objectweb.asm.Opcodes.D2F + ) + ); @Override public void handle(final DecompilerState state) { - state.stack().push( - new Cast( - this.target, - state.stack().pop() - ) - ); + final int opcode = state.instruction().opcode(); + if (CastAgent.SUPPROTED.contains(opcode)) { + state.stack().push( + new Cast( + CastAgent.target(opcode), + state.stack().pop() + ) + ); + } + } + + private static Type target(final int opcode) { + switch (opcode) { + case org.objectweb.asm.Opcodes.I2B: + return Type.BYTE_TYPE; + case org.objectweb.asm.Opcodes.I2C: + return Type.CHAR_TYPE; + case org.objectweb.asm.Opcodes.I2S: + return Type.SHORT_TYPE; + case org.objectweb.asm.Opcodes.I2L: + case org.objectweb.asm.Opcodes.F2L: + case org.objectweb.asm.Opcodes.D2L: + return Type.LONG_TYPE; + case org.objectweb.asm.Opcodes.I2F: + case org.objectweb.asm.Opcodes.L2F: + case org.objectweb.asm.Opcodes.D2F: + return Type.FLOAT_TYPE; + case org.objectweb.asm.Opcodes.I2D: + case org.objectweb.asm.Opcodes.L2D: + case org.objectweb.asm.Opcodes.F2D: + return Type.DOUBLE_TYPE; + case org.objectweb.asm.Opcodes.L2I: + case org.objectweb.asm.Opcodes.F2I: + case org.objectweb.asm.Opcodes.D2I: + return Type.INT_TYPE; + default: + throw new IllegalArgumentException( + String.format( + "Unsupported opcode: %d", + opcode + ) + ); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java index 5d7cf152..e3b036d2 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java @@ -23,10 +23,14 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.CheckCast; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; /** @@ -36,10 +40,18 @@ */ public final class CheckCastAgent implements DecompilationAgent { + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.CHECKCAST + ) + ); + @Override public void handle(final DecompilerState state) { - final AstNode value = state.stack().pop(); - final Object type = state.operand(0); - state.stack().push(new CheckCast(Type.getObjectType((String) type), value)); + if (CheckCastAgent.SUPPORTED.contains(state.instruction().opcode())) { + final AstNode value = state.stack().pop(); + final Object type = state.operand(0); + state.stack().push(new CheckCast(Type.getObjectType((String) type), value)); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java index 2d1f42db..8a03d370 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java @@ -36,37 +36,38 @@ */ public final class ConstAgent implements DecompilationAgent { - /** - * Type of constant. - */ - private final Type type; - - /** - * Constructor. - * @param type Type of constant - */ - ConstAgent(final Type type) { - this.type = type; - } - @Override public void handle(final DecompilerState state) { final int opcode = state.instruction().opcode(); final AstNode res; - if (this.type.equals(Type.INT_TYPE)) { - res = ConstAgent.intConstant(opcode); - } else if (this.type.equals(Type.LONG_TYPE)) { - res = ConstAgent.longConstant(opcode); - } else if (this.type.equals(Type.FLOAT_TYPE)) { - res = ConstAgent.floatConstant(opcode); - } else if (this.type.equals(Type.DOUBLE_TYPE)) { - res = ConstAgent.doubleConstant(opcode); - } else { - throw new UnsupportedOperationException( - String.format("Type %s is not supported yet", this.type) - ); + switch (opcode) { + case Opcodes.ICONST_M1: + case Opcodes.ICONST_0: + case Opcodes.ICONST_1: + case Opcodes.ICONST_2: + case Opcodes.ICONST_3: + case Opcodes.ICONST_4: + case Opcodes.ICONST_5: + res = ConstAgent.intConstant(opcode); + state.stack().push(res); + break; + case Opcodes.LCONST_0: + case Opcodes.LCONST_1: + res = ConstAgent.longConstant(opcode); + state.stack().push(res); + break; + case Opcodes.FCONST_0: + case Opcodes.FCONST_1: + case Opcodes.FCONST_2: + res = ConstAgent.floatConstant(opcode); + state.stack().push(res); + break; + case Opcodes.DCONST_0: + case Opcodes.DCONST_1: + res = ConstAgent.doubleConstant(opcode); + state.stack().push(res); + break; } - state.stack().push(res); } /** diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java index 3259bf98..55a38676 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java @@ -23,10 +23,14 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.Duplicate; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.OperandStack; +import org.objectweb.asm.Opcodes; /** * Dup instruction handler. @@ -34,10 +38,18 @@ */ public final class DupAgent implements DecompilationAgent { + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.DUP + ) + ); + @Override public void handle(final DecompilerState state) { - final OperandStack stack = state.stack(); - stack.push(new Duplicate(stack.pop())); + if (DupAgent.SUPPORTED.contains(state.instruction().opcode())) { + final OperandStack stack = state.stack(); + stack.push(new Duplicate(stack.pop())); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java index 0d98afef..f31e9553 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java @@ -23,6 +23,7 @@ */ package org.eolang.opeo.decompilation.agents; +import jdk.internal.org.objectweb.asm.Opcodes; import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.FieldRetrieval; import org.eolang.opeo.decompilation.DecompilerState; @@ -36,19 +37,21 @@ public final class GetFieldAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - final String owner = (String) state.operand(0); - final String name = (String) state.operand(1); - final String descriptor = (String) state.operand(2); - state.stack().push( - new FieldRetrieval( - state.stack().pop(), - new Attributes() - .name(name) - .descriptor(descriptor) - .owner(owner) - .type("field") - ) - ); + if (state.instruction().opcode() == Opcodes.GETFIELD) { + final String owner = (String) state.operand(0); + final String name = (String) state.operand(1); + final String descriptor = (String) state.operand(2); + state.stack().push( + new FieldRetrieval( + state.stack().pop(), + new Attributes() + .name(name) + .descriptor(descriptor) + .owner(owner) + .type("field") + ) + ); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java index ba1712bb..dba77b75 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java @@ -23,6 +23,7 @@ */ package org.eolang.opeo.decompilation.agents; +import jdk.internal.org.objectweb.asm.Opcodes; import org.eolang.opeo.ast.ClassField; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; @@ -34,9 +35,11 @@ public final class GetStaticAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - final String klass = (String) state.operand(0); - final String method = (String) state.operand(1); - final String descriptor = (String) state.operand(2); - state.stack().push(new ClassField(klass, method, descriptor)); + if (state.instruction().opcode() == Opcodes.GETSTATIC) { + final String klass = (String) state.operand(0); + final String method = (String) state.operand(1); + final String descriptor = (String) state.operand(2); + state.stack().push(new ClassField(klass, method, descriptor)); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java index 2f0dbdcc..a63de73e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java @@ -23,6 +23,9 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.If; import org.eolang.opeo.decompilation.DecompilerState; @@ -40,21 +43,20 @@ */ public final class IfAgent implements DecompilationAgent { + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.IF_ICMPGT + ) + ); + @Override public void handle(final DecompilerState state) { - if (state.instruction().opcode() == Opcodes.IF_ICMPGT) { + if (IfAgent.SUPPORTED.contains(state.instruction().opcode())) { final OperandStack stack = state.stack(); final AstNode second = stack.pop(); final AstNode first = stack.pop(); final Label operand = (Label) state.operand(0); stack.push(new If(first, second, operand)); - } else { - throw new UnsupportedOperationException( - String.format( - "Unsupported opcode: %d", - state.instruction().opcode() - ) - ); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java index 4e055876..86a78c1c 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java @@ -30,6 +30,7 @@ import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Handle; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; /** @@ -40,18 +41,20 @@ public final class InvokedynamicAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - final List operands = state.instruction().operands(); - final String descriptor = (String) operands.get(1); - final List args = state.stack().pop(Type.getArgumentTypes(descriptor).length); - Collections.reverse(args); - final DynamicInvocation node = new DynamicInvocation( - (String) operands.get(0), - new org.eolang.opeo.ast.Handle((Handle) operands.get(2)), - descriptor, - operands.subList(3, operands.size()), - args - ); - state.stack().push(node); + if (state.instruction().opcode() == Opcodes.INVOKEDYNAMIC) { + final List operands = state.instruction().params(); + final String descriptor = (String) operands.get(1); + final List args = state.stack().pop(Type.getArgumentTypes(descriptor).length); + Collections.reverse(args); + final DynamicInvocation node = new DynamicInvocation( + (String) operands.get(0), + new org.eolang.opeo.ast.Handle((Handle) operands.get(2)), + descriptor, + operands.subList(3, operands.size()), + args + ); + state.stack().push(node); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java index d8fd1d36..33a32e49 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java @@ -28,8 +28,9 @@ import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.InterfaceInvocation; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; /** @@ -51,25 +52,27 @@ public final class InvokeinterfaceAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - final String owner = (String) state.operand(0); - final String method = (String) state.operand(1); - final String descriptor = (String) state.operand(2); - final boolean interfaced = (Boolean) state.operand(3); - final List args = state.stack().pop( - Type.getArgumentCount(descriptor) - ); - Collections.reverse(args); - final AstNode source = state.stack().pop(); - state.stack().push( - new InterfaceInvocation( - source, - new Attributes() - .name(method) - .descriptor(descriptor) - .interfaced(interfaced) - .owner(owner), - args - ) - ); + if (state.instruction().opcode() != Opcodes.INVOKEINTERFACE) { + final String owner = (String) state.operand(0); + final String method = (String) state.operand(1); + final String descriptor = (String) state.operand(2); + final boolean interfaced = (Boolean) state.operand(3); + final List args = state.stack().pop( + Type.getArgumentCount(descriptor) + ); + Collections.reverse(args); + final AstNode source = state.stack().pop(); + state.stack().push( + new InterfaceInvocation( + source, + new Attributes() + .name(method) + .descriptor(descriptor) + .interfaced(interfaced) + .owner(owner), + args + ) + ); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java index 977dbcc4..750afe28 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java @@ -35,6 +35,7 @@ import org.eolang.opeo.ast.This; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; /** @@ -75,31 +76,33 @@ public final class InvokespecialAgent implements DecompilationAgent { @Override @SuppressWarnings("PMD.UnusedLocalVariable") public void handle(final DecompilerState state) { - final String type = (String) state.operand(0); - final String name = (String) state.operand(1); - final String descriptor = (String) state.operand(2); - final boolean interfaced = (boolean) state.operand(3); - final List args = state.stack().pop( - Type.getArgumentCount(descriptor) - ); - Collections.reverse(args); - final AstNode target = state.stack().pop(); - if (InvokespecialAgent.isThis(target)) { - state.stack().push( - new Super(target, args, descriptor, type, name) - ); - } else if (this.isNewAddress(target)) { - state.stack().push( - new Constructor( - target, - new Attributes().descriptor(descriptor).interfaced(interfaced), - args - ) - ); - } else { - state.stack().push( - new Super(target, args, descriptor, type, name) + if (state.instruction().opcode() == Opcodes.INVOKESPECIAL) { + final String type = (String) state.operand(0); + final String name = (String) state.operand(1); + final String descriptor = (String) state.operand(2); + final boolean interfaced = (boolean) state.operand(3); + final List args = state.stack().pop( + Type.getArgumentCount(descriptor) ); + Collections.reverse(args); + final AstNode target = state.stack().pop(); + if (InvokespecialAgent.isThis(target)) { + state.stack().push( + new Super(target, args, descriptor, type, name) + ); + } else if (this.isNewAddress(target)) { + state.stack().push( + new Constructor( + target, + new Attributes().descriptor(descriptor).interfaced(interfaced), + args + ) + ); + } else { + state.stack().push( + new Super(target, args, descriptor, type, name) + ); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java index 654c700f..22f37284 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java @@ -31,6 +31,7 @@ import org.eolang.opeo.ast.StaticInvocation; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; /** @@ -41,22 +42,24 @@ public final class InvokestaticAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - final String owner = (String) state.operand(0); - final String method = (String) state.operand(1); - final String descriptor = (String) state.operand(2); - final boolean interfaced = (boolean) state.operand(3); - final List args = state.stack().pop(Type.getArgumentCount(descriptor)); - Collections.reverse(args); - state.stack().push( - new StaticInvocation( - new Attributes() - .name(method) - .descriptor(descriptor) - .owner(owner) - .interfaced(interfaced), - new Owner(owner), - args - ) - ); + if (state.instruction().opcode() == Opcodes.INVOKESTATIC) { + final String owner = (String) state.operand(0); + final String method = (String) state.operand(1); + final String descriptor = (String) state.operand(2); + final boolean interfaced = (boolean) state.operand(3); + final List args = state.stack().pop(Type.getArgumentCount(descriptor)); + Collections.reverse(args); + state.stack().push( + new StaticInvocation( + new Attributes() + .name(method) + .descriptor(descriptor) + .owner(owner) + .interfaced(interfaced), + new Owner(owner), + args + ) + ); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java index 7e937de3..906ff714 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java @@ -30,6 +30,7 @@ import org.eolang.opeo.ast.Invocation; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; /** @@ -51,25 +52,27 @@ public final class InvokevirtualAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - final String owner = (String) state.operand(0); - final String method = (String) state.operand(1); - final String descriptor = (String) state.operand(2); - final boolean interfaced = (Boolean) state.operand(3); - final List args = state.stack().pop( - Type.getArgumentCount(descriptor) - ); - Collections.reverse(args); - final AstNode source = state.stack().pop(); - state.stack().push( - new Invocation( - source, - new Attributes() - .name(method) - .descriptor(descriptor) - .owner(owner) - .interfaced(interfaced), - args - ) - ); + if (state.instruction().opcode() == Opcodes.INVOKEVIRTUAL) { + final String owner = (String) state.operand(0); + final String method = (String) state.operand(1); + final String descriptor = (String) state.operand(2); + final boolean interfaced = (Boolean) state.operand(3); + final List args = state.stack().pop( + Type.getArgumentCount(descriptor) + ); + Collections.reverse(args); + final AstNode source = state.stack().pop(); + state.stack().push( + new Invocation( + source, + new Attributes() + .name(method) + .descriptor(descriptor) + .owner(owner) + .interfaced(interfaced), + args + ) + ); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java index 3d10b406..93c985e8 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java @@ -23,6 +23,7 @@ */ package org.eolang.opeo.decompilation.agents; +import org.eolang.opeo.LabelInstruction; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Label; import org.eolang.opeo.ast.Labeled; @@ -36,10 +37,12 @@ public final class LabelAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - final Labeled node = new Labeled( - state.stack().first().orElse(new AstNode.Empty()), - new Label(String.class.cast(state.operand(0))) - ); - state.stack().push(node); + if (state.instruction().opcode() == LabelInstruction.LABEL_OPCODE) { + final Labeled node = new Labeled( + state.stack().first().orElse(new AstNode.Empty()), + new Label(String.class.cast(state.operand(0))) + ); + state.stack().push(node); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java index 01861883..50f1276d 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java @@ -23,6 +23,7 @@ */ package org.eolang.opeo.decompilation.agents; +import jdk.internal.org.objectweb.asm.Opcodes; import org.eolang.opeo.ast.Constant; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; @@ -35,8 +36,10 @@ public final class LdcAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - final Object operand = state.operand(0); - state.stack().push(new Constant(operand)); + if (state.instruction().opcode() == Opcodes.LDC) { + final Object operand = state.operand(0); + state.stack().push(new Constant(operand)); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java index 5c8e49d3..a2c34f46 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java @@ -23,8 +23,12 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; /** @@ -37,24 +41,43 @@ */ public final class LoadAgent implements DecompilationAgent { - /** - * Type of the variable. - */ - private final Type type; - - /** - * Constructor. - * @param type Type of the variable. - */ - public LoadAgent(final Type type) { - this.type = type; - } + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.ILOAD, + Opcodes.LLOAD, + Opcodes.FLOAD, + Opcodes.DLOAD, + Opcodes.ALOAD + ) + ); @Override public void handle(final DecompilerState state) { - final Integer index = (Integer) state.operand(0); - state.stack().push( - state.variable(index, this.type) - ); + final int opcode = state.instruction().opcode(); + if (LoadAgent.SUPPORTED.contains(opcode)) { + final Integer index = (Integer) state.operand(0); + state.stack().push( + state.variable(index, LoadAgent.type(opcode)) + ); + } + } + + private static Type type(final int opcode) { + switch (opcode) { + case Opcodes.ILOAD: + return Type.INT_TYPE; + case Opcodes.LLOAD: + return Type.LONG_TYPE; + case Opcodes.FLOAD: + return Type.FLOAT_TYPE; + case Opcodes.DLOAD: + return Type.DOUBLE_TYPE; + case Opcodes.ALOAD: + return Type.getType(Object.class); + default: + throw new IllegalArgumentException( + String.format("Unsupported opcode: %d", opcode) + ); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java index dd2c487f..0f2e391a 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java @@ -23,11 +23,14 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Multiplication; import org.eolang.opeo.ast.Opcode; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; /** @@ -36,33 +39,22 @@ */ public final class MulAgent implements DecompilationAgent { - /** - * Do we put numbers to opcodes? - */ - private final boolean counting; - - /** - * Constructor. - * @param counting Do we put numbers to opcodes? - */ - MulAgent(final boolean counting) { - this.counting = counting; - } + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.IMUL, + Opcodes.LMUL, + Opcodes.FMUL, + Opcodes.DMUL + ) + ); @Override public void handle(final DecompilerState state) { - if (state.instruction().opcode() == Opcodes.IMUL) { + if (MulAgent.SUPPORTED.contains(state.instruction().opcode())) { final AstNode right = state.stack().pop(); final AstNode left = state.stack().pop(); state.stack().push(new Multiplication(left, right)); - } else { - state.stack().push( - new Opcode( - state.instruction().opcode(), - state.instruction().operands(), - this.counting - ) - ); + } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java index 544f5ce2..b658ac85 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java @@ -23,9 +23,13 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.NewAddress; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; /** * New instruction handler. @@ -33,9 +37,18 @@ */ public final class NewAgent implements DecompilationAgent { + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.NEW + ) + ); + + @Override public void handle(final DecompilerState state) { - state.stack().push(new NewAddress(state.operand(0).toString())); + if (NewAgent.SUPPORTED.contains(state.instruction().opcode())) { + state.stack().push(new NewAddress(state.operand(0).toString())); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java index bc767e3e..24cc33aa 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java @@ -23,12 +23,17 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import org.eolang.opeo.SelectiveDecompiler; import org.eolang.opeo.ast.ArrayConstructor; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Reference; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.OperandStack; +import org.objectweb.asm.Opcodes; /** * New array instruction handler. @@ -36,12 +41,19 @@ */ public final class NewArrayAgent implements DecompilationAgent { + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.NEWARRAY + ) + ); + @Override public void handle(final DecompilerState state) { - final String type = (String) state.operand(0); - final OperandStack stack = state.stack(); - final AstNode size = stack.pop(); - stack.push(new Reference(new ArrayConstructor(size, type))); + if (NewArrayAgent.SUPPORTED.contains(state.instruction().opcode())) { + final String type = (String) state.operand(0); + final OperandStack stack = state.stack(); + final AstNode size = stack.pop(); + stack.push(new Reference(new ArrayConstructor(size, type))); + } } - } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java index 97cf73a7..075f9941 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java @@ -23,6 +23,7 @@ */ package org.eolang.opeo.decompilation.agents; +import jdk.internal.org.objectweb.asm.Opcodes; import org.eolang.opeo.ast.Popped; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; @@ -36,8 +37,10 @@ public final class PopAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - final OperandStack stack = state.stack(); - stack.push(new Popped(stack.pop())); + if (state.instruction().opcode() == Opcodes.POP) { + final OperandStack stack = state.stack(); + stack.push(new Popped(stack.pop())); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java index 2849d3a5..9da7cac9 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java @@ -29,6 +29,7 @@ import org.eolang.opeo.ast.FieldAssignment; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; /** * Putfield instruction handler. @@ -39,22 +40,23 @@ public final class PutFieldAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - final AstNode value = state.stack().pop(); - final String name = (String) state.operand(1); - final String owner = (String) state.operand(0); - final String descriptor = (String) state.operand(2); - state.stack().push( - new FieldAssignment( - new Field( - state.stack().pop(), - new Attributes() - .name(name) - .owner(owner) - .descriptor(descriptor) - ), - value - ) - ); + if (state.instruction().opcode() == Opcodes.GETFIELD) { + final AstNode value = state.stack().pop(); + final String name = (String) state.operand(1); + final String owner = (String) state.operand(0); + final String descriptor = (String) state.operand(2); + state.stack().push( + new FieldAssignment( + new Field( + state.stack().pop(), + new Attributes() + .name(name) + .owner(owner) + .descriptor(descriptor) + ), + value + ) + ); + } } - } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java index 63534e27..30f1d807 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java @@ -23,6 +23,9 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.LocalVariable; import org.eolang.opeo.ast.Typed; @@ -30,6 +33,7 @@ import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.OperandStack; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; /** @@ -38,44 +42,66 @@ */ public final class StoreAgent implements DecompilationAgent { - /** - * Type of the variable. - */ - private final Type type; - - /** - * Constructor. - * @param type Type of the variable. - */ - public StoreAgent(final Type type) { - this.type = type; - } + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.ISTORE, + Opcodes.LSTORE, + Opcodes.FSTORE, + Opcodes.DSTORE, + Opcodes.ASTORE + ) + ); @Override public void handle(final DecompilerState state) { - final OperandStack stack = state.stack(); - final AstNode value = stack.pop(); - stack.push( - new VariableAssignment( - (LocalVariable) state.variable((Integer) state.operand(0), this.infer(value)), - value - ) - ); + final int opcode = state.instruction().opcode(); + if (StoreAgent.SUPPORTED.contains(opcode)) { + final OperandStack stack = state.stack(); + final AstNode value = stack.pop(); + stack.push( + new VariableAssignment( + (LocalVariable) state.variable( + (Integer) state.operand(0), this.infer(value, opcode)), + value + ) + ); + } + } /** * Infer type of the variable. * @param value Value to infer type from. + * @param opcode Opcode. * @return Inferred type. */ - private Type infer(final AstNode value) { + private Type infer(final AstNode value, final int opcode) { final Type result; if (value instanceof Typed) { result = ((Typed) value).type(); } else { - result = this.type; + result = StoreAgent.type(opcode); } return result; } + + private static Type type(final int opcode) { + switch (opcode) { + case Opcodes.ISTORE: + return Type.INT_TYPE; + case Opcodes.LSTORE: + return Type.LONG_TYPE; + case Opcodes.FSTORE: + return Type.FLOAT_TYPE; + case Opcodes.DSTORE: + return Type.DOUBLE_TYPE; + case Opcodes.ASTORE: + return Type.getType(Object.class); + default: + throw new IllegalArgumentException( + String.format("Unsupported opcode: %d", opcode) + ); + } + } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java index 069c111d..8734a67b 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java @@ -23,6 +23,9 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Duplicate; import org.eolang.opeo.ast.FieldRetrieval; @@ -45,17 +48,25 @@ */ public final class StoreToArrayAgent implements DecompilationAgent { + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + org.objectweb.asm.Opcodes.AASTORE + ) + ); + @Override public void handle(final DecompilerState state) { - final AstNode value = state.stack().pop(); - final AstNode index = state.stack().pop(); - final AstNode array = state.stack().pop(); - try { - final Reference ref = this.findRef(array); - ref.link(new StoreArray(ref.object(), index, value)); - state.stack().push(ref); - } catch (final IllegalStateException exception) { - state.stack().push(new StoreArray(array, index, value)); + if (StoreToArrayAgent.SUPPORTED.contains(state.instruction().opcode())) { + final AstNode value = state.stack().pop(); + final AstNode index = state.stack().pop(); + final AstNode array = state.stack().pop(); + try { + final Reference ref = this.findRef(array); + ref.link(new StoreArray(ref.object(), index, value)); + state.stack().push(ref); + } catch (final IllegalStateException exception) { + state.stack().push(new StoreArray(array, index, value)); + } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java index f3a841cf..ec3dced8 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java @@ -23,10 +23,14 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Substraction; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; /** * Substraction instruction handler. @@ -34,10 +38,22 @@ */ public final class SubstractionAgent implements DecompilationAgent { + private static final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.ISUB, + Opcodes.LSUB, + Opcodes.FSUB, + Opcodes.DSUB + ) + ); + @Override public void handle(final DecompilerState state) { - final AstNode right = state.stack().pop(); - final AstNode left = state.stack().pop(); - state.stack().push(new Substraction(left, right)); + final int opcode = state.instruction().opcode(); + if (SubstractionAgent.SUPPORTED.contains(opcode)) { + final AstNode right = state.stack().pop(); + final AstNode left = state.stack().pop(); + state.stack().push(new Substraction(left, right)); + } } } From 83318e7e798c917ff9e202c51851d0bc5ad96434 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 12 Aug 2024 15:26:46 +0300 Subject: [PATCH 05/11] feat(#376): fix some small mistakes --- .../opeo/decompilation/DecompilerMachine.java | 8 +- .../opeo/decompilation/DecompilerState.java | 44 ++++++++- .../opeo/decompilation/LocalVariables.java | 2 + .../opeo/decompilation/OperandStack.java | 5 + .../opeo/decompilation/agents/AllAgents.java | 99 ++++++++++--------- .../opeo/decompilation/agents/ConstAgent.java | 82 +++++++++------ .../decompilation/agents/GetFieldAgent.java | 2 +- .../decompilation/agents/GetStaticAgent.java | 2 +- .../agents/InvokeinterfaceAgent.java | 2 +- .../opeo/decompilation/agents/LdcAgent.java | 2 +- .../decompilation/agents/NewArrayAgent.java | 2 +- .../opeo/decompilation/agents/PopAgent.java | 2 +- .../decompilation/agents/PutFieldAgent.java | 2 +- .../decompilation/agents/ReturnAgent.java | 50 ++++++---- .../decompilation/DecompilerMachineTest.java | 20 ++-- 15 files changed, 207 insertions(+), 117 deletions(-) diff --git a/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java b/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java index 57f27cea..1a9223b6 100644 --- a/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java +++ b/src/main/java/org/eolang/opeo/decompilation/DecompilerMachine.java @@ -24,15 +24,12 @@ package org.eolang.opeo.decompilation; import java.util.Arrays; -import java.util.Deque; import java.util.HashMap; import java.util.LinkedList; -import java.util.List; import java.util.Map; import java.util.stream.Collectors; import org.cactoos.list.ListOf; import org.eolang.opeo.Instruction; -import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Opcode; import org.eolang.opeo.ast.Root; import org.eolang.opeo.decompilation.agents.AllAgents; @@ -92,9 +89,10 @@ public DecompilerMachine(final LocalVariables locals, final Map */ public Iterable decompile(final Instruction... instructions) { final DecompilerState initial = new DecompilerState( - new OperandStack(Arrays.stream(instructions) + Arrays.stream(instructions) .map(Opcode::new) - .collect(Collectors.toCollection(LinkedList::new))), + .collect(Collectors.toCollection(LinkedList::new)), + new OperandStack(), this.locals ); this.agents.handle(initial); diff --git a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java index 6dfe44d8..9bde9ae9 100644 --- a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java +++ b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java @@ -23,8 +23,11 @@ */ package org.eolang.opeo.decompilation; +import java.util.Deque; +import java.util.Iterator; +import java.util.LinkedList; +import lombok.EqualsAndHashCode; import lombok.ToString; -import org.eolang.opeo.Instruction; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Opcode; import org.objectweb.asm.Type; @@ -35,13 +38,16 @@ * @since 0.2 */ @ToString +@EqualsAndHashCode public final class DecompilerState { + private final Deque opcodes; + /** * Current operand stack. * It might have some values inside. */ - private final OperandStack operands; + private final OperandStack stack; /** * Method local variables. @@ -62,16 +68,44 @@ public DecompilerState(final LocalVariables vars) { * @param vars Method local variables. */ public DecompilerState(final OperandStack operands, final LocalVariables vars) { - this.operands = operands; + this(new LinkedList<>(), operands, vars); + } + + public DecompilerState( + final Deque opcodes, + final OperandStack stack, + final LocalVariables vars + ) { + this.opcodes = opcodes; + this.stack = stack; this.vars = vars; } + public boolean hasInstructions() { + return !this.opcodes.isEmpty(); + } + + public void move() { + this.opcodes.pop(); + } + /** * Retrieve current bytecode instruction. * @return Current bytecode instruction. */ public Opcode instruction() { - return (Opcode) this.stack().peek(); + return this.opcodes.peek(); +// final Iterator iterator = this.stack().iterator(); +// while (iterator.hasNext()) { +// final AstNode next = iterator.next(); +// if (next instanceof Opcode) { +// return (Opcode) next; +// } +// } +// throw new IllegalStateException( +// "No opcode found in the stack" +// ); +// return (Opcode) this.stack().peek(); } /** @@ -108,6 +142,6 @@ public AstNode variable(final int index, final Type type) { * @return Operand stack. */ public OperandStack stack() { - return this.operands; + return this.stack; } } diff --git a/src/main/java/org/eolang/opeo/decompilation/LocalVariables.java b/src/main/java/org/eolang/opeo/decompilation/LocalVariables.java index 47f38c13..bc37efc8 100644 --- a/src/main/java/org/eolang/opeo/decompilation/LocalVariables.java +++ b/src/main/java/org/eolang/opeo/decompilation/LocalVariables.java @@ -27,6 +27,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; +import lombok.EqualsAndHashCode; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.LocalVariable; import org.eolang.opeo.ast.This; @@ -37,6 +38,7 @@ * Local variables. * @since 0.1 */ +@EqualsAndHashCode public final class LocalVariables { /** diff --git a/src/main/java/org/eolang/opeo/decompilation/OperandStack.java b/src/main/java/org/eolang/opeo/decompilation/OperandStack.java index 680199a4..da100ddd 100644 --- a/src/main/java/org/eolang/opeo/decompilation/OperandStack.java +++ b/src/main/java/org/eolang/opeo/decompilation/OperandStack.java @@ -24,11 +24,14 @@ package org.eolang.opeo.decompilation; import java.util.ArrayDeque; +import java.util.Collection; +import java.util.Collections; import java.util.Deque; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Optional; +import lombok.EqualsAndHashCode; import lombok.ToString; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Label; @@ -44,6 +47,7 @@ * @since 0.2 */ @ToString +@EqualsAndHashCode public final class OperandStack { /** @@ -138,4 +142,5 @@ public void dup() { Iterator descendingIterator() { return this.stack.descendingIterator(); } + } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java index e588966f..90ce7dfb 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java @@ -59,54 +59,54 @@ public AllAgents(final boolean counting) { this( new MapOf( new MapEntry<>(Opcodes.ICONST_M1, new ConstAgent()), - new MapEntry<>(Opcodes.ICONST_0, new ConstAgent()), - new MapEntry<>(Opcodes.ICONST_1, new ConstAgent()), - new MapEntry<>(Opcodes.ICONST_2, new ConstAgent()), - new MapEntry<>(Opcodes.ICONST_3, new ConstAgent()), - new MapEntry<>(Opcodes.ICONST_4, new ConstAgent()), - new MapEntry<>(Opcodes.ICONST_5, new ConstAgent()), - new MapEntry<>(Opcodes.LCONST_0, new ConstAgent()), - new MapEntry<>(Opcodes.LCONST_1, new ConstAgent()), - new MapEntry<>(Opcodes.FCONST_0, new ConstAgent()), - new MapEntry<>(Opcodes.FCONST_1, new ConstAgent()), - new MapEntry<>(Opcodes.FCONST_2, new ConstAgent()), - new MapEntry<>(Opcodes.DCONST_0, new ConstAgent()), - new MapEntry<>(Opcodes.DCONST_1, new ConstAgent()), +// new MapEntry<>(Opcodes.ICONST_0, new ConstAgent()), +// new MapEntry<>(Opcodes.ICONST_1, new ConstAgent()), +// new MapEntry<>(Opcodes.ICONST_2, new ConstAgent()), +// new MapEntry<>(Opcodes.ICONST_3, new ConstAgent()), +// new MapEntry<>(Opcodes.ICONST_4, new ConstAgent()), +// new MapEntry<>(Opcodes.ICONST_5, new ConstAgent()), +// new MapEntry<>(Opcodes.LCONST_0, new ConstAgent()), +// new MapEntry<>(Opcodes.LCONST_1, new ConstAgent()), +// new MapEntry<>(Opcodes.FCONST_0, new ConstAgent()), +// new MapEntry<>(Opcodes.FCONST_1, new ConstAgent()), +// new MapEntry<>(Opcodes.FCONST_2, new ConstAgent()), +// new MapEntry<>(Opcodes.DCONST_0, new ConstAgent()), +// new MapEntry<>(Opcodes.DCONST_1, new ConstAgent()), new MapEntry<>(Opcodes.IADD, new AddAgent()), - new MapEntry<>(Opcodes.LADD, new AddAgent()), - new MapEntry<>(Opcodes.FADD, new AddAgent()), - new MapEntry<>(Opcodes.DADD, new AddAgent()), +// new MapEntry<>(Opcodes.LADD, new AddAgent()), +// new MapEntry<>(Opcodes.FADD, new AddAgent()), +// new MapEntry<>(Opcodes.DADD, new AddAgent()), new MapEntry<>(Opcodes.ISUB, new SubstractionAgent()), - new MapEntry<>(Opcodes.LSUB, new SubstractionAgent()), - new MapEntry<>(Opcodes.FSUB, new SubstractionAgent()), - new MapEntry<>(Opcodes.DSUB, new SubstractionAgent()), +// new MapEntry<>(Opcodes.LSUB, new SubstractionAgent()), +// new MapEntry<>(Opcodes.FSUB, new SubstractionAgent()), +// new MapEntry<>(Opcodes.DSUB, new SubstractionAgent()), new MapEntry<>(Opcodes.IMUL, new MulAgent()), new MapEntry<>(Opcodes.IF_ICMPGT, new IfAgent()), new MapEntry<>(Opcodes.I2B, new CastAgent()), - new MapEntry<>(Opcodes.I2C, new CastAgent()), - new MapEntry<>(Opcodes.I2S, new CastAgent()), - new MapEntry<>(Opcodes.I2L, new CastAgent()), - new MapEntry<>(Opcodes.I2F, new CastAgent()), - new MapEntry<>(Opcodes.I2D, new CastAgent()), - new MapEntry<>(Opcodes.L2I, new CastAgent()), - new MapEntry<>(Opcodes.L2F, new CastAgent()), - new MapEntry<>(Opcodes.L2D, new CastAgent()), - new MapEntry<>(Opcodes.F2I, new CastAgent()), - new MapEntry<>(Opcodes.F2L, new CastAgent()), - new MapEntry<>(Opcodes.F2D, new CastAgent()), - new MapEntry<>(Opcodes.D2I, new CastAgent()), - new MapEntry<>(Opcodes.D2L, new CastAgent()), - new MapEntry<>(Opcodes.D2F, new CastAgent()), +// new MapEntry<>(Opcodes.I2C, new CastAgent()), +// new MapEntry<>(Opcodes.I2S, new CastAgent()), +// new MapEntry<>(Opcodes.I2L, new CastAgent()), +// new MapEntry<>(Opcodes.I2F, new CastAgent()), +// new MapEntry<>(Opcodes.I2D, new CastAgent()), +// new MapEntry<>(Opcodes.L2I, new CastAgent()), +// new MapEntry<>(Opcodes.L2F, new CastAgent()), +// new MapEntry<>(Opcodes.L2D, new CastAgent()), +// new MapEntry<>(Opcodes.F2I, new CastAgent()), +// new MapEntry<>(Opcodes.F2L, new CastAgent()), +// new MapEntry<>(Opcodes.F2D, new CastAgent()), +// new MapEntry<>(Opcodes.D2I, new CastAgent()), +// new MapEntry<>(Opcodes.D2L, new CastAgent()), +// new MapEntry<>(Opcodes.D2F, new CastAgent()), new MapEntry<>(Opcodes.ILOAD, new LoadAgent()), - new MapEntry<>(Opcodes.LLOAD, new LoadAgent()), - new MapEntry<>(Opcodes.FLOAD, new LoadAgent()), - new MapEntry<>(Opcodes.DLOAD, new LoadAgent()), - new MapEntry<>(Opcodes.ALOAD, new LoadAgent()), +// new MapEntry<>(Opcodes.LLOAD, new LoadAgent()), +// new MapEntry<>(Opcodes.FLOAD, new LoadAgent()), +// new MapEntry<>(Opcodes.DLOAD, new LoadAgent()), +// new MapEntry<>(Opcodes.ALOAD, new LoadAgent()), new MapEntry<>(Opcodes.ISTORE, new StoreAgent()), - new MapEntry<>(Opcodes.LSTORE, new StoreAgent()), - new MapEntry<>(Opcodes.FSTORE, new StoreAgent()), - new MapEntry<>(Opcodes.DSTORE, new StoreAgent()), - new MapEntry<>(Opcodes.ASTORE, new StoreAgent()), +// new MapEntry<>(Opcodes.LSTORE, new StoreAgent()), +// new MapEntry<>(Opcodes.FSTORE, new StoreAgent()), +// new MapEntry<>(Opcodes.DSTORE, new StoreAgent()), +// new MapEntry<>(Opcodes.ASTORE, new StoreAgent()), new MapEntry<>(Opcodes.AASTORE, new StoreToArrayAgent()), new MapEntry<>(Opcodes.ANEWARRAY, new NewArrayAgent()), new MapEntry<>(Opcodes.CHECKCAST, new CheckCastAgent()), @@ -124,10 +124,10 @@ public AllAgents(final boolean counting) { new MapEntry<>(Opcodes.LDC, new LdcAgent()), new MapEntry<>(Opcodes.POP, new PopAgent()), new MapEntry<>(Opcodes.RETURN, new ReturnAgent()), - new MapEntry<>(Opcodes.IRETURN, new ReturnAgent()), - new MapEntry<>(Opcodes.ARETURN, new ReturnAgent()), - new MapEntry<>(LabelInstruction.LABEL_OPCODE, new LabelAgent()), - new MapEntry<>(AllAgents.UNIMPLEMENTED, new UnimplementedAgent(counting)) +// new MapEntry<>(Opcodes.IRETURN, new ReturnAgent()), +// new MapEntry<>(Opcodes.ARETURN, new ReturnAgent()), + new MapEntry<>(LabelInstruction.LABEL_OPCODE, new LabelAgent()) +// , new MapEntry<>(AllAgents.UNIMPLEMENTED, new UnimplementedAgent(counting)) ) ); } @@ -143,7 +143,14 @@ private AllAgents(final Map agents) { @Override public void handle(final DecompilerState state) { //todo: until? - this.agents.values().forEach(agent -> agent.handle(state)); + int hash; + while (state.hasInstructions()) { + this.agents.values().forEach(agent -> agent.handle(state)); + state.move(); + } +// do { +// hash = state.hashCode(); +// } while (hash != state.hashCode()); // this.agent(state.instruction().opcode()).handle(state); } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java index 8a03d370..663ff2f1 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java @@ -23,6 +23,10 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import org.eolang.opeo.SelectiveDecompiler; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Literal; import org.eolang.opeo.decompilation.DecompilerState; @@ -36,38 +40,60 @@ */ public final class ConstAgent implements DecompilationAgent { + private final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.ICONST_M1, + Opcodes.ICONST_0, + Opcodes.ICONST_1, + Opcodes.ICONST_2, + Opcodes.ICONST_3, + Opcodes.ICONST_4, + Opcodes.ICONST_5, + Opcodes.LCONST_0, + Opcodes.LCONST_1, + Opcodes.FCONST_0, + Opcodes.FCONST_1, + Opcodes.FCONST_2, + Opcodes.DCONST_0, + Opcodes.DCONST_1 + ) + ); + @Override public void handle(final DecompilerState state) { final int opcode = state.instruction().opcode(); - final AstNode res; - switch (opcode) { - case Opcodes.ICONST_M1: - case Opcodes.ICONST_0: - case Opcodes.ICONST_1: - case Opcodes.ICONST_2: - case Opcodes.ICONST_3: - case Opcodes.ICONST_4: - case Opcodes.ICONST_5: - res = ConstAgent.intConstant(opcode); - state.stack().push(res); - break; - case Opcodes.LCONST_0: - case Opcodes.LCONST_1: - res = ConstAgent.longConstant(opcode); - state.stack().push(res); - break; - case Opcodes.FCONST_0: - case Opcodes.FCONST_1: - case Opcodes.FCONST_2: - res = ConstAgent.floatConstant(opcode); - state.stack().push(res); - break; - case Opcodes.DCONST_0: - case Opcodes.DCONST_1: - res = ConstAgent.doubleConstant(opcode); - state.stack().push(res); - break; + if (this.SUPPORTED.contains(opcode)) { + final AstNode res; + switch (opcode) { + case Opcodes.ICONST_M1: + case Opcodes.ICONST_0: + case Opcodes.ICONST_1: + case Opcodes.ICONST_2: + case Opcodes.ICONST_3: + case Opcodes.ICONST_4: + case Opcodes.ICONST_5: + res = ConstAgent.intConstant(opcode); + state.stack().push(res); + break; + case Opcodes.LCONST_0: + case Opcodes.LCONST_1: + res = ConstAgent.longConstant(opcode); + state.stack().push(res); + break; + case Opcodes.FCONST_0: + case Opcodes.FCONST_1: + case Opcodes.FCONST_2: + res = ConstAgent.floatConstant(opcode); + state.stack().push(res); + break; + case Opcodes.DCONST_0: + case Opcodes.DCONST_1: + res = ConstAgent.doubleConstant(opcode); + state.stack().push(res); + break; + } } + } /** diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java index f31e9553..36b70a57 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java @@ -23,11 +23,11 @@ */ package org.eolang.opeo.decompilation.agents; -import jdk.internal.org.objectweb.asm.Opcodes; import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.FieldRetrieval; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; /** * Getfield instruction handler. diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java index dba77b75..ba4906ba 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java @@ -23,10 +23,10 @@ */ package org.eolang.opeo.decompilation.agents; -import jdk.internal.org.objectweb.asm.Opcodes; import org.eolang.opeo.ast.ClassField; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; /** * Getstatic instruction handler. diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java index 33a32e49..68eaea92 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java @@ -52,7 +52,7 @@ public final class InvokeinterfaceAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - if (state.instruction().opcode() != Opcodes.INVOKEINTERFACE) { + if (state.instruction().opcode() == Opcodes.INVOKEINTERFACE) { final String owner = (String) state.operand(0); final String method = (String) state.operand(1); final String descriptor = (String) state.operand(2); diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java index 50f1276d..a3e8738c 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java @@ -23,10 +23,10 @@ */ package org.eolang.opeo.decompilation.agents; -import jdk.internal.org.objectweb.asm.Opcodes; import org.eolang.opeo.ast.Constant; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.objectweb.asm.Opcodes; /** * Ldc instruction handler. diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java index 24cc33aa..c1668c52 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java @@ -43,7 +43,7 @@ public final class NewArrayAgent implements DecompilationAgent { private static final Set SUPPORTED = new HashSet<>( Arrays.asList( - Opcodes.NEWARRAY + Opcodes.ANEWARRAY ) ); diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java index 075f9941..7b4f52f2 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java @@ -23,11 +23,11 @@ */ package org.eolang.opeo.decompilation.agents; -import jdk.internal.org.objectweb.asm.Opcodes; import org.eolang.opeo.ast.Popped; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.OperandStack; +import org.objectweb.asm.Opcodes; /** * Pop instruction handler. diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java index 9da7cac9..ca8c8f1d 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java @@ -40,7 +40,7 @@ public final class PutFieldAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { - if (state.instruction().opcode() == Opcodes.GETFIELD) { + if (state.instruction().opcode() == Opcodes.PUTFIELD) { final AstNode value = state.stack().pop(); final String name = (String) state.operand(1); final String owner = (String) state.operand(0); diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java index eb56509d..c1774cff 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java @@ -23,6 +23,9 @@ */ package org.eolang.opeo.decompilation.agents; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; import org.eolang.opeo.ast.Return; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; @@ -36,26 +39,39 @@ */ public final class ReturnAgent implements DecompilationAgent { + private final Set SUPPORTED = new HashSet<>( + Arrays.asList( + Opcodes.RETURN, + Opcodes.IRETURN, + Opcodes.LRETURN, + Opcodes.FRETURN, + Opcodes.DRETURN, + Opcodes.ARETURN + ) + ); + @Override public void handle(final DecompilerState state) { final int opcode = state.instruction().opcode(); - final OperandStack stack = state.stack(); - if (opcode == Opcodes.RETURN) { - stack.push(new Return()); - } else if (opcode == Opcodes.IRETURN) { - stack.push(new Return(stack.pop())); - } else if (opcode == Opcodes.LRETURN) { - stack.push(new Return(stack.pop())); - } else if (opcode == Opcodes.FRETURN) { - stack.push(new Return(stack.pop())); - } else if (opcode == Opcodes.DRETURN) { - stack.push(new Return(stack.pop())); - } else if (opcode == Opcodes.ARETURN) { - stack.push(new Return(stack.pop())); - } else { - throw new IllegalStateException( - String.format("Unexpected opcode: %d", opcode) - ); + if (this.SUPPORTED.contains(opcode)) { + final OperandStack stack = state.stack(); + if (opcode == Opcodes.RETURN) { + stack.push(new Return()); + } else if (opcode == Opcodes.IRETURN) { + stack.push(new Return(stack.pop())); + } else if (opcode == Opcodes.LRETURN) { + stack.push(new Return(stack.pop())); + } else if (opcode == Opcodes.FRETURN) { + stack.push(new Return(stack.pop())); + } else if (opcode == Opcodes.DRETURN) { + stack.push(new Return(stack.pop())); + } else if (opcode == Opcodes.ARETURN) { + stack.push(new Return(stack.pop())); + } else { + throw new IllegalStateException( + String.format("Unexpected opcode: %d", opcode) + ); + } } } } diff --git a/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java b/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java index 5c253d2b..c7b61c16 100644 --- a/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java +++ b/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java @@ -23,6 +23,7 @@ */ package org.eolang.opeo.decompilation; +import com.jcabi.xml.XMLDocument; import java.util.Collections; import java.util.concurrent.atomic.AtomicReference; import org.eolang.jeo.matchers.SameXml; @@ -243,17 +244,18 @@ void decompilesInvokeVirtual() { @Test void decompilesArrayCreation() throws ImpossibleModificationException { final String type = "java/lang/Object"; + final String xml = new Xembler( + new DecompilerMachine() + .decompile( + new OpcodeInstruction(Opcodes.ICONST_2), + new OpcodeInstruction(Opcodes.ICONST_3), + new OpcodeInstruction(Opcodes.IADD), + new OpcodeInstruction(Opcodes.ANEWARRAY, type) + ) + ).xml(); MatcherAssert.assertThat( "Can't decompile array creation", - new Xembler( - new DecompilerMachine() - .decompile( - new OpcodeInstruction(Opcodes.ICONST_2), - new OpcodeInstruction(Opcodes.ICONST_3), - new OpcodeInstruction(Opcodes.IADD), - new OpcodeInstruction(Opcodes.ANEWARRAY, type) - ) - ).xml(), + xml, new SameXml( new Xembler( new Root( From f4f5df70674026882615ec89ec3d3361392a68d0 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 12 Aug 2024 15:46:04 +0300 Subject: [PATCH 06/11] feat(#376): each agent decides when to move to the next position --- .../opeo/decompilation/DecompilerState.java | 9 +- .../opeo/decompilation/agents/AddAgent.java | 1 + .../opeo/decompilation/agents/AllAgents.java | 90 +++++++++---------- .../decompilation/agents/BipushAgent.java | 1 + .../opeo/decompilation/agents/CastAgent.java | 1 + .../decompilation/agents/CheckCastAgent.java | 1 + .../opeo/decompilation/agents/ConstAgent.java | 1 + .../opeo/decompilation/agents/DupAgent.java | 1 + .../decompilation/agents/GetFieldAgent.java | 1 + .../decompilation/agents/GetStaticAgent.java | 1 + .../opeo/decompilation/agents/IfAgent.java | 1 + .../agents/InvokedynamicAgent.java | 1 + .../agents/InvokeinterfaceAgent.java | 1 + .../agents/InvokespecialAgent.java | 1 + .../agents/InvokestaticAgent.java | 1 + .../agents/InvokevirtualAgent.java | 1 + .../opeo/decompilation/agents/LabelAgent.java | 1 + .../opeo/decompilation/agents/LdcAgent.java | 1 + .../opeo/decompilation/agents/LoadAgent.java | 1 + .../opeo/decompilation/agents/MulAgent.java | 2 +- .../opeo/decompilation/agents/NewAgent.java | 1 + .../decompilation/agents/NewArrayAgent.java | 1 + .../opeo/decompilation/agents/PopAgent.java | 1 + .../decompilation/agents/PutFieldAgent.java | 1 + .../decompilation/agents/ReturnAgent.java | 1 + .../opeo/decompilation/agents/StoreAgent.java | 1 + .../agents/StoreToArrayAgent.java | 1 + .../agents/SubstractionAgent.java | 1 + 28 files changed, 78 insertions(+), 48 deletions(-) diff --git a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java index 9bde9ae9..79c32f65 100644 --- a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java +++ b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java @@ -26,10 +26,13 @@ import java.util.Deque; import java.util.Iterator; import java.util.LinkedList; +import java.util.Optional; +import javax.swing.text.html.Option; import lombok.EqualsAndHashCode; import lombok.ToString; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Opcode; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; /** @@ -86,7 +89,9 @@ public boolean hasInstructions() { } public void move() { - this.opcodes.pop(); + if (!this.opcodes.isEmpty()) { + this.opcodes.pop(); + } } /** @@ -94,7 +99,7 @@ public void move() { * @return Current bytecode instruction. */ public Opcode instruction() { - return this.opcodes.peek(); + return Optional.ofNullable(this.opcodes.peek()).orElse(new Opcode(-100)); // final Iterator iterator = this.stack().iterator(); // while (iterator.hasNext()) { // final AstNode next = iterator.next(); diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java index 84b002e6..66a1d070 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java @@ -54,6 +54,7 @@ public void handle(final DecompilerState state) { final AstNode right = state.stack().pop(); final AstNode left = state.stack().pop(); state.stack().push(new Addition(left, right)); + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java index 90ce7dfb..6c793985 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java @@ -59,54 +59,55 @@ public AllAgents(final boolean counting) { this( new MapOf( new MapEntry<>(Opcodes.ICONST_M1, new ConstAgent()), -// new MapEntry<>(Opcodes.ICONST_0, new ConstAgent()), -// new MapEntry<>(Opcodes.ICONST_1, new ConstAgent()), -// new MapEntry<>(Opcodes.ICONST_2, new ConstAgent()), -// new MapEntry<>(Opcodes.ICONST_3, new ConstAgent()), -// new MapEntry<>(Opcodes.ICONST_4, new ConstAgent()), -// new MapEntry<>(Opcodes.ICONST_5, new ConstAgent()), -// new MapEntry<>(Opcodes.LCONST_0, new ConstAgent()), -// new MapEntry<>(Opcodes.LCONST_1, new ConstAgent()), -// new MapEntry<>(Opcodes.FCONST_0, new ConstAgent()), -// new MapEntry<>(Opcodes.FCONST_1, new ConstAgent()), -// new MapEntry<>(Opcodes.FCONST_2, new ConstAgent()), -// new MapEntry<>(Opcodes.DCONST_0, new ConstAgent()), -// new MapEntry<>(Opcodes.DCONST_1, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_0, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_1, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_2, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_3, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_4, new ConstAgent()), + new MapEntry<>(Opcodes.ICONST_5, new ConstAgent()), + new MapEntry<>(Opcodes.LCONST_0, new ConstAgent()), + new MapEntry<>(Opcodes.LCONST_1, new ConstAgent()), + new MapEntry<>(Opcodes.FCONST_0, new ConstAgent()), + new MapEntry<>(Opcodes.FCONST_1, new ConstAgent()), + new MapEntry<>(Opcodes.FCONST_2, new ConstAgent()), + new MapEntry<>(Opcodes.DCONST_0, new ConstAgent()), + new MapEntry<>(Opcodes.DCONST_1, new ConstAgent()), +// new MapEntry<>(Opcodes.IADD, new AddAgent()), -// new MapEntry<>(Opcodes.LADD, new AddAgent()), -// new MapEntry<>(Opcodes.FADD, new AddAgent()), -// new MapEntry<>(Opcodes.DADD, new AddAgent()), + new MapEntry<>(Opcodes.LADD, new AddAgent()), + new MapEntry<>(Opcodes.FADD, new AddAgent()), + new MapEntry<>(Opcodes.DADD, new AddAgent()), new MapEntry<>(Opcodes.ISUB, new SubstractionAgent()), -// new MapEntry<>(Opcodes.LSUB, new SubstractionAgent()), -// new MapEntry<>(Opcodes.FSUB, new SubstractionAgent()), -// new MapEntry<>(Opcodes.DSUB, new SubstractionAgent()), + new MapEntry<>(Opcodes.LSUB, new SubstractionAgent()), + new MapEntry<>(Opcodes.FSUB, new SubstractionAgent()), + new MapEntry<>(Opcodes.DSUB, new SubstractionAgent()), new MapEntry<>(Opcodes.IMUL, new MulAgent()), new MapEntry<>(Opcodes.IF_ICMPGT, new IfAgent()), new MapEntry<>(Opcodes.I2B, new CastAgent()), -// new MapEntry<>(Opcodes.I2C, new CastAgent()), -// new MapEntry<>(Opcodes.I2S, new CastAgent()), -// new MapEntry<>(Opcodes.I2L, new CastAgent()), -// new MapEntry<>(Opcodes.I2F, new CastAgent()), -// new MapEntry<>(Opcodes.I2D, new CastAgent()), -// new MapEntry<>(Opcodes.L2I, new CastAgent()), -// new MapEntry<>(Opcodes.L2F, new CastAgent()), -// new MapEntry<>(Opcodes.L2D, new CastAgent()), -// new MapEntry<>(Opcodes.F2I, new CastAgent()), -// new MapEntry<>(Opcodes.F2L, new CastAgent()), -// new MapEntry<>(Opcodes.F2D, new CastAgent()), -// new MapEntry<>(Opcodes.D2I, new CastAgent()), -// new MapEntry<>(Opcodes.D2L, new CastAgent()), -// new MapEntry<>(Opcodes.D2F, new CastAgent()), + new MapEntry<>(Opcodes.I2C, new CastAgent()), + new MapEntry<>(Opcodes.I2S, new CastAgent()), + new MapEntry<>(Opcodes.I2L, new CastAgent()), + new MapEntry<>(Opcodes.I2F, new CastAgent()), + new MapEntry<>(Opcodes.I2D, new CastAgent()), + new MapEntry<>(Opcodes.L2I, new CastAgent()), + new MapEntry<>(Opcodes.L2F, new CastAgent()), + new MapEntry<>(Opcodes.L2D, new CastAgent()), + new MapEntry<>(Opcodes.F2I, new CastAgent()), + new MapEntry<>(Opcodes.F2L, new CastAgent()), + new MapEntry<>(Opcodes.F2D, new CastAgent()), + new MapEntry<>(Opcodes.D2I, new CastAgent()), + new MapEntry<>(Opcodes.D2L, new CastAgent()), + new MapEntry<>(Opcodes.D2F, new CastAgent()), new MapEntry<>(Opcodes.ILOAD, new LoadAgent()), -// new MapEntry<>(Opcodes.LLOAD, new LoadAgent()), -// new MapEntry<>(Opcodes.FLOAD, new LoadAgent()), -// new MapEntry<>(Opcodes.DLOAD, new LoadAgent()), -// new MapEntry<>(Opcodes.ALOAD, new LoadAgent()), + new MapEntry<>(Opcodes.LLOAD, new LoadAgent()), + new MapEntry<>(Opcodes.FLOAD, new LoadAgent()), + new MapEntry<>(Opcodes.DLOAD, new LoadAgent()), + new MapEntry<>(Opcodes.ALOAD, new LoadAgent()), new MapEntry<>(Opcodes.ISTORE, new StoreAgent()), -// new MapEntry<>(Opcodes.LSTORE, new StoreAgent()), -// new MapEntry<>(Opcodes.FSTORE, new StoreAgent()), -// new MapEntry<>(Opcodes.DSTORE, new StoreAgent()), -// new MapEntry<>(Opcodes.ASTORE, new StoreAgent()), + new MapEntry<>(Opcodes.LSTORE, new StoreAgent()), + new MapEntry<>(Opcodes.FSTORE, new StoreAgent()), + new MapEntry<>(Opcodes.DSTORE, new StoreAgent()), + new MapEntry<>(Opcodes.ASTORE, new StoreAgent()), new MapEntry<>(Opcodes.AASTORE, new StoreToArrayAgent()), new MapEntry<>(Opcodes.ANEWARRAY, new NewArrayAgent()), new MapEntry<>(Opcodes.CHECKCAST, new CheckCastAgent()), @@ -124,8 +125,8 @@ public AllAgents(final boolean counting) { new MapEntry<>(Opcodes.LDC, new LdcAgent()), new MapEntry<>(Opcodes.POP, new PopAgent()), new MapEntry<>(Opcodes.RETURN, new ReturnAgent()), -// new MapEntry<>(Opcodes.IRETURN, new ReturnAgent()), -// new MapEntry<>(Opcodes.ARETURN, new ReturnAgent()), + new MapEntry<>(Opcodes.IRETURN, new ReturnAgent()), + new MapEntry<>(Opcodes.ARETURN, new ReturnAgent()), new MapEntry<>(LabelInstruction.LABEL_OPCODE, new LabelAgent()) // , new MapEntry<>(AllAgents.UNIMPLEMENTED, new UnimplementedAgent(counting)) ) @@ -143,10 +144,9 @@ private AllAgents(final Map agents) { @Override public void handle(final DecompilerState state) { //todo: until? - int hash; while (state.hasInstructions()) { this.agents.values().forEach(agent -> agent.handle(state)); - state.move(); +// state.move(); } // do { // hash = state.hashCode(); diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java index d10e7b8a..6f6fbf53 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java @@ -47,6 +47,7 @@ public final class BipushAgent implements DecompilationAgent { public void handle(final DecompilerState state) { if (BipushAgent.SUPPORTED.contains(state.instruction().opcode())) { state.stack().push(new Literal(state.operand(0))); + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java index dddd6ca1..69f59f95 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java @@ -67,6 +67,7 @@ public void handle(final DecompilerState state) { state.stack().pop() ) ); + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java index e3b036d2..d9423cfb 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java @@ -52,6 +52,7 @@ public void handle(final DecompilerState state) { final AstNode value = state.stack().pop(); final Object type = state.operand(0); state.stack().push(new CheckCast(Type.getObjectType((String) type), value)); + state.move(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java index 663ff2f1..9a47779e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java @@ -92,6 +92,7 @@ public void handle(final DecompilerState state) { state.stack().push(res); break; } + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java index 55a38676..5f03badd 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java @@ -49,6 +49,7 @@ public void handle(final DecompilerState state) { if (DupAgent.SUPPORTED.contains(state.instruction().opcode())) { final OperandStack stack = state.stack(); stack.push(new Duplicate(stack.pop())); + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java index 36b70a57..aa7c309f 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java @@ -51,6 +51,7 @@ public void handle(final DecompilerState state) { .type("field") ) ); + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java index ba4906ba..625d2daf 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java @@ -40,6 +40,7 @@ public void handle(final DecompilerState state) { final String method = (String) state.operand(1); final String descriptor = (String) state.operand(2); state.stack().push(new ClassField(klass, method, descriptor)); + state.move(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java index a63de73e..147de8aa 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java @@ -57,6 +57,7 @@ public void handle(final DecompilerState state) { final AstNode first = stack.pop(); final Label operand = (Label) state.operand(0); stack.push(new If(first, second, operand)); + state.move(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java index 86a78c1c..ce5743f2 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java @@ -54,6 +54,7 @@ public void handle(final DecompilerState state) { args ); state.stack().push(node); + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java index 68eaea92..6d67b13f 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java @@ -73,6 +73,7 @@ public void handle(final DecompilerState state) { args ) ); + state.move(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java index 750afe28..e4f922fa 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java @@ -103,6 +103,7 @@ public void handle(final DecompilerState state) { new Super(target, args, descriptor, type, name) ); } + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java index 22f37284..5be0785e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java @@ -60,6 +60,7 @@ public void handle(final DecompilerState state) { args ) ); + state.move(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java index 906ff714..f80522f6 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java @@ -73,6 +73,7 @@ public void handle(final DecompilerState state) { args ) ); + state.move(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java index 93c985e8..7ec8b291 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java @@ -43,6 +43,7 @@ public void handle(final DecompilerState state) { new Label(String.class.cast(state.operand(0))) ); state.stack().push(node); + state.move(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java index a3e8738c..5d6ac7aa 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java @@ -39,6 +39,7 @@ public void handle(final DecompilerState state) { if (state.instruction().opcode() == Opcodes.LDC) { final Object operand = state.operand(0); state.stack().push(new Constant(operand)); + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java index a2c34f46..337d8afe 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java @@ -59,6 +59,7 @@ public void handle(final DecompilerState state) { state.stack().push( state.variable(index, LoadAgent.type(opcode)) ); + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java index 0f2e391a..d4c2fc38 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java @@ -54,7 +54,7 @@ public void handle(final DecompilerState state) { final AstNode right = state.stack().pop(); final AstNode left = state.stack().pop(); state.stack().push(new Multiplication(left, right)); - + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java index b658ac85..960ea622 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java @@ -48,6 +48,7 @@ public final class NewAgent implements DecompilationAgent { public void handle(final DecompilerState state) { if (NewAgent.SUPPORTED.contains(state.instruction().opcode())) { state.stack().push(new NewAddress(state.operand(0).toString())); + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java index c1668c52..10a43311 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java @@ -54,6 +54,7 @@ public void handle(final DecompilerState state) { final OperandStack stack = state.stack(); final AstNode size = stack.pop(); stack.push(new Reference(new ArrayConstructor(size, type))); + state.move(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java index 7b4f52f2..dae1e70e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java @@ -40,6 +40,7 @@ public void handle(final DecompilerState state) { if (state.instruction().opcode() == Opcodes.POP) { final OperandStack stack = state.stack(); stack.push(new Popped(stack.pop())); + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java index ca8c8f1d..e0cb3ff8 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java @@ -57,6 +57,7 @@ public void handle(final DecompilerState state) { value ) ); + state.move(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java index c1774cff..7c9e85aa 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java @@ -72,6 +72,7 @@ public void handle(final DecompilerState state) { String.format("Unexpected opcode: %d", opcode) ); } + state.move(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java index 30f1d807..5ac3f009 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java @@ -65,6 +65,7 @@ public void handle(final DecompilerState state) { value ) ); + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java index 8734a67b..f8a21019 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java @@ -67,6 +67,7 @@ public void handle(final DecompilerState state) { } catch (final IllegalStateException exception) { state.stack().push(new StoreArray(array, index, value)); } + state.move(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java index ec3dced8..f76f7499 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java @@ -54,6 +54,7 @@ public void handle(final DecompilerState state) { final AstNode right = state.stack().pop(); final AstNode left = state.stack().pop(); state.stack().push(new Substraction(left, right)); + state.move(); } } } From 6fa702ed80ec65515b5ba563d56c1dcff786cbaf Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 12 Aug 2024 15:56:07 +0300 Subject: [PATCH 07/11] feat(#376): fix all unit tests --- .../opeo/decompilation/agents/AllAgents.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java index 6c793985..d6531b5d 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java @@ -72,7 +72,6 @@ public AllAgents(final boolean counting) { new MapEntry<>(Opcodes.FCONST_2, new ConstAgent()), new MapEntry<>(Opcodes.DCONST_0, new ConstAgent()), new MapEntry<>(Opcodes.DCONST_1, new ConstAgent()), -// new MapEntry<>(Opcodes.IADD, new AddAgent()), new MapEntry<>(Opcodes.LADD, new AddAgent()), new MapEntry<>(Opcodes.FADD, new AddAgent()), @@ -127,8 +126,8 @@ public AllAgents(final boolean counting) { new MapEntry<>(Opcodes.RETURN, new ReturnAgent()), new MapEntry<>(Opcodes.IRETURN, new ReturnAgent()), new MapEntry<>(Opcodes.ARETURN, new ReturnAgent()), - new MapEntry<>(LabelInstruction.LABEL_OPCODE, new LabelAgent()) -// , new MapEntry<>(AllAgents.UNIMPLEMENTED, new UnimplementedAgent(counting)) + new MapEntry<>(LabelInstruction.LABEL_OPCODE, new LabelAgent()), + new MapEntry<>(AllAgents.UNIMPLEMENTED, new UnimplementedAgent(counting)) ) ); } @@ -179,7 +178,7 @@ public String[] supportedOpcodes() { * Unimplemented instruction handler. * @since 0.1 */ - private static final class UnimplementedAgent implements DecompilationAgent { + private final static class UnimplementedAgent implements DecompilationAgent { /** * Do we put numbers to opcodes? @@ -196,13 +195,17 @@ private UnimplementedAgent(final boolean counting) { @Override public void handle(final DecompilerState state) { - state.stack().push( - new Opcode( - state.instruction().opcode(), - state.instruction().params(), - this.counting - ) - ); + if (!new AllAgents(false).agents.keySet().contains(state.instruction().opcode())) { + state.stack().push( + new Opcode( + state.instruction().opcode(), + state.instruction().params(), + this.counting + ) + ); + state.move(); + + } } } } From f636ee9f8394627d90c20becb5b49a8b8169bc70 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 12 Aug 2024 16:27:12 +0300 Subject: [PATCH 08/11] feat(#376): add a few puzzles --- .../opeo/decompilation/DecompilerState.java | 61 +++++++++---------- .../opeo/decompilation/agents/AddAgent.java | 2 +- .../opeo/decompilation/agents/AllAgents.java | 30 ++++----- .../decompilation/agents/BipushAgent.java | 2 +- .../opeo/decompilation/agents/CastAgent.java | 35 ++++++++--- .../decompilation/agents/CheckCastAgent.java | 2 +- .../opeo/decompilation/agents/ConstAgent.java | 4 +- .../opeo/decompilation/agents/DupAgent.java | 2 +- .../decompilation/agents/GetFieldAgent.java | 2 +- .../decompilation/agents/GetStaticAgent.java | 2 +- .../opeo/decompilation/agents/IfAgent.java | 2 +- .../agents/InvokedynamicAgent.java | 2 +- .../agents/InvokeinterfaceAgent.java | 2 +- .../agents/InvokespecialAgent.java | 2 +- .../agents/InvokestaticAgent.java | 2 +- .../agents/InvokevirtualAgent.java | 2 +- .../opeo/decompilation/agents/LabelAgent.java | 2 +- .../opeo/decompilation/agents/LdcAgent.java | 2 +- .../opeo/decompilation/agents/LoadAgent.java | 2 +- .../opeo/decompilation/agents/MulAgent.java | 3 +- .../opeo/decompilation/agents/NewAgent.java | 2 +- .../decompilation/agents/NewArrayAgent.java | 3 +- .../opeo/decompilation/agents/PopAgent.java | 2 +- .../decompilation/agents/PutFieldAgent.java | 2 +- .../decompilation/agents/ReturnAgent.java | 2 +- .../opeo/decompilation/agents/StoreAgent.java | 28 ++++++--- .../agents/StoreToArrayAgent.java | 2 +- .../agents/SubstractionAgent.java | 2 +- 28 files changed, 111 insertions(+), 95 deletions(-) diff --git a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java index 79c32f65..6cb1bd10 100644 --- a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java +++ b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java @@ -24,15 +24,12 @@ package org.eolang.opeo.decompilation; import java.util.Deque; -import java.util.Iterator; import java.util.LinkedList; import java.util.Optional; -import javax.swing.text.html.Option; import lombok.EqualsAndHashCode; import lombok.ToString; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Opcode; -import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; /** @@ -44,6 +41,11 @@ @EqualsAndHashCode public final class DecompilerState { + /** + * Remaining opcodes. + * Each method has an original list of opcodes which we decompile. + * When some agent decompiles an instruction, it removes it from this list. + */ private final Deque opcodes; /** @@ -74,6 +76,12 @@ public DecompilerState(final OperandStack operands, final LocalVariables vars) { this(new LinkedList<>(), operands, vars); } + /** + * Constructor. + * @param opcodes Remaining opcodes. + * @param stack Operand stack. + * @param vars Method local variables. + */ public DecompilerState( final Deque opcodes, final OperandStack stack, @@ -84,35 +92,32 @@ public DecompilerState( this.vars = vars; } + /** + * Retrieve current bytecode instruction. + * @return Current bytecode instruction. + */ + public Opcode instruction() { + return Optional.ofNullable(this.opcodes.peek()).orElse(new Opcode(-1)); + } + + /** + * Check if there are any instructions left. + * @return True if there are instructions left. + */ public boolean hasInstructions() { return !this.opcodes.isEmpty(); } - public void move() { + /** + * Remove current instruction from the list. + * This is used when we decompile an instruction. + */ + public void decompileInstruction() { if (!this.opcodes.isEmpty()) { this.opcodes.pop(); } } - /** - * Retrieve current bytecode instruction. - * @return Current bytecode instruction. - */ - public Opcode instruction() { - return Optional.ofNullable(this.opcodes.peek()).orElse(new Opcode(-100)); -// final Iterator iterator = this.stack().iterator(); -// while (iterator.hasNext()) { -// final AstNode next = iterator.next(); -// if (next instanceof Opcode) { -// return (Opcode) next; -// } -// } -// throw new IllegalStateException( -// "No opcode found in the stack" -// ); -// return (Opcode) this.stack().peek(); - } - /** * Instruction operand. * @param index Operand index. @@ -120,16 +125,6 @@ public Opcode instruction() { */ public Object operand(final int index) { return this.instruction().operand(index); -// if (this.current.operands().size() <= index) { -// throw new IllegalStateException( -// String.format( -// "Instruction '%s' doesn't have operand at index '%d'", -// this.current, -// index -// ) -// ); -// } -// return this.current.operand(index); } /** diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java index 66a1d070..66b0064b 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java @@ -54,7 +54,7 @@ public void handle(final DecompilerState state) { final AstNode right = state.stack().pop(); final AstNode left = state.stack().pop(); state.stack().push(new Addition(left, right)); - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java index d6531b5d..79719671 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java @@ -32,12 +32,22 @@ import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; /** * All agents that try to decompile incoming instructions. * @since 0.2 * @checkstyle ClassFanOutComplexityCheck (500 lines) + * @todo #376:90min Decompilation Finish Condition. + * Currently we decompile until we out of instructions. + * But this might be incorrect when we start decompile high-level constructs. + * We should check decompilation stack instead. + * If it changes, we should continue. Otherwise, we should stop. + * The current implementation you can find here {@link #handle(DecompilerState)}. + * @todo #376:90min Unsupported Opcodes. + * Currently we have the ugly map {@link #agents} that contains instructions we can handle. + * We should refactor it to a more elegant solution. + * For example, each agent might provide a list of instructions it can decompile. + * By doing this we can infer the entire list of supported and unsupported instructions. */ public final class AllAgents implements DecompilationAgent { @@ -142,15 +152,9 @@ private AllAgents(final Map agents) { @Override public void handle(final DecompilerState state) { - //todo: until? while (state.hasInstructions()) { this.agents.values().forEach(agent -> agent.handle(state)); -// state.move(); } -// do { -// hash = state.hashCode(); -// } while (hash != state.hashCode()); -// this.agent(state.instruction().opcode()).handle(state); } /** @@ -165,15 +169,6 @@ public String[] supportedOpcodes() { .toArray(String[]::new); } - /** - * Get instruction handler. - * @param opcode Instruction opcode. - * @return Instruction handler. - */ -// private DecompilationAgent agent(final int opcode) { -// return this.agents.getOrDefault(opcode, this.agents.get(AllAgents.UNIMPLEMENTED)); -// } - /** * Unimplemented instruction handler. * @since 0.1 @@ -203,8 +198,7 @@ public void handle(final DecompilerState state) { this.counting ) ); - state.move(); - + state.decompileInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java index 6f6fbf53..3f37884a 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java @@ -47,7 +47,7 @@ public final class BipushAgent implements DecompilationAgent { public void handle(final DecompilerState state) { if (BipushAgent.SUPPORTED.contains(state.instruction().opcode())) { state.stack().push(new Literal(state.operand(0))); - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java index 69f59f95..42dba666 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java @@ -27,8 +27,8 @@ import java.util.HashSet; import java.util.Set; import org.eolang.opeo.ast.Cast; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Type; /** @@ -37,6 +37,9 @@ */ public final class CastAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPROTED = new HashSet<>( Arrays.asList( org.objectweb.asm.Opcodes.I2B, @@ -67,34 +70,47 @@ public void handle(final DecompilerState state) { state.stack().pop() ) ); - state.move(); + state.decompileInstruction(); } } + /** + * Target type. + * @param opcode Opcode to handle. + * @return Target type. + */ private static Type target(final int opcode) { + final Type result; switch (opcode) { case org.objectweb.asm.Opcodes.I2B: - return Type.BYTE_TYPE; + result = Type.BYTE_TYPE; + break; case org.objectweb.asm.Opcodes.I2C: - return Type.CHAR_TYPE; + result = Type.CHAR_TYPE; + break; case org.objectweb.asm.Opcodes.I2S: - return Type.SHORT_TYPE; + result = Type.SHORT_TYPE; + break; case org.objectweb.asm.Opcodes.I2L: case org.objectweb.asm.Opcodes.F2L: case org.objectweb.asm.Opcodes.D2L: - return Type.LONG_TYPE; + result = Type.LONG_TYPE; + break; case org.objectweb.asm.Opcodes.I2F: case org.objectweb.asm.Opcodes.L2F: case org.objectweb.asm.Opcodes.D2F: - return Type.FLOAT_TYPE; + result = Type.FLOAT_TYPE; + break; case org.objectweb.asm.Opcodes.I2D: case org.objectweb.asm.Opcodes.L2D: case org.objectweb.asm.Opcodes.F2D: - return Type.DOUBLE_TYPE; + result = Type.DOUBLE_TYPE; + break; case org.objectweb.asm.Opcodes.L2I: case org.objectweb.asm.Opcodes.F2I: case org.objectweb.asm.Opcodes.D2I: - return Type.INT_TYPE; + result = Type.INT_TYPE; + break; default: throw new IllegalArgumentException( String.format( @@ -103,5 +119,6 @@ private static Type target(final int opcode) { ) ); } + return result; } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java index d9423cfb..479be296 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java @@ -52,7 +52,7 @@ public void handle(final DecompilerState state) { final AstNode value = state.stack().pop(); final Object type = state.operand(0); state.stack().push(new CheckCast(Type.getObjectType((String) type), value)); - state.move(); + state.decompileInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java index 9a47779e..44eaf172 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java @@ -26,13 +26,11 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; -import org.eolang.opeo.SelectiveDecompiler; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Literal; import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; /** * Iconst instruction handler. @@ -92,7 +90,7 @@ public void handle(final DecompilerState state) { state.stack().push(res); break; } - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java index 5f03badd..6388830e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java @@ -49,7 +49,7 @@ public void handle(final DecompilerState state) { if (DupAgent.SUPPORTED.contains(state.instruction().opcode())) { final OperandStack stack = state.stack(); stack.push(new Duplicate(stack.pop())); - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java index aa7c309f..8b3d3f71 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java @@ -51,7 +51,7 @@ public void handle(final DecompilerState state) { .type("field") ) ); - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java index 625d2daf..b3d47a72 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java @@ -40,7 +40,7 @@ public void handle(final DecompilerState state) { final String method = (String) state.operand(1); final String descriptor = (String) state.operand(2); state.stack().push(new ClassField(klass, method, descriptor)); - state.move(); + state.decompileInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java index 147de8aa..5f15a75f 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java @@ -57,7 +57,7 @@ public void handle(final DecompilerState state) { final AstNode first = stack.pop(); final Label operand = (Label) state.operand(0); stack.push(new If(first, second, operand)); - state.move(); + state.decompileInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java index ce5743f2..5e465e2e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java @@ -54,7 +54,7 @@ public void handle(final DecompilerState state) { args ); state.stack().push(node); - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java index 6d67b13f..4f598c4e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java @@ -73,7 +73,7 @@ public void handle(final DecompilerState state) { args ) ); - state.move(); + state.decompileInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java index e4f922fa..ab8438e7 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java @@ -103,7 +103,7 @@ public void handle(final DecompilerState state) { new Super(target, args, descriptor, type, name) ); } - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java index 5be0785e..27c4b28d 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java @@ -60,7 +60,7 @@ public void handle(final DecompilerState state) { args ) ); - state.move(); + state.decompileInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java index f80522f6..28f8e71a 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java @@ -73,7 +73,7 @@ public void handle(final DecompilerState state) { args ) ); - state.move(); + state.decompileInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java index 7ec8b291..9f22868e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java @@ -43,7 +43,7 @@ public void handle(final DecompilerState state) { new Label(String.class.cast(state.operand(0))) ); state.stack().push(node); - state.move(); + state.decompileInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java index 5d6ac7aa..d8dc0081 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java @@ -39,7 +39,7 @@ public void handle(final DecompilerState state) { if (state.instruction().opcode() == Opcodes.LDC) { final Object operand = state.operand(0); state.stack().push(new Constant(operand)); - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java index 337d8afe..019cb326 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java @@ -59,7 +59,7 @@ public void handle(final DecompilerState state) { state.stack().push( state.variable(index, LoadAgent.type(opcode)) ); - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java index d4c2fc38..7c135614 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java @@ -28,7 +28,6 @@ import java.util.Set; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Multiplication; -import org.eolang.opeo.ast.Opcode; import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; @@ -54,7 +53,7 @@ public void handle(final DecompilerState state) { final AstNode right = state.stack().pop(); final AstNode left = state.stack().pop(); state.stack().push(new Multiplication(left, right)); - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java index 960ea622..1b63ebdd 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java @@ -48,7 +48,7 @@ public final class NewAgent implements DecompilationAgent { public void handle(final DecompilerState state) { if (NewAgent.SUPPORTED.contains(state.instruction().opcode())) { state.stack().push(new NewAddress(state.operand(0).toString())); - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java index 10a43311..4d99aa08 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java @@ -26,7 +26,6 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; -import org.eolang.opeo.SelectiveDecompiler; import org.eolang.opeo.ast.ArrayConstructor; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Reference; @@ -54,7 +53,7 @@ public void handle(final DecompilerState state) { final OperandStack stack = state.stack(); final AstNode size = stack.pop(); stack.push(new Reference(new ArrayConstructor(size, type))); - state.move(); + state.decompileInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java index dae1e70e..9bb5bf5b 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java @@ -40,7 +40,7 @@ public void handle(final DecompilerState state) { if (state.instruction().opcode() == Opcodes.POP) { final OperandStack stack = state.stack(); stack.push(new Popped(stack.pop())); - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java index e0cb3ff8..12c7fa1b 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java @@ -57,7 +57,7 @@ public void handle(final DecompilerState state) { value ) ); - state.move(); + state.decompileInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java index 7c9e85aa..98acb9b5 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java @@ -72,7 +72,7 @@ public void handle(final DecompilerState state) { String.format("Unexpected opcode: %d", opcode) ); } - state.move(); + state.decompileInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java index 5ac3f009..7997e727 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java @@ -42,6 +42,9 @@ */ public final class StoreAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.ISTORE, @@ -65,7 +68,7 @@ public void handle(final DecompilerState state) { value ) ); - state.move(); + state.decompileInstruction(); } } @@ -86,23 +89,34 @@ private Type infer(final AstNode value, final int opcode) { return result; } - + /** + * Infer type from opcode. + * @param opcode Opcode. + * @return Inferred type. + */ private static Type type(final int opcode) { + final Type result; switch (opcode) { case Opcodes.ISTORE: - return Type.INT_TYPE; + result = Type.INT_TYPE; + break; case Opcodes.LSTORE: - return Type.LONG_TYPE; + result = Type.LONG_TYPE; + break; case Opcodes.FSTORE: - return Type.FLOAT_TYPE; + result = Type.FLOAT_TYPE; + break; case Opcodes.DSTORE: - return Type.DOUBLE_TYPE; + result = Type.DOUBLE_TYPE; + break; case Opcodes.ASTORE: - return Type.getType(Object.class); + result = Type.getType(Object.class); + break; default: throw new IllegalArgumentException( String.format("Unsupported opcode: %d", opcode) ); } + return result; } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java index f8a21019..81c3b32f 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java @@ -67,7 +67,7 @@ public void handle(final DecompilerState state) { } catch (final IllegalStateException exception) { state.stack().push(new StoreArray(array, index, value)); } - state.move(); + state.decompileInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java index f76f7499..66109407 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java @@ -54,7 +54,7 @@ public void handle(final DecompilerState state) { final AstNode right = state.stack().pop(); final AstNode left = state.stack().pop(); state.stack().push(new Substraction(left, right)); - state.move(); + state.decompileInstruction(); } } } From e1c3237af560de116e1bc0f7510014f92c3c89c1 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 12 Aug 2024 16:39:03 +0300 Subject: [PATCH 09/11] feat(#376): fix several code offences --- .../opeo/decompilation/DecompilerState.java | 2 +- .../opeo/decompilation/OperandStack.java | 2 - .../opeo/decompilation/agents/AddAgent.java | 2 +- .../opeo/decompilation/agents/AllAgents.java | 4 +- .../decompilation/agents/BipushAgent.java | 2 +- .../opeo/decompilation/agents/CastAgent.java | 64 ++++++++++--------- .../decompilation/agents/CheckCastAgent.java | 7 +- .../opeo/decompilation/agents/ConstAgent.java | 2 +- .../opeo/decompilation/agents/DupAgent.java | 2 +- .../decompilation/agents/GetFieldAgent.java | 2 +- .../decompilation/agents/GetStaticAgent.java | 2 +- .../opeo/decompilation/agents/IfAgent.java | 2 +- .../agents/InvokedynamicAgent.java | 2 +- .../agents/InvokeinterfaceAgent.java | 2 +- .../agents/InvokespecialAgent.java | 2 +- .../agents/InvokestaticAgent.java | 2 +- .../agents/InvokevirtualAgent.java | 2 +- .../opeo/decompilation/agents/LabelAgent.java | 2 +- .../opeo/decompilation/agents/LdcAgent.java | 2 +- .../opeo/decompilation/agents/LoadAgent.java | 27 ++++++-- .../opeo/decompilation/agents/MulAgent.java | 2 +- .../opeo/decompilation/agents/NewAgent.java | 2 +- .../decompilation/agents/NewArrayAgent.java | 2 +- .../opeo/decompilation/agents/PopAgent.java | 4 +- .../decompilation/agents/PutFieldAgent.java | 2 +- .../decompilation/agents/ReturnAgent.java | 2 +- .../opeo/decompilation/agents/StoreAgent.java | 2 +- .../agents/StoreToArrayAgent.java | 2 +- .../agents/SubstractionAgent.java | 2 +- .../eolang/opeo/ast/ArrayConstructorTest.java | 8 ++- .../decompilation/DecompilerMachineTest.java | 1 - 31 files changed, 93 insertions(+), 70 deletions(-) diff --git a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java index 6cb1bd10..8f832f0c 100644 --- a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java +++ b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java @@ -112,7 +112,7 @@ public boolean hasInstructions() { * Remove current instruction from the list. * This is used when we decompile an instruction. */ - public void decompileInstruction() { + public void popInstruction() { if (!this.opcodes.isEmpty()) { this.opcodes.pop(); } diff --git a/src/main/java/org/eolang/opeo/decompilation/OperandStack.java b/src/main/java/org/eolang/opeo/decompilation/OperandStack.java index da100ddd..37ebdfcd 100644 --- a/src/main/java/org/eolang/opeo/decompilation/OperandStack.java +++ b/src/main/java/org/eolang/opeo/decompilation/OperandStack.java @@ -24,8 +24,6 @@ package org.eolang.opeo.decompilation; import java.util.ArrayDeque; -import java.util.Collection; -import java.util.Collections; import java.util.Deque; import java.util.Iterator; import java.util.LinkedList; diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java index 66b0064b..072b8442 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java @@ -54,7 +54,7 @@ public void handle(final DecompilerState state) { final AstNode right = state.stack().pop(); final AstNode left = state.stack().pop(); state.stack().push(new Addition(left, right)); - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java index 79719671..248a37b6 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java @@ -173,7 +173,7 @@ public String[] supportedOpcodes() { * Unimplemented instruction handler. * @since 0.1 */ - private final static class UnimplementedAgent implements DecompilationAgent { + private static final class UnimplementedAgent implements DecompilationAgent { /** * Do we put numbers to opcodes? @@ -198,7 +198,7 @@ public void handle(final DecompilerState state) { this.counting ) ); - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java index 3f37884a..209edbaa 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java @@ -47,7 +47,7 @@ public final class BipushAgent implements DecompilationAgent { public void handle(final DecompilerState state) { if (BipushAgent.SUPPORTED.contains(state.instruction().opcode())) { state.stack().push(new Literal(state.operand(0))); - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java index 42dba666..72f77967 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java @@ -29,6 +29,7 @@ import org.eolang.opeo.ast.Cast; import org.eolang.opeo.decompilation.DecompilationAgent; import org.eolang.opeo.decompilation.DecompilerState; +import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; /** @@ -42,21 +43,21 @@ public final class CastAgent implements DecompilationAgent { */ private static final Set SUPPROTED = new HashSet<>( Arrays.asList( - org.objectweb.asm.Opcodes.I2B, - org.objectweb.asm.Opcodes.I2C, - org.objectweb.asm.Opcodes.I2S, - org.objectweb.asm.Opcodes.I2L, - org.objectweb.asm.Opcodes.I2F, - org.objectweb.asm.Opcodes.I2D, - org.objectweb.asm.Opcodes.L2I, - org.objectweb.asm.Opcodes.L2F, - org.objectweb.asm.Opcodes.L2D, - org.objectweb.asm.Opcodes.F2I, - org.objectweb.asm.Opcodes.F2L, - org.objectweb.asm.Opcodes.F2D, - org.objectweb.asm.Opcodes.D2I, - org.objectweb.asm.Opcodes.D2L, - org.objectweb.asm.Opcodes.D2F + Opcodes.I2B, + Opcodes.I2C, + Opcodes.I2S, + Opcodes.I2L, + Opcodes.I2F, + Opcodes.I2D, + Opcodes.L2I, + Opcodes.L2F, + Opcodes.L2D, + Opcodes.F2I, + Opcodes.F2L, + Opcodes.F2D, + Opcodes.D2I, + Opcodes.D2L, + Opcodes.D2F ) ); @@ -70,7 +71,7 @@ public void handle(final DecompilerState state) { state.stack().pop() ) ); - state.decompileInstruction(); + state.popInstruction(); } } @@ -78,37 +79,38 @@ public void handle(final DecompilerState state) { * Target type. * @param opcode Opcode to handle. * @return Target type. + * @checkstyle CyclomaticComplexityCheck (100 lines) */ private static Type target(final int opcode) { final Type result; switch (opcode) { - case org.objectweb.asm.Opcodes.I2B: + case Opcodes.I2B: result = Type.BYTE_TYPE; break; - case org.objectweb.asm.Opcodes.I2C: + case Opcodes.I2C: result = Type.CHAR_TYPE; break; - case org.objectweb.asm.Opcodes.I2S: + case Opcodes.I2S: result = Type.SHORT_TYPE; break; - case org.objectweb.asm.Opcodes.I2L: - case org.objectweb.asm.Opcodes.F2L: - case org.objectweb.asm.Opcodes.D2L: + case Opcodes.I2L: + case Opcodes.F2L: + case Opcodes.D2L: result = Type.LONG_TYPE; break; - case org.objectweb.asm.Opcodes.I2F: - case org.objectweb.asm.Opcodes.L2F: - case org.objectweb.asm.Opcodes.D2F: + case Opcodes.I2F: + case Opcodes.L2F: + case Opcodes.D2F: result = Type.FLOAT_TYPE; break; - case org.objectweb.asm.Opcodes.I2D: - case org.objectweb.asm.Opcodes.L2D: - case org.objectweb.asm.Opcodes.F2D: + case Opcodes.I2D: + case Opcodes.L2D: + case Opcodes.F2D: result = Type.DOUBLE_TYPE; break; - case org.objectweb.asm.Opcodes.L2I: - case org.objectweb.asm.Opcodes.F2I: - case org.objectweb.asm.Opcodes.D2I: + case Opcodes.L2I: + case Opcodes.F2I: + case Opcodes.D2I: result = Type.INT_TYPE; break; default: diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java index 479be296..31cceeaf 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/CheckCastAgent.java @@ -28,8 +28,8 @@ import java.util.Set; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.CheckCast; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; @@ -40,6 +40,9 @@ */ public final class CheckCastAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.CHECKCAST @@ -52,7 +55,7 @@ public void handle(final DecompilerState state) { final AstNode value = state.stack().pop(); final Object type = state.operand(0); state.stack().push(new CheckCast(Type.getObjectType((String) type), value)); - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java index 44eaf172..4ac77a76 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java @@ -90,7 +90,7 @@ public void handle(final DecompilerState state) { state.stack().push(res); break; } - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java index 6388830e..d2748c1a 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java @@ -49,7 +49,7 @@ public void handle(final DecompilerState state) { if (DupAgent.SUPPORTED.contains(state.instruction().opcode())) { final OperandStack stack = state.stack(); stack.push(new Duplicate(stack.pop())); - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java index 8b3d3f71..40dc496b 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java @@ -51,7 +51,7 @@ public void handle(final DecompilerState state) { .type("field") ) ); - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java index b3d47a72..f3cd1d56 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java @@ -40,7 +40,7 @@ public void handle(final DecompilerState state) { final String method = (String) state.operand(1); final String descriptor = (String) state.operand(2); state.stack().push(new ClassField(klass, method, descriptor)); - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java index 5f15a75f..9f8cc430 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java @@ -57,7 +57,7 @@ public void handle(final DecompilerState state) { final AstNode first = stack.pop(); final Label operand = (Label) state.operand(0); stack.push(new If(first, second, operand)); - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java index 5e465e2e..8db9906e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java @@ -54,7 +54,7 @@ public void handle(final DecompilerState state) { args ); state.stack().push(node); - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java index 4f598c4e..43823505 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokeinterfaceAgent.java @@ -73,7 +73,7 @@ public void handle(final DecompilerState state) { args ) ); - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java index ab8438e7..f2529eb9 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java @@ -103,7 +103,7 @@ public void handle(final DecompilerState state) { new Super(target, args, descriptor, type, name) ); } - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java index 27c4b28d..1cdbe835 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java @@ -60,7 +60,7 @@ public void handle(final DecompilerState state) { args ) ); - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java index 28f8e71a..3be53879 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java @@ -73,7 +73,7 @@ public void handle(final DecompilerState state) { args ) ); - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java index 9f22868e..d9190d3c 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java @@ -43,7 +43,7 @@ public void handle(final DecompilerState state) { new Label(String.class.cast(state.operand(0))) ); state.stack().push(node); - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java index d8dc0081..e255084c 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java @@ -39,7 +39,7 @@ public void handle(final DecompilerState state) { if (state.instruction().opcode() == Opcodes.LDC) { final Object operand = state.operand(0); state.stack().push(new Constant(operand)); - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java index 019cb326..54d00d02 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java @@ -41,6 +41,9 @@ */ public final class LoadAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.ILOAD, @@ -59,26 +62,38 @@ public void handle(final DecompilerState state) { state.stack().push( state.variable(index, LoadAgent.type(opcode)) ); - state.decompileInstruction(); + state.popInstruction(); } } + /** + * Infer type from opcode. + * @param opcode Opcode + * @return Type + */ private static Type type(final int opcode) { + final Type result; switch (opcode) { case Opcodes.ILOAD: - return Type.INT_TYPE; + result = Type.INT_TYPE; + break; case Opcodes.LLOAD: - return Type.LONG_TYPE; + result = Type.LONG_TYPE; + break; case Opcodes.FLOAD: - return Type.FLOAT_TYPE; + result = Type.FLOAT_TYPE; + break; case Opcodes.DLOAD: - return Type.DOUBLE_TYPE; + result = Type.DOUBLE_TYPE; + break; case Opcodes.ALOAD: - return Type.getType(Object.class); + result = Type.getType(Object.class); + break; default: throw new IllegalArgumentException( String.format("Unsupported opcode: %d", opcode) ); } + return result; } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java index 7c135614..00b03b7b 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java @@ -53,7 +53,7 @@ public void handle(final DecompilerState state) { final AstNode right = state.stack().pop(); final AstNode left = state.stack().pop(); state.stack().push(new Multiplication(left, right)); - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java index 1b63ebdd..f27415ca 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java @@ -48,7 +48,7 @@ public final class NewAgent implements DecompilationAgent { public void handle(final DecompilerState state) { if (NewAgent.SUPPORTED.contains(state.instruction().opcode())) { state.stack().push(new NewAddress(state.operand(0).toString())); - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java index 4d99aa08..eed9bcac 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java @@ -53,7 +53,7 @@ public void handle(final DecompilerState state) { final OperandStack stack = state.stack(); final AstNode size = stack.pop(); stack.push(new Reference(new ArrayConstructor(size, type))); - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java index 9bb5bf5b..3ae38915 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PopAgent.java @@ -24,8 +24,8 @@ package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.Popped; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.OperandStack; import org.objectweb.asm.Opcodes; @@ -40,7 +40,7 @@ public void handle(final DecompilerState state) { if (state.instruction().opcode() == Opcodes.POP) { final OperandStack stack = state.stack(); stack.push(new Popped(stack.pop())); - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java index 12c7fa1b..06dda56a 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java @@ -57,7 +57,7 @@ public void handle(final DecompilerState state) { value ) ); - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java index 98acb9b5..37cb27ac 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java @@ -72,7 +72,7 @@ public void handle(final DecompilerState state) { String.format("Unexpected opcode: %d", opcode) ); } - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java index 7997e727..4e0f1d0b 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java @@ -68,7 +68,7 @@ public void handle(final DecompilerState state) { value ) ); - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java index 81c3b32f..4c621de7 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java @@ -67,7 +67,7 @@ public void handle(final DecompilerState state) { } catch (final IllegalStateException exception) { state.stack().push(new StoreArray(array, index, value)); } - state.decompileInstruction(); + state.popInstruction(); } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java index 66109407..781fdd0e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java @@ -54,7 +54,7 @@ public void handle(final DecompilerState state) { final AstNode right = state.stack().pop(); final AstNode left = state.stack().pop(); state.stack().push(new Substraction(left, right)); - state.decompileInstruction(); + state.popInstruction(); } } } diff --git a/src/test/java/org/eolang/opeo/ast/ArrayConstructorTest.java b/src/test/java/org/eolang/opeo/ast/ArrayConstructorTest.java index dfbaa515..eaf6bae8 100644 --- a/src/test/java/org/eolang/opeo/ast/ArrayConstructorTest.java +++ b/src/test/java/org/eolang/opeo/ast/ArrayConstructorTest.java @@ -62,7 +62,13 @@ void createsArrayConstructorFromXmir() { node -> new Addition(new Literal(1), new Literal(2)) ), Matchers.equalTo( - new ArrayConstructor(new Addition(new Literal(1), new Literal(2)), "java/lang/Integer") + new ArrayConstructor( + new Addition( + new Literal(1), + new Literal(2) + ), + "java/lang/Integer" + ) ) ); } diff --git a/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java b/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java index c7b61c16..1db3a020 100644 --- a/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java +++ b/src/test/java/org/eolang/opeo/decompilation/DecompilerMachineTest.java @@ -23,7 +23,6 @@ */ package org.eolang.opeo.decompilation; -import com.jcabi.xml.XMLDocument; import java.util.Collections; import java.util.concurrent.atomic.AtomicReference; import org.eolang.jeo.matchers.SameXml; From 7253c752475ee28350e09b52d9681000084a904c Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 12 Aug 2024 16:50:56 +0300 Subject: [PATCH 10/11] feat(#376): fix all qulice suggestions --- .../opeo/decompilation/DecompilerState.java | 6 +++--- .../opeo/decompilation/agents/AddAgent.java | 5 ++++- .../opeo/decompilation/agents/AllAgents.java | 4 ++-- .../opeo/decompilation/agents/BipushAgent.java | 5 ++++- .../opeo/decompilation/agents/ConstAgent.java | 18 ++++++++++++++---- .../opeo/decompilation/agents/DupAgent.java | 5 ++++- .../decompilation/agents/GetFieldAgent.java | 2 +- .../decompilation/agents/GetStaticAgent.java | 2 +- .../opeo/decompilation/agents/IfAgent.java | 5 ++++- .../agents/InvokedynamicAgent.java | 2 +- .../agents/InvokespecialAgent.java | 2 +- .../agents/InvokestaticAgent.java | 2 +- .../agents/InvokevirtualAgent.java | 2 +- .../opeo/decompilation/agents/LabelAgent.java | 2 +- .../opeo/decompilation/agents/LdcAgent.java | 2 +- .../opeo/decompilation/agents/LoadAgent.java | 2 +- .../opeo/decompilation/agents/MulAgent.java | 3 +++ .../opeo/decompilation/agents/NewAgent.java | 6 ++++-- .../decompilation/agents/NewArrayAgent.java | 5 ++++- .../decompilation/agents/PutFieldAgent.java | 2 +- .../opeo/decompilation/agents/ReturnAgent.java | 9 ++++++--- .../opeo/decompilation/agents/StoreAgent.java | 8 ++++---- .../agents/StoreToArrayAgent.java | 5 ++++- .../agents/SubstractionAgent.java | 5 ++++- 24 files changed, 74 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java index 8f832f0c..09c7b512 100644 --- a/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java +++ b/src/main/java/org/eolang/opeo/decompilation/DecompilerState.java @@ -52,7 +52,7 @@ public final class DecompilerState { * Current operand stack. * It might have some values inside. */ - private final OperandStack stack; + private final OperandStack ostack; /** * Method local variables. @@ -88,7 +88,7 @@ public DecompilerState( final LocalVariables vars ) { this.opcodes = opcodes; - this.stack = stack; + this.ostack = stack; this.vars = vars; } @@ -142,6 +142,6 @@ public AstNode variable(final int index, final Type type) { * @return Operand stack. */ public OperandStack stack() { - return this.stack; + return this.ostack; } } diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java index 072b8442..e1798516 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AddAgent.java @@ -28,8 +28,8 @@ import java.util.Set; import org.eolang.opeo.ast.Addition; import org.eolang.opeo.ast.AstNode; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; /** @@ -38,6 +38,9 @@ */ public final class AddAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.IADD, diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java index 248a37b6..792200ee 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/AllAgents.java @@ -29,14 +29,13 @@ import org.eolang.opeo.LabelInstruction; import org.eolang.opeo.ast.Opcode; import org.eolang.opeo.ast.OpcodeName; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; /** * All agents that try to decompile incoming instructions. * @since 0.2 - * @checkstyle ClassFanOutComplexityCheck (500 lines) * @todo #376:90min Decompilation Finish Condition. * Currently we decompile until we out of instructions. * But this might be incorrect when we start decompile high-level constructs. @@ -48,6 +47,7 @@ * We should refactor it to a more elegant solution. * For example, each agent might provide a list of instructions it can decompile. * By doing this we can infer the entire list of supported and unsupported instructions. + * @checkstyle ClassFanOutComplexityCheck (500 lines) */ public final class AllAgents implements DecompilationAgent { diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java index 209edbaa..ab3cc69f 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/BipushAgent.java @@ -27,8 +27,8 @@ import java.util.HashSet; import java.util.Set; import org.eolang.opeo.ast.Literal; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; /** @@ -37,6 +37,9 @@ */ public final class BipushAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.BIPUSH diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java index 4ac77a76..d514db5d 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ConstAgent.java @@ -28,17 +28,21 @@ import java.util.Set; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Literal; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; /** * Iconst instruction handler. * @since 0.1 + * @checkstyle CyclomaticComplexityCheck (500 lines) */ public final class ConstAgent implements DecompilationAgent { - private final Set SUPPORTED = new HashSet<>( + /** + * Supported opcodes. + */ + private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.ICONST_M1, Opcodes.ICONST_0, @@ -60,7 +64,7 @@ public final class ConstAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { final int opcode = state.instruction().opcode(); - if (this.SUPPORTED.contains(opcode)) { + if (ConstAgent.SUPPORTED.contains(opcode)) { final AstNode res; switch (opcode) { case Opcodes.ICONST_M1: @@ -89,10 +93,16 @@ public void handle(final DecompilerState state) { res = ConstAgent.doubleConstant(opcode); state.stack().push(res); break; + default: + throw new UnsupportedOperationException( + String.format( + "Constant handler for opcode %s is not supported yet", + opcode + ) + ); } state.popInstruction(); } - } /** diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java index d2748c1a..dc903f5b 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/DupAgent.java @@ -27,8 +27,8 @@ import java.util.HashSet; import java.util.Set; import org.eolang.opeo.ast.Duplicate; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.OperandStack; import org.objectweb.asm.Opcodes; @@ -38,6 +38,9 @@ */ public final class DupAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.DUP diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java index 40dc496b..18a36e73 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetFieldAgent.java @@ -25,8 +25,8 @@ import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.FieldRetrieval; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; /** diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java index f3cd1d56..242708b8 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/GetStaticAgent.java @@ -24,8 +24,8 @@ package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.ClassField; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; /** diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java index 9f8cc430..45113d3a 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/IfAgent.java @@ -28,8 +28,8 @@ import java.util.Set; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.If; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.OperandStack; import org.objectweb.asm.Label; import org.objectweb.asm.Opcodes; @@ -43,6 +43,9 @@ */ public final class IfAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.IF_ICMPGT diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java index 8db9906e..6d85fb82 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokedynamicAgent.java @@ -27,8 +27,8 @@ import java.util.List; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.DynamicInvocation; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Handle; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java index f2529eb9..71be3aee 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokespecialAgent.java @@ -33,8 +33,8 @@ import org.eolang.opeo.ast.NewAddress; import org.eolang.opeo.ast.Super; import org.eolang.opeo.ast.This; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java index 1cdbe835..63230793 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokestaticAgent.java @@ -29,8 +29,8 @@ import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.Owner; import org.eolang.opeo.ast.StaticInvocation; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java index 3be53879..f1d8758d 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/InvokevirtualAgent.java @@ -28,8 +28,8 @@ import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.Invocation; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java index d9190d3c..f38ce3a8 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LabelAgent.java @@ -27,8 +27,8 @@ import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Label; import org.eolang.opeo.ast.Labeled; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; /** * Label instruction handler. diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java index e255084c..dbc5fb42 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LdcAgent.java @@ -24,8 +24,8 @@ package org.eolang.opeo.decompilation.agents; import org.eolang.opeo.ast.Constant; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; /** diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java index 54d00d02..43a4c376 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java @@ -26,8 +26,8 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java index 00b03b7b..c1814c4a 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/MulAgent.java @@ -38,6 +38,9 @@ */ public final class MulAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.IMUL, diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java index f27415ca..4c8242ad 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewAgent.java @@ -27,8 +27,8 @@ import java.util.HashSet; import java.util.Set; import org.eolang.opeo.ast.NewAddress; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; /** @@ -37,13 +37,15 @@ */ public final class NewAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.NEW ) ); - @Override public void handle(final DecompilerState state) { if (NewAgent.SUPPORTED.contains(state.instruction().opcode())) { diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java index eed9bcac..4825c7eb 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/NewArrayAgent.java @@ -29,8 +29,8 @@ import org.eolang.opeo.ast.ArrayConstructor; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Reference; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.OperandStack; import org.objectweb.asm.Opcodes; @@ -40,6 +40,9 @@ */ public final class NewArrayAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.ANEWARRAY diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java index 06dda56a..49674109 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/PutFieldAgent.java @@ -27,8 +27,8 @@ import org.eolang.opeo.ast.Attributes; import org.eolang.opeo.ast.Field; import org.eolang.opeo.ast.FieldAssignment; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; /** diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java index 37cb27ac..7d6a2a9f 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/ReturnAgent.java @@ -27,8 +27,8 @@ import java.util.HashSet; import java.util.Set; import org.eolang.opeo.ast.Return; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.OperandStack; import org.objectweb.asm.Opcodes; @@ -39,7 +39,10 @@ */ public final class ReturnAgent implements DecompilationAgent { - private final Set SUPPORTED = new HashSet<>( + /** + * Supported opcodes. + */ + private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.RETURN, Opcodes.IRETURN, @@ -53,7 +56,7 @@ public final class ReturnAgent implements DecompilationAgent { @Override public void handle(final DecompilerState state) { final int opcode = state.instruction().opcode(); - if (this.SUPPORTED.contains(opcode)) { + if (ReturnAgent.SUPPORTED.contains(opcode)) { final OperandStack stack = state.stack(); if (opcode == Opcodes.RETURN) { stack.push(new Return()); diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java index 4e0f1d0b..031ff32e 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreAgent.java @@ -30,8 +30,8 @@ import org.eolang.opeo.ast.LocalVariable; import org.eolang.opeo.ast.Typed; import org.eolang.opeo.ast.VariableAssignment; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.OperandStack; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; @@ -64,13 +64,13 @@ public void handle(final DecompilerState state) { stack.push( new VariableAssignment( (LocalVariable) state.variable( - (Integer) state.operand(0), this.infer(value, opcode)), + (Integer) state.operand(0), StoreAgent.infer(value, opcode) + ), value ) ); state.popInstruction(); } - } /** @@ -79,7 +79,7 @@ public void handle(final DecompilerState state) { * @param opcode Opcode. * @return Inferred type. */ - private Type infer(final AstNode value, final int opcode) { + private static Type infer(final AstNode value, final int opcode) { final Type result; if (value instanceof Typed) { result = ((Typed) value).type(); diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java index 4c621de7..b94fa92f 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/StoreToArrayAgent.java @@ -32,8 +32,8 @@ import org.eolang.opeo.ast.Labeled; import org.eolang.opeo.ast.Reference; import org.eolang.opeo.ast.StoreArray; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; /** * Store to array instruction handler. @@ -48,6 +48,9 @@ */ public final class StoreToArrayAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( org.objectweb.asm.Opcodes.AASTORE diff --git a/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java index 781fdd0e..b17c1402 100644 --- a/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java +++ b/src/main/java/org/eolang/opeo/decompilation/agents/SubstractionAgent.java @@ -28,8 +28,8 @@ import java.util.Set; import org.eolang.opeo.ast.AstNode; import org.eolang.opeo.ast.Substraction; -import org.eolang.opeo.decompilation.DecompilerState; import org.eolang.opeo.decompilation.DecompilationAgent; +import org.eolang.opeo.decompilation.DecompilerState; import org.objectweb.asm.Opcodes; /** @@ -38,6 +38,9 @@ */ public final class SubstractionAgent implements DecompilationAgent { + /** + * Supported opcodes. + */ private static final Set SUPPORTED = new HashSet<>( Arrays.asList( Opcodes.ISUB, From d602f5c2f0aa7f0a961a38b657f57d53cc692b31 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Mon, 12 Aug 2024 16:53:42 +0300 Subject: [PATCH 11/11] feat(#376): change jacoco limits --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 0e0185fd..26d10dda 100644 --- a/pom.xml +++ b/pom.xml @@ -337,7 +337,7 @@ SOFTWARE. CLASS MISSEDCOUNT - 7 + 8