diff --git a/spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java b/spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java index b96c56053c4..a177b16d63d 100644 --- a/spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java +++ b/spring-context/src/test/java/org/springframework/util/MBeanTestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,21 +18,26 @@ package org.springframework.util; import java.lang.management.ManagementFactory; import java.lang.reflect.Field; +import java.util.EnumSet; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; +import org.junit.jupiter.api.condition.JRE; + /** * Utilities for MBean tests. * * @author Phillip Webb + * @author Sam Brannen */ public class MBeanTestUtils { /** - * Resets MBeanServerFactory and ManagementFactory to a known consistent state. - *

This involves releasing all currently registered MBeanServers and resetting - * the platformMBeanServer to null. + * Resets {@link MBeanServerFactory} to a known consistent state. This involves + * releasing all currently registered MBeanServers. + *

On JDK 8 - JDK 16, this method also resets the platformMBeanServer field + * in {@link ManagementFactory} to {@code null}. */ public static synchronized void resetMBeanServers() throws Exception { for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) { @@ -46,9 +51,15 @@ public class MBeanTestUtils { } } - Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer"); - field.setAccessible(true); - field.set(null, null); + if (!isCurrentJreWithinRange(JRE.JAVA_16, JRE.OTHER)) { + Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer"); + field.setAccessible(true); + field.set(null, null); + } + } + + static boolean isCurrentJreWithinRange(JRE min, JRE max) { + return EnumSet.range(min, max).contains(JRE.currentVersion()); } } diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml index b24ed59d9cb..d42634b45d5 100644 --- a/src/checkstyle/checkstyle-suppressions.xml +++ b/src/checkstyle/checkstyle-suppressions.xml @@ -4,7 +4,7 @@ - +