From df7d6e081ff9513fbd6cff5d033a307e6798418b Mon Sep 17 00:00:00 2001 From: Kevin Walls Date: Tue, 15 Oct 2024 10:50:45 +0000 Subject: [PATCH] 8338603: DiagnosticCommandMBean operations should standardize types for parameters Reviewed-by: cjplummer, egahlin --- .../internal/DiagnosticCommandImpl.java | 20 +++++++- .../DiagnosticCommandMBean/DcmdMBeanTest.java | 46 ++++++++++++++++--- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/jdk.management/share/classes/com/sun/management/internal/DiagnosticCommandImpl.java b/src/jdk.management/share/classes/com/sun/management/internal/DiagnosticCommandImpl.java index be84beb03bb..eee0ea051d4 100644 --- a/src/jdk.management/share/classes/com/sun/management/internal/DiagnosticCommandImpl.java +++ b/src/jdk.management/share/classes/com/sun/management/internal/DiagnosticCommandImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2024, 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 @@ -317,7 +317,7 @@ public class DiagnosticCommandImpl extends NotificationEmitterSupport for (DiagnosticCommandArgumentInfo arginfo : w.info.getArgumentsInfo()) { HashMap argmap = new HashMap<>(); argmap.put("dcmd.arg.name", arginfo.getName()); - argmap.put("dcmd.arg.type", arginfo.getType()); + argmap.put("dcmd.arg.type", sanitiseType(arginfo.getType())); argmap.put("dcmd.arg.description", arginfo.getDescription()); argmap.put("dcmd.arg.isMandatory", arginfo.isMandatory()); argmap.put("dcmd.arg.isMultiple", arginfo.isMultiple()); @@ -335,6 +335,22 @@ public class DiagnosticCommandImpl extends NotificationEmitterSupport return new ImmutableDescriptor(map); } + // Type names that will be published in dcmd.arg.type: + private static final String [] publicTypes = new String [] { "INT", "STRING", "BOOLEAN", "STRING SET", "MEMORY SIZE", "NANOTIME" }; + + private static final String sanitiseType(String typeName) { + // For any typeName not in the set to be made public, return "STRING". + if (typeName == null) { + return null; + } + for (String t : publicTypes) { + if (typeName.equals(t)) { + return t; + } + } + return "STRING"; + } + private static final String notifName = "javax.management.Notification"; diff --git a/test/jdk/com/sun/management/DiagnosticCommandMBean/DcmdMBeanTest.java b/test/jdk/com/sun/management/DiagnosticCommandMBean/DcmdMBeanTest.java index 3cc9f40b895..ba6fffd0670 100644 --- a/test/jdk/com/sun/management/DiagnosticCommandMBean/DcmdMBeanTest.java +++ b/test/jdk/com/sun/management/DiagnosticCommandMBean/DcmdMBeanTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2024, 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,7 +23,7 @@ /* * @test - * @bug 7150256 + * @bug 7150256 8338603 * @summary Basic Test for the DiagnosticCommandMBean * @author Frederic Parain, Shanliang JIANG * @@ -68,10 +68,14 @@ public class DcmdMBeanTest { System.out.println("Description:" + info.getDescription()); MBeanOperationInfo[] opInfo = info.getOperations(); System.out.println("Operations:"); + int operationFailures = 0; for (int i = 0; i < opInfo.length; i++) { - printOperation(opInfo[i]); + operationFailures += printOperation(opInfo[i]); System.out.println("\n@@@@@@\n"); } + if (operationFailures > 0) { + throw new RuntimeException("FAILED. " + operationFailures + " operations found with non-standard parameter types."); + } } finally { try { cc.close(); @@ -83,7 +87,12 @@ public class DcmdMBeanTest { System.out.println("Test passed"); } - static void printOperation(MBeanOperationInfo info) { + /** + * Print an Operation, and check for any non-standard parameter types. + * Return the number of failed parameters, so the caller can signal to fail the test. + */ + static int printOperation(MBeanOperationInfo info) { + int failures = 0; System.out.println("Name: "+info.getName()); System.out.println("Description: "+info.getDescription()); System.out.println("Return Type: "+info.getReturnType()); @@ -100,8 +109,16 @@ public class DcmdMBeanTest { Descriptor desc3 = (Descriptor)desc2.getFieldValue(desc2.getFieldNames()[j]); for(int k=0; k