Skip to content

Commit 384deda

Browse files
committed
8325949: Create an internal utility method for creating VarHandle instances
Reviewed-by: rriggs
1 parent 67448b0 commit 384deda

31 files changed

+232
-317
lines changed

src/java.base/share/classes/java/lang/ThreadBuilders.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,6 +34,7 @@
3434
import java.util.concurrent.Executor;
3535
import java.util.concurrent.ThreadFactory;
3636
import jdk.internal.misc.Unsafe;
37+
import jdk.internal.invoke.MhUtil;
3738
import jdk.internal.vm.ContinuationSupport;
3839

3940
/**
@@ -273,15 +274,9 @@ public ThreadFactory factory() {
273274
* Base ThreadFactory implementation.
274275
*/
275276
private abstract static class BaseThreadFactory implements ThreadFactory {
276-
private static final VarHandle COUNT;
277-
static {
278-
try {
279-
MethodHandles.Lookup l = MethodHandles.lookup();
280-
COUNT = l.findVarHandle(BaseThreadFactory.class, "count", long.class);
281-
} catch (Exception e) {
282-
throw new InternalError(e);
283-
}
284-
}
277+
private static final VarHandle COUNT = MhUtil.findVarHandle(
278+
MethodHandles.lookup(), "count", long.class);
279+
285280
private final String name;
286281
private final int characteristics;
287282
private final UncaughtExceptionHandler uhe;

src/java.base/share/classes/java/lang/invoke/Invokers.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package java.lang.invoke;
2727

28+
import jdk.internal.invoke.MhUtil;
2829
import jdk.internal.vm.annotation.DontInline;
2930
import jdk.internal.vm.annotation.ForceInline;
3031
import jdk.internal.vm.annotation.Hidden;
@@ -681,16 +682,9 @@ private static NamedFunction getNamedFunction(String name, MethodType type)
681682
}
682683

683684
private static class Lazy {
684-
private static final MethodHandle MH_asSpreader;
685-
686-
static {
687-
try {
688-
MH_asSpreader = IMPL_LOOKUP.findVirtual(MethodHandle.class, "asSpreader",
689-
MethodType.methodType(MethodHandle.class, Class.class, int.class));
690-
} catch (ReflectiveOperationException ex) {
691-
throw newInternalError(ex);
692-
}
693-
}
685+
private static final MethodHandle MH_asSpreader = MhUtil.findVirtual(
686+
IMPL_LOOKUP, MethodHandle.class, "asSpreader",
687+
MethodType.methodType(MethodHandle.class, Class.class, int.class));
694688
}
695689

