Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)

Preface

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".

Source

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.

Bonus (Scala)

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

Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (gray magic that is not so much as black magic)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Royal road edition that is neither magic nor anything)
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I want to ForEach an array with a Lambda expression in Java
Output true with if (a == 1 && a == 2 && a == 3) in Java (Invisible Identifier)
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I want to create a Parquet file even in Ruby
[Java] I want to test standard input & standard output with JUnit
[Java] I want to perform distinct with the key in the object
I want to select multiple items with a custom layout in Dialog
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I want to display a PDF in Chinese (Korean) with thin reports
I want to send an email in Java.
I did Java to make (a == 1 && a == 2 && a == 3) always true
I want to use java8 forEach with index
rsync4j --I want to touch rsync in Java.
I tried to output multiplication table in Java
I want to be eventually even in kotlin
I tried to break a block with java (1)
I want to do something like "cls" in Java
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to use a little icon in Rails
I tried to create a Clova skill in Java
I want to monitor a specific file with WatchService
I tried to make a login function in Java
I want to define a function in Rails Console
I want to transition screens with kotlin and java!
I want to click a GoogleMap pin in RSpec
I want to get along with Map [Java beginner]
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
I want to return an object in CSV format with multi-line header & filter in Java
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
[Beginner] I made a program to sell cakes in Java
I just wanted to make a Reactive Property in Java
I tried to convert a string to a LocalDate type in Java
I tried to make a client of RESAS-API in Java
I want to implement various functions with kotlin and java!
I want to simplify the conditional if-else statement in Java
I want to write a loop that references an index with Java 8's Stream API
[CQ Engine] I want to handle collections like Stream or .Net LINQ even in Java 7.
I want to Flash Attribute in Spring even if I set a reverse proxy! (do not do)
I want to return a type different from the input element with Java8 StreamAPI reduce ()
I created a PDF in Java.
I tried to interact with Java
Log output to file in Java
A story that I struggled to challenge a competition professional with Java
I want to make a button with a line break with link_to [Note]
I want to return to the previous screen with kotlin and java!
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to extract between character strings with a regular expression
[Wire Mock] I want to set up a stub / mock server in Java and perform E2E tests.
[Ruby] I want to output only the odd-numbered characters in the character string
I want to develop a web application!
I want to write a nice build.gradle