Created
April 3, 2014 20:32
-
-
Save dimzon/9962320 to your computer and use it in GitHub Desktop.
Use built-in java bytecode generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.narod.dimzon541.utils.reflect; | |
import sun.reflect.ConstructorAccessor; | |
import sun.reflect.FieldAccessor; | |
import sun.reflect.MethodAccessor; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
@SuppressWarnings("UnusedDeclaration") | |
public class MethodAccessorsGenerator { | |
private static final Object generatorObject; | |
private static final MethodAccessor generateMethod; | |
private static final MethodAccessor generateConstructor; | |
private static final MethodAccessor newFieldAccessor; | |
static { | |
try { | |
Class<?> aClass = Class.forName("sun.reflect.MethodAccessorGenerator"); | |
Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors(); | |
Constructor<?> declaredConstructor = declaredConstructors[0]; | |
declaredConstructor.setAccessible(true); | |
generatorObject = declaredConstructor.newInstance(); | |
Method bar = aClass.getMethod("generateMethod", Class.class, String.class, Class[].class, Class.class, Class[].class, Integer.TYPE); | |
bar.setAccessible(true); | |
generateMethod = (MethodAccessor) bar.invoke( | |
generatorObject, | |
bar.getDeclaringClass(), | |
bar.getName(), | |
bar.getParameterTypes(), | |
bar.getReturnType(), | |
bar.getExceptionTypes(), | |
bar.getModifiers()); | |
bar = aClass.getMethod("generateConstructor", Class.class, Class[].class, Class[].class, Integer.TYPE); | |
generateConstructor = newMethodAccessor(bar); | |
aClass = Class.forName("sun.reflect.UnsafeFieldAccessorFactory"); | |
bar = aClass.getDeclaredMethod("newFieldAccessor", Field.class, Boolean.TYPE); | |
newFieldAccessor = newMethodAccessor(bar); | |
} catch (NoSuchMethodException | |
| InstantiationException | |
| IllegalAccessException | |
| InvocationTargetException | |
| ClassNotFoundException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static FieldAccessor newFieldAccessor(Field field, boolean overrideFinalCheck){ | |
try { | |
return (FieldAccessor) newFieldAccessor.invoke(null,new Object[]{field,overrideFinalCheck}); | |
} catch (InvocationTargetException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static MethodAccessor newMethodAccessor(Method bar){ | |
try { | |
return (MethodAccessor) generateMethod.invoke( | |
generatorObject, new Object[]{ | |
bar.getDeclaringClass(), | |
bar.getName(), | |
bar.getParameterTypes(), | |
bar.getReturnType(), | |
bar.getExceptionTypes(), | |
bar.getModifiers()}); | |
} catch (InvocationTargetException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static ConstructorAccessor newConstructorAccessor(Constructor<?> bar){ | |
try { | |
return (ConstructorAccessor) generateConstructor.invoke( | |
generatorObject, new Object[]{ | |
bar.getDeclaringClass(), | |
bar.getParameterTypes(), | |
bar.getExceptionTypes(), | |
bar.getModifiers()}); | |
} catch (InvocationTargetException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment