Java 9 deprecates Class.newInstance method

Description

Deprecation text:

This method propagates any exception thrown by the nullary constructor, including a checked exception. Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. The Constructor.newInstance method avoids this problem by wrapping any exception thrown by the constructor in a (checked) InvocationTargetException.
The call

clazz.newInstance()

can be replaced by

clazz.getDeclaredConstructor().newInstance()

The latter sequence of calls is inferred to be able to throw the additional exception types InvocationTargetException and NoSuchMethodException. Both of these exception types are subclasses of ReflectiveOperationException.
Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.

Environment

None

Activity

Brent Putman
February 20, 2020 at 1:14 AM

It appears all these have been addressed.

Ian Young
February 26, 2018 at 10:29 AM

I should note that although this method is deprecated in Java 9, it is not deprecated for removal either in Java 9 or the Java 10 RC. So the soonest it could be deprecated for removal would be Java 11 and the earliest it could be removed from the API would be Java 12 (March 2019).

Ian Young
February 26, 2018 at 10:20 AM

I would be surprised if Spring was using the newInstance method rather than the full introspection framework.

Even if it was, though, I think it's in their court to fix their stuff before Java 11, if they intend to support running under Java 11.

That's another question but I think an important one. Obviously Spring 5 is going to track Java 11 but I haven't seen anything to tell us whether Spring 4 will. Does anyone have a reference?

Scott Cantor
February 23, 2018 at 2:49 PM

I can only go by personal experience, but the Eclipse and Spring tools have never successfully searched anything for me apart from just searching text, so I do search the XML that way.

Rod Widdowson
February 23, 2018 at 11:17 AM

Something just occurred to me.  This could be being done in Spring which would be just so much fun.  Are the IDE's smart enough to infer these these days?  Or do you grep *.java *.xml on the string?

Fixed

Details

Assignee

Reporter

Created February 22, 2018 at 3:58 PM
Updated February 20, 2020 at 1:14 AM
Resolved February 20, 2020 at 1:14 AM