Hi Roger,

On 6/13/2016 6:59 AM, Roger Riggs wrote:
Hi,

That example kind of glosses over the need to handle 3 new exceptions.
That's been my annoyance with this change.

throws InstantiationException, IllegalAccessException, InvocationTargetException

The patch sent to the list (http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-June/041754.html), did note addition exception types are thrown by the replacement code. The proposed text with Max's improvement is

+     * <p>The call
+     *
+     * <pre>{@code
+     * clazz.newInstance()
+     * }</pre>
+     *
+     * can be replaced by
+     *
+     * <pre>{@code
+     * clazz.getConstructor().newInstance()
+     * }</pre>
+     *
+     * The latter sequence of calls is inferred to be able to throw
+     * the additional exception types {@link
+     * InvocationTargetException} and {@link
+     * NoSuchMethodException}. Both of these exception types are
+     * subclasses of {@link ReflectiveOperationException}.
+     *

Class.newInstance is declared to throw InstantiationException and IllegalAccessException. Its implementation internally catches NoSuchMethodException and rethrows it as something else.

I considered adding more text to guide developers more explicitly to either use multi-catch

    catch(InstantiationException  | IllegalAccessException e) =>
catch(InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e)

or catching ReflectiveOperationException

    catch(InstantiationException  | IllegalAccessException e) =>
    catch(ReflectiveOperationException e) =>

With the more precise rethrow feature added in 7, catching ReflectiveOperationException and then rethrowing it doesn't lose any type information.

I was hesitant to add such text as to not overwhelm further the non-deprecated text of the method. However, if people think such guidance is warranted, I'll craft something.

Thanks,

-Joe



Roger



On 6/12/2016 11:43 PM, joe darcy wrote:

On 6/12/2016 7:21 PM, Wang Weijun wrote:
Why not just clazz.getConstructor().newInstance()?

That seems to work to and is much shorter; thanks :-)

-Joe


+     * can be replaced by
+     *
+     * <pre>{@code
+ * clazz.getConstructor(new Class<?>[0]).newInstance((Object[])null);
+     * }</pre>



Reply via email to