I want to output true even in Java with a == 1 && a == 2 && a == 3 This is the black magic edition.
Since it is Java (?), The if judgment part is extracted to another class. Since it does not rewrite the value of the argument, make it a static method.
package study.javassist;
public class Judge {
public static boolean judge(int a) {
if (a == 1 && a == 2 && a == 3) {
return true;
} else {
return false;
}
}
}
The main class that calls the above class is as follows.
package study.javassist;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;
public class JudgeCall {
public static void main(String[] args) {
try {
new JudgeCall().execute();
} catch (NotFoundException | CannotCompileException e) {
e.printStackTrace();
}
}
private void execute() throws NotFoundException, CannotCompileException {
ClassPool cp = ClassPool.getDefault();
CtClass cc = cp.get("study.javassist.Judge");
CtMethod method = cc.getDeclaredMethod("judge");
String newJudgeMethod = "{return true;}";
method.setBody(newJudgeMethod);
cc.toClass();
System.out.println(Judge.judge(1));
System.out.println(Judge.judge(2));
System.out.println(Judge.judge(3));
System.out.println(Judge.judge(4));
}
}
true
true
true
true
In the execute () method, the processing content of the judge method of the study.javassist.Judge class is rewritten so that it is always true.
javassist, I avoided it somehow, but can it be used when dealing with the basic part of the system? Next time I have a chance, I will consider it.
Recommended Posts