It is a version that rewrites the value of the variable a using Javassist of "I want to output true with a == 1 && a == 2 && a == 3 even in Java".
EqualsA.java
package com.example;
public class EqualsA {
private int a;
public void execute() {
System.out.println("start");
if (a == 1 && a == 2 && a == 3) {
System.out.println(true);
} else {
System.out.println(false);
}
System.out.println("end");
}
}
EqualsMain.java
package com.example;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.expr.ExprEditor;
import javassist.expr.FieldAccess;
public class EqualsMain {
public static void main(String[] args) throws Exception {
convert();
new EqualsA().execute();
}
static void convert() throws Exception {
ClassPool classPool = ClassPool.getDefault();
CtClass cc = classPool.get("com.example.EqualsA");
cc.instrument(new ExprEditor() {
private int i = 1;
@Override
public void edit(FieldAccess f) throws CannotCompileException {
if (f.getFieldName().equals("a")) {
f.replace(String.format("$_ = %d;", i++));
}
}
});
cc.toClass();
}
}
ExprEditor doesn't seem to be able to handle local variables, but it can handle fields.
object EqualsA {
def main(args: Array[String]): Unit = {
val a = Int(1)
if (a == 1 && a == 2 && a == 3) {
println("true")
} else {
println("false")
}
}
case class Int(n: scala.Int) {
override def equals(other: Any) = true
}
}
Recommended Posts