Getting the instance when Constructor#newInstance throws?
Posted
by
Shtééf
on Stack Overflow
See other posts from Stack Overflow
or by Shtééf
Published on 2011-02-20T14:33:31Z
Indexed on
2011/02/20
15:25 UTC
Read the original article
Hit count: 289
I'm working on a simple plugin system, where third party plugins implement a Plugin
interface. A directory of JARs is scanned, and the implementing classes are instantiated with Constructor#newInstance
.
The thing is, these plugins call back into register*
methods of the plugin host. These registrations use the Plugin
instance as a handle. My problem is how to clean up these registrations if the constructor decides to fail and throw halfway through.
InvocationTargetException
doesn't seem to have anything on it to get the instance. Is there a way to get at the instance of an exception throwing constructor?
P.S.: It's typically strongly advised to users that the constructor not do anything, but in practice people are doing it any ways.
© Stack Overflow or respective owner