Avoid usage of deprecated newInstance() method#1172
Merged
ptziegler merged 1 commit intoeclipse-windowbuilder:masterfrom Jul 18, 2025
Merged
Avoid usage of deprecated newInstance() method#1172ptziegler merged 1 commit intoeclipse-windowbuilder:masterfrom
ptziegler merged 1 commit intoeclipse-windowbuilder:masterfrom
Conversation
b5ccee7 to
5fa1622
Compare
ptziegler
commented
Jul 17, 2025
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; | ||
| } |
Contributor
Author
There was a problem hiding this comment.
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().
5fa1622 to
1060ca2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As per the documentation, one can replace the call to clazz.newInstance() with clazz.getDeclaredConstructor().newInstance().