Skip to content

8364461: JFR: Default constructor may not be first in setting control #26582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/jdk.jfr/share/classes/jdk/jfr/internal/EventControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,20 +285,25 @@ private void defineSetting(Class<? extends SettingControl> settingsClass, Method

private SettingControl instantiateSettingControl(Class<? extends SettingControl> 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<? extends SettingControl> 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
Expand Down
10 changes: 9 additions & 1 deletion test/jdk/jdk/jfr/api/settings/RegExpControl.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
Expand Down