有没有一种快速的方法来实现反射字段访问?

huangapple go评论72阅读模式
英文:

Is there a fast approach to reflective field access?

问题

我需要一种以反射性质访问字段而无需承受标准反射带来的性能损耗的方法。我已经通过使用特权查找句柄并借助LambdaMetaFactory弄清楚了如何通过方法/构造函数来做到这一点,然而,我似乎无法弄清楚如何获得字段访问权。

我曾尝试通过类似于javaassist的方式生成内部类,理论上该内部类应该可以访问该字段,但这并没有成功,而是抛出了IllegalAccessError。

如果我能重新定义这个类,任务就会变得简单,因为我可以生成getter/setter方法。然而,对于我正在工作的项目,我无法使用代理,因为它需要在运行时加载,并且我还需要动态导入来自工具的attach API。

有人能指导我正确的方向吗?我已经研究了LambdaMetaFactory如何为方法生成接口,并尝试用类似的方式来处理字段,但没有成功。字段和方法在内部是否有某些不同之处,使得在不重新定义的情况下,这个任务变得不可能?

英文:

I need a way to access fields in a reflective nature without the performance hits from standard reflection. I have figured out how to do this with methods/constructors via LambdaMetaFactory using a privileged lookup handle, however, I can't seem to figure out how to gain field access.

I thought I could generate an inner class via something like javaassist which should theoretically have access to that field but that did not work out, throwing an IllegalAccessError.

If I could redefine the class the task would be trivial as I could generate getter/setter methods. However, for the project I am working on, I am unable to use an agent as it would need to be loaded at runtime and I would have to dynamically import the attach api from tools.

Could anyone guide me in the right direction here? I've looked into how LambdaMetaFactory generates it's interface for methods and tried to mirror that with fields with no success. Is there something internally different with fields and methods that makes this task impossible without redefinition?

答案1

得分: 2

在OpendJDK(以及建立在其之上的JDKs)的情况下,LambdaMetaFactory 生成一个基本上是普通类文件(仅访问另一个类的private成员)并使用 sun.misc.Unsafe 中的特殊方法来创建一个匿名类

创建一个类似的访问字段的类文件是直接的,通过以下快速而不太优雅的程序可以演示创建一个带有它的匿名类:

public class Generator {
    public static void main(String[] args) throws Throwable {
        ToIntFunction<Thread> ft = generateIntFieldAccessor(Thread.class, "threadStatus");
        System.out.println(ft.applyAsInt(Thread.currentThread()));
    }

    private static <X> ToIntFunction<X> generateIntFieldAccessor(
        Class<? super X> c, String name) throws Throwable {

        byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
        Class<?> unsafe = Class.forName("sun.misc.Unsafe");
        Field u = unsafe.getDeclaredField("theUnsafe");
        u.setAccessible(true);
        Object theUnsafe = u.get(null);
        Class<ToIntFunction<X>> gen = (Class<ToIntFunction<X>>)
            MethodHandles.publicLookup().bind(theUnsafe, "defineAnonymousClass",
                 MethodType.methodType(
                     Class.class, Class.class, byte[].class, Object[].class))
                .invokeExact(c, code, (Object[])null);
        return gen.getConstructor().newInstance();
    }

    private static final String HEAD = "头部内容省略";
    private static final String TAIL = "尾部内容省略";

    public static byte[] generateIntReaderCode(Field f) {
        // 实现省略,详情见原文
    }
}

当然,对于生产代码,你应该更好地采用一种常用的代码生成库,以获得可维护的工厂代码。例如,OpenJDK 的 LambdaMetaFactory 在内部使用了 ASM 库。

如果你尝试实现类似的解决方案失败了,你必须发布你尝试过的内容,这样我们才能帮助你找出问题所在。但也许,知道一般情况下是可能的,可能已经对你有所帮助。

英文:

In case of OpendJDK (and JDKs built atop it), the LambdaMetaFactory generates a mostly ordinary class file (just accessing private member(s) of another class) and uses a special method in sun.misc.Unsafe, to create an anonymous class.

Creating a similar class file accessing a field, is straight-forward and creating an anonymous class with it, does work, as can be demonstrated with the following quick&dirty program:

public class Generator {
    public static void main(String[] args) throws Throwable {
        ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
        System.out.println(ft.applyAsInt(Thread.currentThread()));
    }

