Skip to content

Conversation

@ptziegler
Copy link
Contributor

As per the documentation, one can replace the call to clazz.newInstance() with clazz.getDeclaredConstructor().newInstance().

@ptziegler ptziegler added this to the 1.21.0 milestone Jul 17, 2025
@ptziegler ptziegler force-pushed the deprecated-class-constructor branch from b5ccee7 to 5fa1622 Compare July 17, 2025 18:08
Comment on lines -1420 to -1428
private ExceptionThrower() throws Throwable {
if (System.getProperty("wbp.ReflectionUtils.propagate().InstantiationException") != null) {
throw new InstantiationException();
}
if (System.getProperty("wbp.ReflectionUtils.propagate().IllegalAccessException") != null) {
throw new IllegalAccessException();
}
throw throwable;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very nasty piece of code. Creating an instance of this class via clazz.newInstance() bypasses the compiler check that allows you to throw an Exception without having to catch it.

By using clazz.getDeclaredConstructor().newInstance() this exception is wrapped inside an InvocationTargetException and thus defeating the entire point of this class.

While it's still a very questionable implementation, the new approach takes advantage of type erasure to make the compiler think the Exception is actually a RuntimeException.

As per the documentation, one can replace the call to
clazz.newInstance() with clazz.getDeclaredConstructor().newInstance().
@ptziegler ptziegler force-pushed the deprecated-class-constructor branch from 5fa1622 to 1060ca2 Compare July 17, 2025 18:46
@ptziegler ptziegler merged commit 9a2363c into eclipse-windowbuilder:master Jul 18, 2025
8 checks passed
@ptziegler ptziegler deleted the deprecated-class-constructor branch July 18, 2025 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant