Paper
Paper copied to clipboard
Add support for private constructors in plugin main classes
Adds support for private constructors in plugin main classes, using Constructor#setAccessible(boolean), allowing for better encapsulation.
What is the benefit of allowing private constructors to be used?
What is the benefit of allowing private constructors to be used?
The only benefit I can think of would be that it makes sure people, who use your plugin's API, which may come with internal classes exposed, cannot accidentally create an instance of your plugin's main class.
A potential solution to this is making the constructor package-private and creating the plugin instance with a plugin bootstrapper, but I can see how this might be non-ideal.
Doesn't creating a new javaplugin instance result in an exception already? I thought it did...
What Strokkur said is pretty much what I was thinking. I’ll admit I wasn’t aware of an Exception when making new JavaPlugin instances, but even so, I don’t think it would hurt to prevent it at compile time.
I think its probably fine to allow non-public constructors*.
However, can you also please update ProviderUtil#loadClass? This way this applies to paper plugins too.
Isn't setAccessible unofficially deprecated? I think the replacement is called trySetAccessible
However, can you also please update ProviderUtil#loadClass? This way this applies to paper plugins too.
Done
Isn't
setAccessibleunofficially deprecated? I think the replacement is calledtrySetAccessible
I looked at trySetAccessible, the only difference is that instead of throwing an InaccessibleObjectException, it returns false, but it can still throw a SecurityException.