From b7ae0ae1d7481e66a07f40bf01c5614fdf44c2ed Mon Sep 17 00:00:00 2001 From: Erik Gahlin Date: Fri, 17 May 2024 14:06:08 +0000 Subject: [PATCH] 8328572: JFR: Use Class.forPrimitiveName(String) Reviewed-by: mgronlun --- .../classes/jdk/jfr/internal/util/Utils.java | 79 +++++-------------- 1 file changed, 18 insertions(+), 61 deletions(-) diff --git a/src/jdk.jfr/share/classes/jdk/jfr/internal/util/Utils.java b/src/jdk.jfr/share/classes/jdk/jfr/internal/util/Utils.java index c133a11b3c5..53b3599f9d9 100644 --- a/src/jdk.jfr/share/classes/jdk/jfr/internal/util/Utils.java +++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/util/Utils.java @@ -29,6 +29,7 @@ import java.io.IOException; import java.io.RandomAccessFile; import java.lang.annotation.Annotation; import java.lang.annotation.Repeatable; +import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -37,7 +38,6 @@ import java.nio.file.Path; import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Comparator; import java.util.HashMap; import java.util.List; @@ -243,67 +243,24 @@ public final class Utils { } public static Object makePrimitiveArray(String typeName, List values) { - int length = values.size(); - switch (typeName) { - case "int": - int[] ints = new int[length]; - for (int i = 0; i < length; i++) { - ints[i] = (int) values.get(i); - } - return ints; - case "long": - long[] longs = new long[length]; - for (int i = 0; i < length; i++) { - longs[i] = (long) values.get(i); - } - return longs; - - case "float": - float[] floats = new float[length]; - for (int i = 0; i < length; i++) { - floats[i] = (float) values.get(i); - } - return floats; - - case "double": - double[] doubles = new double[length]; - for (int i = 0; i < length; i++) { - doubles[i] = (double) values.get(i); - } - return doubles; - - case "short": - short[] shorts = new short[length]; - for (int i = 0; i < length; i++) { - shorts[i] = (short) values.get(i); - } - return shorts; - case "char": - char[] chars = new char[length]; - for (int i = 0; i < length; i++) { - chars[i] = (char) values.get(i); - } - return chars; - case "byte": - byte[] bytes = new byte[length]; - for (int i = 0; i < length; i++) { - bytes[i] = (byte) values.get(i); - } - return bytes; - case "boolean": - boolean[] booleans = new boolean[length]; - for (int i = 0; i < length; i++) { - booleans[i] = (boolean) values.get(i); - } - return booleans; - case "java.lang.String": - String[] strings = new String[length]; - for (int i = 0; i < length; i++) { - strings[i] = (String) values.get(i); - } - return strings; + Class componentType = makePrimitiveType(typeName); + if (componentType == null) { + return null; } - return null; + int length = values.size(); + Object array = Array.newInstance(componentType, length); + for (int index = 0; index < length; index++) { + Array.set(array, index, values.get(index)); + } + return array; + } + + private static Class makePrimitiveType(String typeName) { + return switch(typeName) { + case "void" -> null; + case "java.lang.String" -> String.class; + default -> Class.forPrimitiveName(typeName); + }; } public static boolean isSettingVisible(long typeId, boolean hasEventHook) {