JDK 9 RFR of JDK-8168681: Correct deprecation text for Class.newInstance

Mandy Chung mandy.chung at oracle.com
Thu Nov 3 05:01:00 UTC 2016


> On Nov 2, 2016, at 5:51 PM, joe darcy <joe.darcy at oracle.com> wrote:
> 
> :
> The getConstructor call only returns *public* constructors while newInstance can call non-public constructors too, subject to the appropriate security checks.
> 
> 
> Therefore, using "getDeclaredConstructor()" is a better replacement since it should be able to find and call non-public constructors when that is permissible. The patch for this is:
> 
> --- a/src/java.base/share/classes/java/lang/Class.java    Wed Nov 02 16:24:43 2016 -0700
> +++ b/src/java.base/share/classes/java/lang/Class.java    Wed Nov 02 17:36:25 2016 -0700
> @@ -485,7 +485,7 @@
>      * can be replaced by
>      *
>      * <pre>{@code
> -     * clazz.getConstructor().newInstance()
> +     * clazz.getDeclaredConstructor().newInstance()
>      * }</pre>
>      *
>      * The latter sequence of calls is inferred to be able to throw
> 

+1

Mandy




More information about the core-libs-dev mailing list