Skip to content

Instantly share code, notes, and snippets.

@pclark97
Forked from varikin/BestJavaEver.java
Created February 2, 2018 23:37
Show Gist options
  • Save pclark97/01bc139b65fa7f86316d7befb917d54c to your computer and use it in GitHub Desktop.
Save pclark97/01bc139b65fa7f86316d7befb917d54c to your computer and use it in GitHub Desktop.
The Original Best Java Ever
import static java.lang.System.*;
import static java.lang.Integer.*;
import static java.lang.String.*;
import static java.lang.Float.compare;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
* Demonstrates good coding standards with Java.
*
* @date Apr 1, 2014
*/
public strictfp enum BestJavaEver {
THE_BEST_ENUM_VALUE_EVER() {
@Override
public String toString() {
this./*TODO: is this the right method?*/calculateStringRepr();
return name();
}
};
public abstract String toString();
private static String $$ = (String)null + null;
static List<Double> dynamicList;
static float f;
@Deprecated
public static void \u006d\u0061\u0069\u006e(String...args) throws Exception {
}
protected void calculateStringRepr() {
/*
TODO: Close this comment?
TODO: Determine why the above method is named \u006d\u0061\u002A\u002F
// First, an arithmetic check.
// 100 is 100.
Integer a = 100;
Integer b = 100;
if (a != b) throw new RuntimeException();
// But 200 isn't 200.
Integer c = 200;
Integer d = 200;
if (c == d) throw new RuntimeException();
ensureBestMathEver();
if ((Integer) 2 + 2 == (Integer) 4)
throw new RuntimeException();
f = 1 / (float)(ordinal());
// f should always be smaller than the max value of a float. Get us out of this silly class before it's too late.
if (new Comparable() {
@Override
public int compareTo(Object o) {
return compare(f, (Float) o);
}
}.compareTo(Float.MAX_VALUE) < 0) {
return;
}
// validation time
Boolean boolTrue = convertStringToBoolean("true");
int errors = countErrors(true);
assert errors == 0;
// Store f in a list. We do this with dynamically interpreted Java code for greater abstraction
// and ease of refactoring.
dynamicList = new ArrayList<Double>();
JavaInterpreter.eval("dynamicList.add(f)");
http://www.code42.com
// I've always wondered what null's hash code is. Let's find out.
try {
throw null;
} catch (Throwable t) {
int nullsHashCode = t.hashCode();
// Null's hash code is constant, so we can use it in our math.
int _ = __._(0123456765432l + (long)((float)nullsHashCode / dynamicList.get(0)));
try{
out.println(o_O()[_--+--_+-_--+--_]);
label: break label;
Runtime.getRuntime().halt(0);
} finally {
out.println("We're done here.");
}
}
}
public void ensureBestMathEver() {
try {
Class cache = Integer.class.getDeclaredClasses()[0];
Field c = cache.getDeclaredField("cache");
c.setAccessible(true);
Integer[] array = (Integer[]) c.get(cache);
array[132] = array[133];
} catch (Exception e) {
}
}
private static String o_O()[] {
class Local extends Exception {
private String defaultString = "Default string representation";
public String Local() throws Local {
try {
// Use reflection.
return getProperty("defaultString");
} finally {
// If reflection doesn't work.
this.defaultString = ((BestJavaEver)null).$$;
throw this; // against a wall.
}
}
public String getMessage() {
return "Local; details: " + this;
}
}
try {
new Local().Local();
return null;
} catch (Local local) {
try {
return new String[] {local.getMessage()};
} catch (VirtualMachineError ex) {
// Sometimes the JVM flakes out getting an exception's message.
// In that case, just use the default.
return new String[] {local.defaultString};
}
}
}
private static Boolean convertStringToBoolean(String str) {
if(Boolean.getBoolean(str)) {
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
}
public static int countErrors(Boolean bool) {
try {
// bool must be false for validation to complete
if(bool) {
throw new IllegalArgumentException("bool is true, throwing");
}
} finally {
// happy path return 0 errors
return 0;
}
}
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
String errorMessage = "Something has gone wrong with " + THE_BEST_ENUM_VALUE_EVER;
}
});
}
}
class __ {
public static int _(Long n) {
// TODO: Refactor this method out. Until then, make sure nobody calls it with this assert.
assert false;
return valueOf(valueOf((n &= n%(n >>> n))));
}
}
/**
* We can run arbitrary Java strings with this Java interpreter.
*/
class JavaInterpreter {
public static void eval(String javaString) {
ScriptEngineManager manager = new ScriptEngineManager();
// Pass in our private key to ensure that our executed Java code is appropriately signed.
byte[] privateKey = {0x4A, 0x61, 0x76, 0x61, 0x53, 0x63, 0x72, 0x69, 0x70, 0x74};
String keyString = new String(privateKey);
ScriptEngine engine = manager.getEngineByName(keyString);
// Make sure our interpreter is also the Best Ever.
for (Field field : BestJavaEver.class.getDeclaredFields()) {
try {
engine.put(field.getName(), field.get(null));
} catch (Exception ex) {
}
}
try {
engine.eval(javaString);
} catch (Exception ex) {
// What could possibly go wrong?
}
}
}
#!/bin/bash
RED_TERM='\033[31m'
GREEN_TERM='\033[32m'
YELLOW_TERM='\033[33m'
BLUE_TERM='\033[34m'
END_COLOR='\033[0m'
# Echo some message in color
echo_color() {
color=$2
msg="${color}${1}${END_COLOR}"
echo -e "${color}${msg}${END_COLOR}"
}
echo_color "Compiling the best java code ever" $BLUE_TERM
javac BestJavaEver.java
echo_color "Executing BestJavaEver" $BLUE_TERM
result=$(java BestJavaEver)
if [[ "$result" == "nullnull" ]]; then
echo_color "** SUCCESS **" $GREEN_TERM
exit 0
else
echo_color "** FAILED **" $RED_TERM
echo_color "result = $result" $YELLOW_TERM
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment