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")) ); }