    private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
        Class&lt;? super X&gt; c, String name) throws Throwable {

        byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
        Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
        Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
        u.setAccessible(true);
        Object theUnsafe = u.get(null);
        Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
            MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
                 MethodType.methodType(
                     Class.class, Class.class, byte[].class, Object[].class))
                .invokeExact(c, code, (Object[])null);
        return gen.getConstructor().newInstance();
    }

    private static final String HEAD = &quot;&#202;&#254;&#186;&#190;
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
04
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
\t
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
\n
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
&quot; + &quot;\n
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
\f
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
\f\t
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
\b\f
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
java/lang/Object
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
&quot; + &quot;java/util/function/ToIntFunction
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
&lt;init&gt;
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
()V
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
Code
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
\n&quot; + &quot;applyAsInt
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
(Ljava/lang/Object;)I
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
I&quot;; private static final String TAIL = &quot;
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
01
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
\f
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
&quot; + &quot;
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
\r
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
*&#183;
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
&#177;
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
\r
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
&quot; + &quot;
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
\b+&#192;
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
&#180;
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
&#172;
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
public class Generator {
public static void main(String[] args) throws Throwable {
ToIntFunction&lt;Thread&gt; ft=generateIntFieldAccessor(Thread.class, &quot;threadStatus&quot;);
System.out.println(ft.applyAsInt(Thread.currentThread()));
}
private static &lt;X&gt; ToIntFunction&lt;X&gt; generateIntFieldAccessor(
Class&lt;? super X&gt; c, String name) throws Throwable {
byte[] code = Generator.generateIntReaderCode(c.getDeclaredField(name));
Class&lt;?&gt; unsafe = Class.forName(&quot;sun.misc.Unsafe&quot;);
Field u = unsafe.getDeclaredField(&quot;theUnsafe&quot;);
u.setAccessible(true);
Object theUnsafe = u.get(null);
Class&lt;ToIntFunction&lt;X&gt;&gt; gen = (Class&lt;ToIntFunction&lt;X&gt;&gt;)
MethodHandles.publicLookup().bind(theUnsafe, &quot;defineAnonymousClass&quot;,
MethodType.methodType(
Class.class, Class.class, byte[].class, Object[].class))
.invokeExact(c, code, (Object[])null);
return gen.getConstructor().newInstance();
}
private static final String HEAD = &quot;&#202;&#254;&#186;&#190;\0\0\0004\0\24\7\0\21\7\0\t\7\0\n\7\0\22&quot;
+ &quot;\n\0\2\0\6\f\0\13\0\f\t\0\4\0\b\f\0\23\0\20\1\0\20java/lang/Object\1\0\40&quot;
+ &quot;java/util/function/ToIntFunction\1\0\6&lt;init&gt;\1\0\3()V\1\0\4Code\1\0\n&quot;
+ &quot;applyAsInt\1\0\25(Ljava/lang/Object;)I\1\0\1I&quot;;
private static final String TAIL = &quot;\0001\0\1\0\2\0\1\0\3\0\0\0\2\0\1\0\13\0\f\0&quot;
+ &quot;\1\0\r\0\0\0\21\0\1\0\1\0\0\0\5*&#183;\0\5&#177;\0\0\0\0\0\21\0\16\0\17\0\1\0\r\0\0&quot;
+ &quot;\0\24\0\1\0\2\0\0\0\b+&#192;\0\4&#180;\0\7&#172;\0\0\0\0\0\0&quot;;
public static byte[] generateIntReaderCode(Field f) {
return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) {
@SuppressWarnings(&quot;deprecation&quot;) byte[] get() {
HEAD.getBytes(0, count = HEAD.length(), buf, 0);
try(DataOutputStream dos = new DataOutputStream(this)) {
String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;);
dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;);
dos.writeByte(1); dos.writeUTF(decl);
dos.writeByte(1); dos.writeUTF(f.getName());
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
int dynSize = count;
byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length());
TAIL.getBytes(0, TAIL.length(), result, dynSize);
return result;
}
}.get();
}
}
&quot;; public static byte[] generateIntReaderCode(Field f) { return new ByteArrayOutputStream(HEAD.length() + TAIL.length() + 100) { @SuppressWarnings(&quot;deprecation&quot;) byte[] get() { HEAD.getBytes(0, count = HEAD.length(), buf, 0); try(DataOutputStream dos = new DataOutputStream(this)) { String decl = f.getDeclaringClass().getName().replace(&#39;.&#39;, &#39;/&#39;); dos.writeByte(1); dos.writeUTF(decl+&quot;$&quot;+f.getName()+&quot;$access&quot;); dos.writeByte(1); dos.writeUTF(decl); dos.writeByte(1); dos.writeUTF(f.getName()); } catch (IOException ex) { throw new UncheckedIOException(ex); } int dynSize = count; byte[] result = Arrays.copyOf(buf, dynSize + TAIL.length()); TAIL.getBytes(0, TAIL.length(), result, dynSize); return result; } }.get(); } }

Demo on Ideone

Of course, for production code you should better resort to one of the commonly used code generation libraries, to have maintainable factory code. E.g., OpenJDK’s LambdaMetaFactory uses the ASM library under the hood.

If your attempt to implement a similar solution failed, you have to post what you’ve tried, so we can help identifying the problem. But perhaps, knowing that it is possible in general, does already help you.

答案2

得分: 0

你可以尝试使用Byte Buddy或Javassist进行运行时代码生成,但仅当你需要多次访问不同对象上的相同字段时,才能获得性能提升。否则,代码生成的开销可能会高于使用反射。

如果你认为运行时代码生成可能适用于你的情况,请查看https://github.com/raner/projo,特别是projo-runtime-code-generation/src/main/java/pro/projo/internal/rcg中的代码。请注意,该代码实际上还会生成字段,而不是使用现有类的现有字段,因此它并不完全符合你的需求,但可能会指引你朝着正确的方向前进。

英文:

You could try runtime code generation using Byte Buddy or Javassist, but this will only provide a performance gain if you need to access the same field on different objects many times. Otherwise the overhead for the code generation will likely be higher than that of using reflection.

If you think runtime code generation might work for your situation, have a look at https://github.com/raner/projo, specifically the code in projo-runtime-code-generation/src/main/java/pro/projo/internal/rcg. Note that that code actually generates the fields as well, it does not use existing fields of existing classes, so it's not 100% what you need but might give you a pointer in the right direction.

huangapple
  • 本文由 发表于 2020年8月15日 04:38:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63419740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定