696690
static {

src/java.base/share/classes/java/net/Socket.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import jdk.internal.event.SocketReadEvent;
2929
import jdk.internal.event.SocketWriteEvent;
30+
import jdk.internal.invoke.MhUtil;
3031
import sun.security.util.SecurityConstants;
3132

3233
import java.io.InputStream;
@@ -102,14 +103,10 @@
102103
public class Socket implements java.io.Closeable {
103104
private static final VarHandle STATE, IN, OUT;
104105
static {
105-
try {
106-
MethodHandles.Lookup l = MethodHandles.lookup();
107-
STATE = l.findVarHandle(Socket.class, "state", int.class);
108-
IN = l.findVarHandle(Socket.class, "in", InputStream.class);
109-
OUT = l.findVarHandle(Socket.class, "out", OutputStream.class);
110-
} catch (Exception e) {
111-
throw new InternalError(e);
112-
}
106+
MethodHandles.Lookup l = MethodHandles.lookup();
107+
STATE = MhUtil.findVarHandle(l, "state", int.class);
108+
IN = MhUtil.findVarHandle(l, "in", InputStream.class);
109+
OUT = MhUtil.findVarHandle(l, "out", OutputStream.class);
113110
}
114111

115112
// the underlying SocketImpl, may be null, may be swapped when connecting

src/java.base/share/classes/java/nio/channels/SelectionKey.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,8 @@
2525

2626
package java.nio.channels;
2727

28+
import jdk.internal.invoke.MhUtil;
29+
2830
import java.lang.invoke.MethodHandles;
2931
import java.lang.invoke.VarHandle;
3032

@@ -429,15 +431,9 @@ public final boolean isAcceptable() {
429431

430432
// -- Attachments --
431433

432-
private static final VarHandle ATTACHMENT;
433-
static {
434-
try {
435-
MethodHandles.Lookup l = MethodHandles.lookup();
436-
ATTACHMENT = l.findVarHandle(SelectionKey.class, "attachment", Object.class);
437-
} catch (Exception e) {
438-
throw new InternalError(e);
439-
}
440-
}
434+
private static final VarHandle ATTACHMENT = MhUtil.findVarHandle(
435+
MethodHandles.lookup(), "attachment", Object.class);
436+
441437
private volatile Object attachment;
442438

443439
/**

src/java.base/share/classes/java/nio/channels/spi/AbstractSelectionKey.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,7 @@
3030
import java.nio.channels.SelectionKey;
3131
import java.nio.channels.Selector;
3232

33+
import jdk.internal.invoke.MhUtil;
3334
import sun.nio.ch.SelectionKeyImpl;
3435
import sun.nio.ch.SelectorImpl;
3536

@@ -46,15 +47,8 @@
4647
public abstract class AbstractSelectionKey
4748
extends SelectionKey
4849
{
49-
private static final VarHandle INVALID;
50-
static {
51-
try {
52-
MethodHandles.Lookup l = MethodHandles.lookup();
53-
INVALID = l.findVarHandle(AbstractSelectionKey.class, "invalid", boolean.class);
54-
} catch (Exception e) {
55-
throw new InternalError(e);
56-
}
57-
}
50+
private static final VarHandle INVALID = MhUtil.findVarHandle(
51+
MethodHandles.lookup(), "invalid", boolean.class);
5852

5953
/**
6054
* Initializes a new instance of this class.

src/java.base/share/classes/java/nio/channels/spi/AbstractSelector.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.nio.channels.Selector;
3333
import java.util.HashSet;
3434
import java.util.Set;
35+
36+
import jdk.internal.invoke.MhUtil;
3537
import sun.nio.ch.Interruptible;
3638
import sun.nio.ch.SelectorImpl;
3739

@@ -72,15 +74,9 @@
7274
public abstract class AbstractSelector
7375
extends Selector
7476
{
75-
private static final VarHandle CLOSED;
76-
static {
77-
try {
78-
MethodHandles.Lookup l = MethodHandles.lookup();
79-
CLOSED = l.findVarHandle(AbstractSelector.class, "closed", boolean.class);
80-
} catch (Exception e) {
81-
throw new InternalError(e);
82-
}
83-
}
77+
private static final VarHandle CLOSED = MhUtil.findVarHandle(
78+
MethodHandles.lookup(), "closed", boolean.class);
79+
8480
private volatile boolean closed;
8581

8682
// The provider that created this selector

src/java.base/share/classes/java/util/concurrent/CompletableFuture.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
package java.util.concurrent;
3737

38+
import jdk.internal.invoke.MhUtil;
39+
3840
import java.lang.invoke.MethodHandles;
3941
import java.lang.invoke.VarHandle;
4042
import java.util.concurrent.locks.LockSupport;
@@ -3079,14 +3081,10 @@ static final class MinimalStage<T> extends CompletableFuture<T> {
30793081
private static final VarHandle STACK;
30803082
private static final VarHandle NEXT;
30813083
static {
3082-
try {
3083-
MethodHandles.Lookup l = MethodHandles.lookup();
3084-
RESULT = l.findVarHandle(CompletableFuture.class, "result", Object.class);
3085-
STACK = l.findVarHandle(CompletableFuture.class, "stack", Completion.class);
3086-
NEXT = l.findVarHandle(Completion.class, "next", Completion.class);
3087-
} catch (ReflectiveOperationException e) {
3088-
throw new ExceptionInInitializerError(e);
3089-
}
3084+
MethodHandles.Lookup l = MethodHandles.lookup();
3085+
RESULT = MhUtil.findVarHandle(l, "result", Object.class);
3086+
STACK = MhUtil.findVarHandle(l, "stack", Completion.class);
3087+
NEXT = MhUtil.findVarHandle(l, Completion.class, "next", Completion.class);
30903088

30913089
// Reduce the risk of rare disastrous classloading in first call to
30923090
// LockSupport.park: https://bugs.openjdk.org/browse/JDK-8074773

src/java.base/share/classes/java/util/concurrent/Exchanger.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
package java.util.concurrent;
3838

39+
import jdk.internal.invoke.MhUtil;
40+
3941
import java.lang.invoke.MethodHandles;
4042
import java.lang.invoke.VarHandle;
4143
import java.util.concurrent.locks.LockSupport;
@@ -530,15 +532,11 @@ public V exchange(V x, long timeout, TimeUnit unit)
530532
private static final VarHandle ENTRY;
531533
private static final VarHandle AA;
532534
static {
533-
try {
534-
MethodHandles.Lookup l = MethodHandles.lookup();
535-
BOUND = l.findVarHandle(Exchanger.class, "bound", int.class);
536-
MATCH = l.findVarHandle(Node.class, "match", Object.class);
537-
ENTRY = l.findVarHandle(Slot.class, "entry", Node.class);
538-
AA = MethodHandles.arrayElementVarHandle(Slot[].class);
539-
} catch (ReflectiveOperationException e) {
540-
throw new ExceptionInInitializerError(e);
541-
}
535+
MethodHandles.Lookup l = MethodHandles.lookup();
536+
BOUND = MhUtil.findVarHandle(l, "bound", int.class);
537+
MATCH = MhUtil.findVarHandle(l, Node.class, "match", Object.class);
538+
ENTRY = MhUtil.findVarHandle(l, Slot.class, "entry", Node.class);
539+
AA = MethodHandles.arrayElementVarHandle(Slot[].class);
542540
}
543541

544542
}

src/java.base/share/classes/java/util/concurrent/FutureTask.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
package java.util.concurrent;
3737

38+
import jdk.internal.invoke.MhUtil;
39+
3840
import java.lang.invoke.MethodHandles;
3941
import java.lang.invoke.VarHandle;
4042
import java.util.concurrent.locks.LockSupport;
@@ -582,14 +584,10 @@ public String toString() {
582584
private static final VarHandle RUNNER;
583585
private static final VarHandle WAITERS;
584586
static {
585-
try {
586-
MethodHandles.Lookup l = MethodHandles.lookup();
587-
STATE = l.findVarHandle(FutureTask.class, "state", int.class);
588-
RUNNER = l.findVarHandle(FutureTask.class, "runner", Thread.class);
589-
WAITERS = l.findVarHandle(FutureTask.class, "waiters", WaitNode.class);
590-
} catch (ReflectiveOperationException e) {
591-
throw new ExceptionInInitializerError(e);
592-
}
587+
MethodHandles.Lookup l = MethodHandles.lookup();
588+
STATE = MhUtil.findVarHandle(l, "state", int.class);
589+
RUNNER = MhUtil.findVarHandle(l, "runner", Thread.class);
590+
WAITERS = MhUtil.findVarHandle(l, "waiters", WaitNode.class);
593591

594592
// Reduce the risk of rare disastrous classloading in first call to
595593
// LockSupport.park: https://bugs.openjdk.org/browse/JDK-8074773

src/java.base/share/classes/java/util/concurrent/Phaser.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
package java.util.concurrent;
3737

38+
import jdk.internal.invoke.MhUtil;
39+
3840
import java.lang.invoke.MethodHandles;
3941
import java.lang.invoke.VarHandle;
4042
import java.util.concurrent.atomic.AtomicReference;
@@ -1137,15 +1139,9 @@ public boolean block() {
11371139
}
11381140

11391141
// VarHandle mechanics
1140-
private static final VarHandle STATE;
1142+
private static final VarHandle STATE = MhUtil.findVarHandle(
1143+
MethodHandles.lookup(), "state", long.class);
11411144
static {
1142-
try {
1143-
MethodHandles.Lookup l = MethodHandles.lookup();
1144-
STATE = l.findVarHandle(Phaser.class, "state", long.class);
1145-
} catch (ReflectiveOperationException e) {
1146-
throw new ExceptionInInitializerError(e);
1147-
}
1148-
11491145
// Reduce the risk of rare disastrous classloading in first call to
11501146
// LockSupport.park: https://bugs.openjdk.org/browse/JDK-8074773
11511147
Class<?> ensureLoaded = LockSupport.class;

0 commit comments

Comments
 (0)