diff --git a/src/java.base/share/classes/jdk/internal/javac/ParticipatesInPreview.java b/src/java.base/share/classes/jdk/internal/javac/ParticipatesInPreview.java new file mode 100644 index 00000000000..c291ca9f737 --- /dev/null +++ b/src/java.base/share/classes/jdk/internal/javac/ParticipatesInPreview.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package jdk.internal.javac; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Indicates, when declared on a module declaration, that the module participates + * in preview features and therefore does not need to be compiled with "--enable-preview". + */ +@Target(ElementType.MODULE) +@Retention(RetentionPolicy.CLASS) +public @interface ParticipatesInPreview { +} diff --git a/src/java.base/share/classes/module-info.java b/src/java.base/share/classes/module-info.java index e280defabe0..40f777b6635 100644 --- a/src/java.base/share/classes/module-info.java +++ b/src/java.base/share/classes/module-info.java @@ -141,9 +141,17 @@ module java.base { jdk.compiler; exports com.sun.security.ntlm to java.security.sasl; + // Note: all modules in the exported list participate in preview features + // and therefore if they use preview features they do not need to be + // compiled with "--enable-preview". + // It is recommended for any modules that do participate that their + // module declaration be annotated with jdk.internal.javac.ParticipatesInPreview exports jdk.internal.javac to java.compiler, + java.management, // participates in preview features jdk.compiler, + jdk.incubator.concurrent, // participates in preview features + jdk.incubator.vector, // participates in preview features jdk.jdi, jdk.jfr, jdk.jshell, diff --git a/src/java.management/share/classes/java/lang/management/ThreadInfo.java b/src/java.management/share/classes/java/lang/management/ThreadInfo.java index 4b957ba1f1f..0038d2a1c29 100644 --- a/src/java.management/share/classes/java/lang/management/ThreadInfo.java +++ b/src/java.management/share/classes/java/lang/management/ThreadInfo.java @@ -29,8 +29,6 @@ import javax.management.openmbean.ArrayType; import javax.management.openmbean.CompositeData; import sun.management.ManagementFactoryHelper; import sun.management.ThreadInfoCompositeData; -import sun.management.Util; -import static java.lang.Thread.State.*; /** * Thread information. {@code ThreadInfo} contains the information @@ -226,7 +224,7 @@ public class ThreadInfo { StackTraceElement[] stackTrace, MonitorInfo[] lockedMonitors, LockInfo[] lockedSynchronizers) { - this.virtual = Util.isVirtual(t); + this.virtual = t.isVirtual(); this.threadId = t.threadId(); this.threadName = t.getName(); this.threadState = ManagementFactoryHelper.toThreadState(state); diff --git a/src/java.management/share/classes/module-info.java b/src/java.management/share/classes/module-info.java index 3396b6a2f40..f86c6b752a5 100644 --- a/src/java.management/share/classes/module-info.java +++ b/src/java.management/share/classes/module-info.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,6 +23,8 @@ * questions. */ +import jdk.internal.javac.ParticipatesInPreview; + /** * Defines the Java Management Extensions (JMX) API. *
@@ -35,6 +37,7 @@
* @moduleGraph
* @since 9
*/
+@ParticipatesInPreview
module java.management {
exports java.lang.management;
diff --git a/src/java.management/share/classes/sun/management/ThreadImpl.java b/src/java.management/share/classes/sun/management/ThreadImpl.java
index 04a77166bd3..90cc8f3d1c4 100644
--- a/src/java.management/share/classes/sun/management/ThreadImpl.java
+++ b/src/java.management/share/classes/sun/management/ThreadImpl.java
@@ -31,7 +31,6 @@ import java.lang.management.ThreadMXBean;
import java.util.stream.Stream;
import javax.management.ObjectName;
import java.util.Objects;
-import sun.management.Util;
/**
* Implementation for java.lang.management.ThreadMXBean as well as providing the
@@ -222,7 +221,7 @@ public class ThreadImpl implements ThreadMXBean {
private boolean verifyCurrentThreadCpuTime() {
// check if Thread CPU time measurement is supported.
- if (Util.isVirtual(Thread.currentThread())) {
+ if (Thread.currentThread().isVirtual()) {
throw new UnsupportedOperationException("Not supported by virtual threads");
}
if (!isCurrentThreadCpuTimeSupported()) {
@@ -284,7 +283,7 @@ public class ThreadImpl implements ThreadMXBean {
long id = ids[0];
Thread thread = Thread.currentThread();
if (id == thread.threadId()) {
- if (Util.isVirtual(thread)) {
+ if (thread.isVirtual()) {
times[0] = -1;
} else {
times[0] = getThreadTotalCpuTime0(0);
@@ -327,7 +326,7 @@ public class ThreadImpl implements ThreadMXBean {
long id = ids[0];
Thread thread = Thread.currentThread();
if (id == thread.threadId()) {
- if (Util.isVirtual(thread)) {
+ if (thread.isVirtual()) {
times[0] = -1;
} else {
times[0] = getThreadUserCpuTime0(0);
@@ -361,7 +360,7 @@ public class ThreadImpl implements ThreadMXBean {
}
protected long getCurrentThreadAllocatedBytes() {
- if (isThreadAllocatedMemoryEnabled() && !Util.isVirtual(Thread.currentThread())) {
+ if (isThreadAllocatedMemoryEnabled() && !Thread.currentThread().isVirtual()) {
return getThreadAllocatedMemory0(0);
}
return -1;
@@ -377,7 +376,7 @@ public class ThreadImpl implements ThreadMXBean {
if (verified) {
Thread thread = Thread.currentThread();
if (id == thread.threadId()) {
- if (Util.isVirtual(thread)) {
+ if (thread.isVirtual()) {
return -1L;
} else {
return getThreadAllocatedMemory0(0);
@@ -577,7 +576,7 @@ public class ThreadImpl implements ThreadMXBean {
*/
private static long[] platformThreadIds(Thread[] threads) {
return Stream.of(threads)
- .filter(t -> !Util.isVirtual(t))
+ .filter(t -> !t.isVirtual())
.mapToLong(Thread::threadId)
.toArray();
}
diff --git a/src/java.management/share/classes/sun/management/Util.java b/src/java.management/share/classes/sun/management/Util.java
index 70cf040db34..80bad1e1523 100644
--- a/src/java.management/share/classes/sun/management/Util.java
+++ b/src/java.management/share/classes/sun/management/Util.java
@@ -25,7 +25,6 @@
package sun.management;
-import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.lang.management.ManagementPermission;
import java.lang.management.ThreadInfo;
@@ -88,20 +87,6 @@ public class Util {
checkAccess(controlPermission);
}
- /**
- * Returns true if the given Thread is a virtual thread.
- *
- * @implNote This method uses reflection because Thread::isVirtual is a preview API
- * and the java.management module cannot be compiled with --enable-preview.
- */
- public static boolean isVirtual(Thread thread) {
- try {
- return (boolean) THREAD_IS_VIRTUAL.invoke(thread);
- } catch (Exception e) {
- throw new InternalError(e);
- }
- }
-
/**
* Returns true if the given ThreadInfo is for a virtual thread.
*/
@@ -113,16 +98,6 @@ public class Util {
}
}
- @SuppressWarnings("removal")
- private static Method threadIsVirtual() {
- PrivilegedExceptionAction