From 00b9048d59e8800a59560425d11bbe13d44a60a6 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 13 Nov 2020 18:00:21 +0000 Subject: [PATCH] [github-197] Modify test class HeapDump to allow it work in different JDKs. Thanks to Marius Volkhart. This closes #197 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1883393 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/stress/HeapDump.java | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/integrationtest/org/apache/poi/stress/HeapDump.java b/src/integrationtest/org/apache/poi/stress/HeapDump.java index 47736b504f..098230cad1 100644 --- a/src/integrationtest/org/apache/poi/stress/HeapDump.java +++ b/src/integrationtest/org/apache/poi/stress/HeapDump.java @@ -18,8 +18,11 @@ package org.apache.poi.stress; import java.io.IOException; import java.lang.management.ManagementFactory; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import com.sun.management.HotSpotDiagnosticMXBean; +import org.apache.poi.ss.formula.eval.NotImplementedException; import org.apache.poi.util.SuppressForbidden; @SuppressForbidden("class only exists for manual tests in XSSFFileHandler") @@ -40,9 +43,18 @@ public class HeapDump { * only the live objects */ public static void dumpHeap(String fileName, boolean live) throws IOException { - // initialize hotspot diagnostic MBean - initHotspotMBean(); - hotspotMBean.dumpHeap(fileName, live); + try { + if (isIbmVm()) { + dumpHeapJ9(fileName); + } else { + + // initialize hotspot diagnostic MBean + initHotspotMBean(); + dumpHeapHotSpot(fileName, live); + } + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) { + throw new RuntimeException(e); + } } // initialize the hotspot diagnostic MBean field @@ -59,6 +71,28 @@ public class HeapDump { // get the hotspot diagnostic MBean from the platform MBean server private static HotSpotDiagnosticMXBean getHotspotMBean() throws IOException { return ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), - HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class); + HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class); } -} + + private static boolean isIbmVm() { + try { + Class.forName("com.ibm.jvm.Dump"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } + + private static void dumpHeapJ9(String fileName) throws ClassNotFoundException, NoSuchMethodException, + InvocationTargetException, IllegalAccessException { + Class dump = Class.forName("com.ibm.jvm.Dump"); + Method heapDumpToFile = dump.getMethod("heapDumpToFile", String.class); + heapDumpToFile.invoke(dump, fileName); + } + + private static void dumpHeapHotSpot(String fileName, boolean live) throws NoSuchMethodException, + InvocationTargetException, IllegalAccessException { + Method dumpHeap = hotspotMBean.getClass().getMethod("dumpHeap", String.class, boolean.class); + dumpHeap.invoke(hotspotMBean, fileName, live); + } +} \ No newline at end of file