Skip to content

Conversation

MiniDigger
Copy link
Contributor

also ran some intellij inspections.

@MiniDigger
Copy link
Contributor Author

MiniDigger commented Jan 19, 2018

possible improvements:

  • more default dependencies
  • injecting of subtypes, currently only exactly matching types are injected
  • allow injection of classes that were not registered via a @singleton like syntax on those classes

Copy link
Owner

@aikar aikar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all .toArray with 0 changes. It's more optimized to pass the intended size, or else the toArray call is going to throw away the array you passed and recreate it anyways. Lets not undo already done optimizations.


throw new InvalidCommandArgument(false);
}
return players.toArray(new OnlinePlayer[players.size()]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo this change

}

String[] result = args.toArray(new String[args.size()]);
String[] result = args.toArray(new String[0]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo this change

* @throws IllegalArgumentException when instance isn't an instance of clazz
* @throws IllegalStateException when there is already an instance for the provided class registered
*/
public void registerDependency(Class<?> clazz, Object instance){
Copy link
Owner

@aikar aikar Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use <T> with Class <T> clazz, T instance, to require it actually satisfies the class its providing for.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generic T, github stripped it...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public <T> void registerDependency(Class<T> clazz, T instance) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this and it didn't work. when I want to pass the plugins class, I can't cast to it, thus throwing a compiler error. https://i.imgur.com/ynHskze.png

* @throws IllegalArgumentException when instance isn't an instance of clazz
* @throws IllegalStateException when there is already an instance for the provided class registered
*/
public void registerNamedDependency(Class<?> clazz, String key, Object instance){
Copy link
Owner

@aikar aikar Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above for <T>, but just make this registerDependency, since its a different signature

* @throws IllegalStateException when there is already an instance for the provided class registered
*/
public void registerNamedDependency(Class<?> clazz, String key, Object instance){
if(!clazz.isInstance(instance)){
Copy link
Owner

@aikar aikar Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary with <T> restrictions

throw new IllegalArgumentException(instance.getClass().getName() + " isn't a subclass of " + clazz.getName() + "!");
}
if(dependencies.containsRow(clazz) && dependencies.containsColumn(key)){
throw new IllegalStateException("There is already an instance of " + clazz.getName() + " with the key " + key + " registered!");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we should throw, but just let it overwrite, maybe return old, like a typical put operation.

*
* @param baseCommand the instance which fields should be filled
*/
public void inject(BaseCommand baseCommand){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make package private

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think also more descriptive "injectDependencies"

Map<String, Object> resolveContexts(CommandIssuer sender, List<String> args, int argLimit) throws InvalidCommandArgument {
args = Lists.newArrayList(args);
String[] origArgs = args.toArray(new String[args.size()]);
String[] origArgs = args.toArray(new String[0]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo change

throw new InvalidCommandArgument(false);
}
return players.toArray(new OnlinePlayer[players.size()]);
return players.toArray(new OnlinePlayer[0]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo change

@MiniDigger
Copy link
Contributor Author

if some bungee or sponge ppl could suggest default dependencies or the platform that would be great.
suggestions for bukkit are welcome too.

Copy link
Owner

@aikar aikar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just few more changes then looks good to me.

try {
field.setAccessible(true);
field.set(baseCommand, object);
field.setAccessible(false);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know if theres a reason to set this to false? what if it was already accessible before.

* @param baseCommand the instance which fields should be filled
*/
void injectDependencies(BaseCommand baseCommand){
for (Field field : baseCommand.getClass().getDeclaredFields()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If getDeclaredFields is the one that only includes its own fields, then switch to the other. we want to include parent class fields too, but I'm not at a java doc atm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when we don't use getDeclaredFields, we can't inject private fields. do we want to just iterate over both?


//TODO more default dependencies for sponge
registerDependency(plugin.getClass(), plugin);
registerDependency(PluginContainer.class, plugin);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the PluginContainer one since per @kashike its not the same thing.

@aikar aikar changed the title [WIP] Add dependency injection functionality, see #85 [WIP] Add dependency injection functionality, Closes #85 Jan 20, 2018
@aikar aikar merged commit cebe28e into aikar:master Jan 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants