Skip to content

Instantly share code, notes, and snippets.

@DenWav
Created February 10, 2017 22:39
Show Gist options
  • Save DenWav/35c171054087d9d4f687674c48c557b8 to your computer and use it in GitHub Desktop.
Save DenWav/35c171054087d9d4f687674c48c557b8 to your computer and use it in GitHub Desktop.
package com.demonwav.stupid;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Main {
public static final Object object = new Object() {
private final Object one = "one";
private final Object two = "two";
private final Object three = "three";
private final Object four = "four";
private final Object five = "five";
private final Object six = "six";
private final Object seven = "seven";
private final Object eight = "eight";
private final Object nine = "nine";
private final Object ten = "ten";
private void printFields() throws Throwable {
forMethod(
getField(Class.forName("java.lang.System"), "out"),
"println", Class.forName("java.lang.String")
).call(
one + ", " +
two + ", " +
three + ", " +
four + ", " +
five + ", " +
six + ", " +
seven + ", " +
eight + ", " +
nine + ", " +
ten
);
}
};
public static void main(String[] args) throws Throwable {
forMethod(
Class.forName("java.lang.reflect.Array"),
"set",
Class.forName("java.lang.Object"),
int.class,
Class.forName("java.lang.Object")
).call(forMethod(
Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Class"), 1), 0, Class.forName("java.io.InputStream"));
forMethod(
Class.forName("java.lang.reflect.Array"),
"set",
Class.forName("java.lang.Object"),
int.class,
Class.forName("java.lang.Object")
).call(forMethod(
Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Object"), 1), 0, getField(Class.forName("java.lang.System"), "in"));
forMethod(
forMethod(
(Object) Class.forName("java.util.Scanner"),
"getConstructor",
(Class<?>) forMethod(forMethod(Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Class"), 1), "getClass").call()
).call(forMethod(
Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Class"), 1)),
"newInstance",
(Class<?>) forMethod(forMethod(Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Object"), 1), "getClass").call()
).call(forMethod(
Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Object"), 1));
while (true) {
forMethod(getField(Class.forName("java.lang.System"), "out"), "print", Class.forName("java.lang.String"))
.call("(S)et field / (G)et field / (C)all method / (Q)uit: ");
forMethod(getField(Class.forName("java.lang.System"), "out"), "println").call();
final Object lowerCase = forMethod(forMethod(forMethod(
forMethod(
(Object) Class.forName("java.util.Scanner"),
"getConstructor",
(Class<?>) forMethod(forMethod(Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Class"), 1), "getClass").call()
).call(forMethod(Class.forName("java.lang.reflect.Array"), "newInstance", Class.forName("java.lang.Class"), int.class).call(
Class.forName("java.lang.Class"),
1
)),
"newInstance",
(Class<?>) forMethod(forMethod(Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Object"), 1), "getClass").call()
).call(forMethod(Class.forName("java.lang.reflect.Array"), "newInstance", Class.forName("java.lang.Class"), int.class).call(
Class.forName("java.lang.Object"),
1
)), "nextLine").call(), "toLowerCase").call();
if (lowerCase.equals("q")) {
forMethod(forMethod(Class.forName("java.lang.Runtime"), "getRuntime").call(), "exit", int.class).call(0);
} else if (lowerCase.equals("s")) {
forMethod(getField(Class.forName("java.lang.System"), "out"), "print", Class.forName("java.lang.String"))
.call("<fieldName>,<value>: ");
final Object split = forMethod(forMethod(forMethod(
forMethod(
(Object) Class.forName("java.util.Scanner"),
"getConstructor",
(Class<?>) forMethod(forMethod(Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Class"), 1), "getClass").call()
).call(forMethod(
Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(
Class.forName("java.lang.Class"),
1
)),
"newInstance",
(Class<?>) forMethod(forMethod(Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Object"), 1), "getClass").call()
).call(forMethod(Class.forName("java.lang.reflect.Array"), "newInstance", Class.forName("java.lang.Class"), int.class).call(
Class.forName("java.lang.Object"),
1
)), "nextLine").call(), "split", Class.forName("java.lang.String")).call(",");
forMethod(getField(Class.forName("java.lang.System"), "out"), "println").call();
setField(object, (String) forMethod(
Class.forName("java.lang.reflect.Array"),
"get",
Class.forName("java.lang.Object"),
int.class
).call(split, 0), forMethod(
Class.forName("java.lang.reflect.Array"),
"get",
Class.forName("java.lang.Object"),
int.class
).call(split, 1));
} else if (lowerCase.equals("g")) {
forMethod(getField(Class.forName("java.lang.System"), "out"), "print", Class.forName("java.lang.String"))
.call("<fieldName>: ");
forMethod(getField(Class.forName("java.lang.System"), "out"), "println", Class.forName("java.lang.String"))
.call(getField(object, (String) forMethod(forMethod(
forMethod(
(Object) Class.forName("java.util.Scanner"),
"getConstructor",
(Class<?>) forMethod(forMethod(Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Class"), 1), "getClass").call()
).call(forMethod(
Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(
Class.forName("java.lang.Class"),
1
)),
"newInstance",
(Class<?>) forMethod(forMethod(Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Object"), 1), "getClass").call()
).call(forMethod(Class.forName("java.lang.reflect.Array"), "newInstance", Class.forName("java.lang.Class"), int.class).call(
Class.forName("java.lang.Object"),
1
)), "nextLine").call()));
forMethod(getField(Class.forName("java.lang.System"), "out"), "println").call();
} else if (lowerCase.equals("c")) {
forMethod(getField(Class.forName("java.lang.System"), "out"), "print", Class.forName("java.lang.String"))
.call("<methodName>: ");
forMethod(object, (String) forMethod(forMethod(
forMethod(
(Object) Class.forName("java.util.Scanner"),
"getConstructor",
(Class<?>) forMethod(forMethod(Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Class"), 1), "getClass").call()
).call(forMethod(
Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(
Class.forName("java.lang.Class"),
1
)),
"newInstance",
(Class<?>) forMethod(forMethod(Class.forName("java.lang.reflect.Array"),
"newInstance",
Class.forName("java.lang.Class"),
int.class
).call(Class.forName("java.lang.Object"), 1), "getClass").call()
).call(forMethod(Class.forName("java.lang.reflect.Array"), "newInstance", Class.forName("java.lang.Class"), int.class).call(
Class.forName("java.lang.Object"),
1
)), "nextLine").call()).call();
forMethod(getField(Class.forName("java.lang.System"), "out"), "println").call();
}
}
}
//****************************************************************************************
// Framework
//****************************************************************************************
public static class MethodCaller {
private final Class<?> aClass;
private final Object object;
private final String method;
private final Class<?>[] types;
public MethodCaller(Class<?> aClass, Object object, String method, Class<?>[] types) throws Throwable {
this.aClass = aClass;
this.object = object;
this.method = method;
this.types = types;
}
public Object call(Object... params) throws Throwable {
Method method;
try {
method = aClass.getDeclaredMethod(this.method, types);
} catch (NoSuchMethodException e) {
method = aClass.getMethod(this.method, types);
}
method.setAccessible(true);
return method.invoke(object, params);
}
}
public static MethodCaller forMethod(Class<?> aClass, String method, Class<?>... types) throws Throwable {
return new MethodCaller(aClass, null, method, types);
}
public static MethodCaller forMethod(Object object, String method, Class<?>... types) throws Throwable {
final Class<?> aClass = object.getClass();
return new MethodCaller(aClass, object, method, types);
}
public static Object getField(Class<?> aClass, String fieldName) throws Throwable {
Field field;
try {
field = aClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
field = aClass.getField(fieldName);
}
field.setAccessible(true);
return field.get(null);
}
public static Object getField(Object object, String fieldName) throws Throwable {
final Class<?> aClass = object.getClass();
Field field;
try {
field = aClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
field = aClass.getField(fieldName);
}
field.setAccessible(true);
return field.get(object);
}
public static Object setField(Class<?> aClass, String fieldName, Object value) throws Throwable {
Field field;
try {
field = aClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
field = aClass.getField(fieldName);
}
field.setAccessible(true);
if (Modifier.isFinal(field.getModifiers())) {
final Field modifiers = field.getClass().getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
}
final Object oldVal = getField(aClass, fieldName);
field.set(null, value);
return oldVal;
}
public static Object setField(Object object, String fieldName, Object value) throws Throwable {
final Class<?> aClass = object.getClass();
Field field;
try {
field = aClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
field = aClass.getField(fieldName);
}
field.setAccessible(true);
if (Modifier.isFinal(field.getModifiers())) {
final Field modifiers = field.getClass().getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
}
final Object oldVal = getField(object, fieldName);
field.set(object, value);
return oldVal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment