Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)

Preface

I want to output true even in Java with a == 1 && a == 2 && a == 3 This is the black magic edition.

Operating environment

Logic etc.

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));
	
	}
}

result

true
true
true
true

Commentary

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.

Impressions I tried

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

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 (PowerMockito edition)
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 (Javassist second decoction)
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
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 send an email in Java.
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 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
I made a function to register images with API in Spring Framework. Part 1 (API edition)
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.
[Java] I want to convert a byte array to a hexadecimal number
I want to find a relative path in a situation using Path
[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
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 made a function to register images with API in Spring Framework. Part 2 (Client Edition)
I created a PDF in Java.
I tried to interact with Java
Log output to file in Java
I want to add a browsing function with ruby on rails
I can't create a Java class with a specific name in IntelliJ
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
[Rails] I want to send data of different models in a form
I want to write a nice build.gradle
[Personal memo] How to interact with a random number generator in Java
I want to display images with REST Controller of Java and Spring!
I want to use DBViewer with Eclipse 2018-12! !!