diff --git a/src/jdk.jfr/share/classes/jdk/jfr/internal/EventControl.java b/src/jdk.jfr/share/classes/jdk/jfr/internal/EventControl.java index 02775b7707a02..7a8d490d6e530 100644 --- a/src/jdk.jfr/share/classes/jdk/jfr/internal/EventControl.java +++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/EventControl.java @@ -285,20 +285,25 @@ private void defineSetting(Class settingsClass, Method private SettingControl instantiateSettingControl(Class settingControlClass) throws IllegalAccessException, InstantiationException { SecuritySupport.makeVisibleToJFR(settingControlClass); - final Constructor cc; - try { - cc = settingControlClass.getDeclaredConstructors()[0]; - } catch (Exception e) { - throw (Error) new InternalError("Could not get constructor for " + settingControlClass.getName()).initCause(e); - } - cc.setAccessible(true); try { + Constructor cc = findDefaultConstructor(settingControlClass); + cc.setAccessible(true); return (SettingControl) cc.newInstance(); } catch (IllegalArgumentException | InvocationTargetException e) { throw new InternalError("Could not instantiate setting for class " + settingControlClass.getName()); } } + private Constructor findDefaultConstructor(Class settingControlClass) { + for (Constructor c : settingControlClass.getDeclaredConstructors()) { + if (c.getParameterCount() == 0) { + return c; + } + } + // Programming error by user, fail fast + throw new InstantiationError("Could not find default constructor for " + settingControlClass.getName()); + } + private static Control defineEnabled(PlatformEventType type) { // Java events are enabled by default, // JVM events are not, maybe they should be? Would lower learning curve diff --git a/test/jdk/jdk/jfr/api/settings/RegExpControl.java b/test/jdk/jdk/jfr/api/settings/RegExpControl.java index 051cfb3aa09b0..182cd3998b94a 100644 --- a/test/jdk/jdk/jfr/api/settings/RegExpControl.java +++ b/test/jdk/jdk/jfr/api/settings/RegExpControl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2025, 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 @@ -31,6 +31,14 @@ public final class RegExpControl extends SettingControl { private Pattern pattern = Pattern.compile(".*"); + // Purpose of this constructor is to ensure that the correct + // constructor is picked when the event class is registered + public RegExpControl(String dummy) { + } + + public RegExpControl() { + } + public void setValue(String value) { this.pattern = Pattern.compile(value); }