Skip to content

Commit

Permalink
feat: debuggable annotaion is enabled at test class and method
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Feb 8, 2023
1 parent 34a7c56 commit 5476927
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 38 deletions.
7 changes: 2 additions & 5 deletions src/main/java/reincarnation/Debuggable.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @version 2018/10/04 9:06:28
*/
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Debuggable {
}
12 changes: 10 additions & 2 deletions src/main/java/reincarnation/Debugger.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class Debugger extends AnnotationVisitor {
/** The processing environment. */
static boolean whileDebug = false;

/** The processing environment. */
static boolean enableDebugByClass;

/** The processing environment. */
static boolean enableDebugByMethod;

/** The processing environment. */
private static final boolean whileTest;

Expand Down Expand Up @@ -132,11 +138,13 @@ public static void enable(String className, String methodName) {
* @return
*/
public static boolean isEnable() {
if (debugger.enable && debugger.firstTime) {
boolean enable = debugger.enable || enableDebugByClass || enableDebugByMethod;

if (enable && debugger.firstTime) {
debugger.firstTime = false;
printInfo(false);
}
return debugger.enable;
return enable;
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/reincarnation/CodeVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
import java.util.function.Function;
import java.util.function.Supplier;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;

import bee.UserInterface;
import bee.api.Command;
import bee.util.JavaCompiler;
Expand Down Expand Up @@ -66,6 +72,42 @@ public class CodeVerifier {
/** The built-in parameters. */
private static final List<String> texts = List.of("", " ", "a", "A", "あ", "\\", "\t", "some value");

/**
* Change degub state.
*/
@BeforeAll
protected static final void enableDebugByClass(TestInfo info) {
if (info.getTestClass().get().isAnnotationPresent(Debuggable.class)) {
Debugger.enableDebugByClass = true;
}
}

/**
* Change degub state.
*/
@AfterAll
protected static final void disableDebugByClass() {
Debugger.enableDebugByClass = false;
}

/**
* Change degub state.
*/
@BeforeEach
protected final void enableDebugByMethod(TestInfo info) {
if (info.getTestMethod().get().isAnnotationPresent(Debuggable.class)) {
Debugger.enableDebugByMethod = true;
}
}

/**
* Change degub state.
*/
@AfterEach
protected final void diableDebugByMethod() {
Debugger.enableDebugByMethod = false;
}

/**
* Verify decompiled code.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class TryCatchFinallyTest extends CodeVerifier {
void TryCatchFinallyAfter() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand All @@ -52,7 +51,6 @@ public int run(@Param(from = 0, to = 10) int value) {
return counter + calc(value);
}

@Debuggable
private int calc(int value) {
try {
return MaybeThrow.error(value);
Expand Down Expand Up @@ -135,7 +133,6 @@ public int run(@Param(from = 0, to = 10) int value) {
return count(value) + counter;
}

@Debuggable
private int count(int value) {
try {
return MaybeThrow.error(value);
Expand All @@ -159,7 +156,6 @@ public int run(@Param(from = 0, to = 10) int value) {
return count(value) + counter;
}

@Debuggable
private int count(int value) {
try {
return MaybeThrow.error(value);
Expand Down Expand Up @@ -188,7 +184,6 @@ public int run(@Param(from = 0, to = 10) int value) {
return count(value) + counter;
}

@Debuggable
private int count(int value) {
try {
value = MaybeThrow.error(value);
Expand All @@ -203,6 +198,7 @@ private int count(int value) {
}

@Test
@Debuggable
void TryCatchFinallyNodes() {
verify(new TestCode.IntParam() {

Expand All @@ -213,7 +209,6 @@ public int run(@Param(from = 0, to = 10) int value) {
return count(value) + counter;
}

@Debuggable
private int count(int value) {
try {
if (value % 2 == 0) {
Expand Down
16 changes: 2 additions & 14 deletions src/test/java/reincarnation/decompiler/flow/TryCatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.junit.jupiter.api.parallel.ExecutionMode;

import reincarnation.CodeVerifier;
import reincarnation.Debuggable;
import reincarnation.TestCode;

@Execution(ExecutionMode.SAME_THREAD)
Expand All @@ -30,7 +29,7 @@ void TryCatch() {
verify(new TestCode.IntParam() {

@Override
@Debuggable

public int run(@Param(from = 0, to = 10) int value) {
try {
return MaybeThrow.error(value);
Expand All @@ -45,7 +44,6 @@ public int run(@Param(from = 0, to = 10) int value) {
void TryCatchAfter() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand Down Expand Up @@ -120,7 +118,6 @@ public int run(@Param(from = 0, to = 10) int value) {
}
}

@Debuggable
private int error(int value) {
try {
if (value != 3) {
Expand All @@ -138,7 +135,6 @@ private int error(int value) {
void TryMultipleCatch() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand All @@ -158,7 +154,7 @@ public int run(@Param(from = 0, to = 10) int value) {
@Test
void TryMultipleCatchInherited() {
verify(new TestCode.IntParam() {
@Debuggable

@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand All @@ -178,7 +174,6 @@ public int run(@Param(from = 0, to = 10) int value) {
void TryMultipleCatchAfter() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand All @@ -198,7 +193,6 @@ public int run(@Param(from = 0, to = 10) int value) {
void TryCatchInCatch() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand Down Expand Up @@ -239,7 +233,6 @@ public int run(@Param(from = 0, to = 10) int value) {
void TryCatchInTry() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand All @@ -262,7 +255,6 @@ public int run(@Param(from = 0, to = 10) int value) {
void TryCatchInTryImmediately() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand All @@ -283,7 +275,6 @@ public int run(@Param(from = 0, to = 10) int value) {
void TryCatchsInTry() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand Down Expand Up @@ -312,7 +303,6 @@ public int run(@Param(from = 0, to = 10) int value) {
void TryCatchsInTryMultiCatches() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand Down Expand Up @@ -343,7 +333,6 @@ public int run(@Param(from = 0, to = 10) int value) {
void TryCatchInTryAfter() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand All @@ -367,7 +356,6 @@ public int run(@Param(from = 0, to = 10) int value) {
void TryCatchWithFrameFull() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
for (int i = 0; i < 1; i++) {
Expand Down
16 changes: 5 additions & 11 deletions src/test/java/reincarnation/decompiler/flow/TryFinallyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.junit.jupiter.api.parallel.ExecutionMode;

import reincarnation.CodeVerifier;
import reincarnation.Debuggable;
import reincarnation.TestCode;

@Execution(ExecutionMode.SAME_THREAD)
Expand All @@ -26,7 +25,7 @@ void normal() {
verify(new TestCode.IntParam() {

@Override
@Debuggable

public int run(@Param(from = 0, to = 10) int param) {
try {
return param + 1;
Expand All @@ -42,7 +41,7 @@ void normalAfter() {
verify(new TestCode.IntParam() {

@Override
@Debuggable

public int run(@Param(from = 0, to = 10) int param) {
try {
param += 1;
Expand All @@ -59,7 +58,7 @@ void nodesInTryAndFinallyAfter() {
verify(new TestCode.IntParam() {

@Override
@Debuggable

public int run(@Param(from = 0, to = 10) int param) {
try {
if (param % 2 == 0) {
Expand All @@ -84,7 +83,7 @@ void halfNodesInTryAndFinallyAfter() {
verify(new TestCode.IntParam() {

@Override
@Debuggable

public int run(@Param(from = 0, to = 10) int param) {
try {
if (param % 2 == 0) {
Expand All @@ -109,7 +108,7 @@ void multipleReturnInTry() {
verify(new TestCode.IntParam() {

@Override
@Debuggable

public int run(@Param(from = 0, to = 10) int param) {
try {
if (param % 2 == 0) {
Expand Down Expand Up @@ -182,7 +181,6 @@ public int run(@Param(from = 0, to = 10) int param) {
return field;
}

@Debuggable
private int error(int param) {
try {
return MaybeThrow.error(param);
Expand All @@ -209,7 +207,6 @@ public int run(@Param(from = 0, to = 10) int param) {
return field;
}

@Debuggable
private int error(int param) {
try {
param = MaybeThrow.error(param);
Expand All @@ -225,7 +222,6 @@ private int error(int param) {
void tryFinallyInTryAndFinally() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand All @@ -250,7 +246,6 @@ public int run(@Param(from = 0, to = 10) int value) {
void tryFinallyWithHeadNodeInTryAndFinally() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand Down Expand Up @@ -279,7 +274,6 @@ public int run(@Param(from = 0, to = 10) int value) {
void tryFinallyWithTailNodeInTryAndFinally() {
verify(new TestCode.IntParam() {

@Debuggable
@Override
public int run(@Param(from = 0, to = 10) int value) {
try {
Expand Down

0 comments on commit 5476927

Please sign in to comment.