ghost-discord / ghost

🤖 A modular, multi-feature Discord bot
GNU General Public License v3.0
23 stars 31 forks source link

[Bug] properties can only be accessed in invoke method #28

Closed LeMikaelF closed 4 years ago

LeMikaelF commented 5 years ago

Describe the bug

Trying to access Ghost2Application.getApplicationInstance().getConfig() in a module's field or initializer block will throw a NullPointerException and cause the module to not get loaded, and output:

ERROR: Module subclass com.github.coleb1911.ghost2.commands.modules.XXXX is written incorrectly. Refer to the wiki for help. (Reason(s): Module threw an exception in its constructor. java.lang.NullPointerException)

This error is difficult to debug, since it has no line number attached and since it doesn't necessarily come from the module's constructor. It can also come from a field or an initializer block.

Reproduction Steps to reproduce the bug.

  1. Create a module
  2. Access Ghost2Application.getApplicationInstance().getConfig() before the invoke method is called (for example in the initializer block, field or constructor).
  3. Query the bot on Discord for the relevant command.
  4. The bot will not respond, and the console will show above message.

Expected behavior The system properties should be accessible during creation of the module, for example if one wants to store them in final fields. The current API design doesn't warn against it or prevent it, and leads to potential bugs.

Actual behavior Ghost2Application.getApplicationInstance() returns null if called before invoke method.

Platform

zzzowoey commented 5 years ago

Because of the way Spring context initialization works, the CommandRegistry held by the dispatcher gets initialized before the application context exists, so Module instances are pretty much unable to access any fields from the main class at initialization. I think the only way to fix this is to just move config initialization into getConfig().

I'll fix this sometime before 1.1.

zzzowoey commented 4 years ago

Staged for fix in ecdc9fd.

zzzowoey commented 4 years ago

Fixed on master: 7947559