Skip to content

Commit

Permalink
feat(objectionary#376): fix several code offences
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Aug 12, 2024
1 parent f636ee9 commit e1c3237
Show file tree
Hide file tree
Showing 31 changed files with 93 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/eolang/opeo/decompilation/OperandStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -198,7 +198,7 @@ public void handle(final DecompilerState state) {
this.counting
)
);
state.decompileInstruction();
state.popInstruction();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
64 changes: 33 additions & 31 deletions src/main/java/org/eolang/opeo/decompilation/agents/CastAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -42,21 +43,21 @@ public final class CastAgent implements DecompilationAgent {
*/
private static final Set<Integer> 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
)
);

Expand All @@ -70,45 +71,46 @@ public void handle(final DecompilerState state) {
state.stack().pop()
)
);
state.decompileInstruction();
state.popInstruction();
}
}

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

Expand All @@ -40,6 +40,9 @@
*/
public final class CheckCastAgent implements DecompilationAgent {

/**
* Supported opcodes.
*/
private static final Set<Integer> SUPPORTED = new HashSet<>(
Arrays.asList(
Opcodes.CHECKCAST
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void handle(final DecompilerState state) {
state.stack().push(res);
break;
}
state.decompileInstruction();
state.popInstruction();
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void handle(final DecompilerState state) {
.type("field")
)
);
state.decompileInstruction();
state.popInstruction();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void handle(final DecompilerState state) {
args
);
state.stack().push(node);
state.decompileInstruction();
state.popInstruction();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void handle(final DecompilerState state) {
args
)
);
state.decompileInstruction();
state.popInstruction();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void handle(final DecompilerState state) {
new Super(target, args, descriptor, type, name)
);
}
state.decompileInstruction();
state.popInstruction();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void handle(final DecompilerState state) {
args
)
);
state.decompileInstruction();
state.popInstruction();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void handle(final DecompilerState state) {
args
)
);
state.decompileInstruction();
state.popInstruction();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
27 changes: 21 additions & 6 deletions src/main/java/org/eolang/opeo/decompilation/agents/LoadAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
*/
public final class LoadAgent implements DecompilationAgent {

/**
* Supported opcodes.
*/
private static final Set<Integer> SUPPORTED = new HashSet<>(
Arrays.asList(
Opcodes.ILOAD,
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

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

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

Expand All @@ -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();
}
}

Expand Down
Loading

0 comments on commit e1c3237

Please sign in to comment.