Skip to content

Instantly share code, notes, and snippets.

@Jezza
Last active October 1, 2015 18:16
Show Gist options
  • Save Jezza/28e37855fee0a80d4b5b to your computer and use it in GitHub Desktop.
Save Jezza/28e37855fee0a80d4b5b to your computer and use it in GitHub Desktop.
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Random;
/**
* General fuckery.
*
* @author jezza
* @date Oct 1, 2015
*/
public class Fuckery {
private static Field modifiersField;
private static Field typeField;
private Fuckery() {
throw new IllegalStateException("Helper class only!");
}
/**
* Entry Point to the examples.
* ::Output (One of many)::
* It was true
* It was false
* It was true
* It was true
* It was false
* It was false
* TWO
*/
public static void main(final String[] args) throws Exception {
final RandomBoolean fuckedBoolean = new RandomBoolean();
override(Boolean.class, "TRUE", fuckedBoolean);
override(Boolean.class, "FALSE", fuckedBoolean);
System.out.println("It was " + Boolean.FALSE);
System.out.println("It was " + Boolean.FALSE);
System.out.println("It was " + Boolean.FALSE);
System.out.println("It was " + Boolean.TRUE);
System.out.println("It was " + Boolean.TRUE);
System.out.println("It was " + Boolean.TRUE);
override(Test.class, "ONE", Test.TWO);
System.out.println(Test.ONE.name());
}
/**
* Used for static fields.
*
* @param clazz
* - The class the field resides.
* @param fieldName
* - The name of the field to find.
* @param newValue
* - The value to be injected.
* @throws Exception
*/
public static void override(final Class<?> clazz, final String fieldName, final Object newValue) throws Exception {
final Field field = clazz.getDeclaredField(fieldName);
override(field, null, newValue);
}
/**
* Used for instance fields.
*
* @param clazz
* - The class the field resides.
* @param fieldName
* - The name of the field to find.
* @param instance
* - the instance of the class to change.
* @param newValue
* - The value to be injected.
* @throws Exception
*/
public static void override(final Class<?> clazz, final String fieldName, final Object instance, final Object newValue) throws Exception {
final Field field = clazz.getDeclaredField(fieldName);
override(field, instance, newValue);
}
/**
* Used for instance fields.
*
* @param field
* - The field to change.
* @param newValue
* - The value to be injected.
* @throws Exception
*/
public static void override(final Field field, final Object newValue) throws Exception {
override(field, null, newValue);
}
/**
* Used for instance fields.
*
* @param field
* - The field to change.
* @param instance
* - the instance of the class to change.
* @param newValue
* - The value to be injected.
* @throws Exception
*/
public static void override(final Field field, final Object instance, final Object newValue) throws Exception {
field.setAccessible(true);
modifiersField().setInt(field, field.getModifiers() & ~Modifier.FINAL);
typeField().set(field, newValue.getClass());
field.set(instance, newValue);
}
/**
* This retrieves the field containing the modifiers of a field.
*
* @return The field assocated with the field modifiers.
* @throws Exception
*/
private static Field modifiersField() throws Exception {
if (modifiersField == null) {
modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
}
return modifiersField;
}
/**
* This retrieves the field containing the class type of a field.
*
* @return The field assocated with the class type attached to fields.
* @throws Exception
*/
private static Field typeField() throws Exception {
if (typeField == null) {
typeField = Field.class.getDeclaredField("type");
typeField.setAccessible(true);
}
return typeField;
}
private enum Test {
ONE, TWO, THREE;
}
public static final class RandomBoolean implements java.io.Serializable, Comparable<Boolean> {
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -3665804199014368530L;
private static final Random random = new Random();
public RandomBoolean() {
}
public boolean booleanValue() {
return random.nextBoolean();
}
@Override
public String toString() {
return booleanValue() ? "true" : "false";
}
@Override
public int hashCode() {
return Boolean.hashCode(booleanValue());
}
@Override
public boolean equals(final Object obj) {
return booleanValue();
}
@Override
public int compareTo(final Boolean b) {
return Boolean.compare(booleanValue(), b.booleanValue());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment