Quellcode durchsuchen

Rework BaseCommandPlugin to be daggery.

Getting hold of a CommandController is annoying and fiddly,
so add a CommandHelper class to hide that away from plugins.

Allow registering of a command given just its class/info.

Change-Id: Ia208db1f23e3356d83a87ff2372e4f7aa49b24cc
Reviewed-on: http://gerrit.dmdirc.com/2854
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8rc1
Chris Smith vor 10 Jahren
Ursprung
Commit
37d92ed2d3

+ 58
- 10
src/com/dmdirc/plugins/implementations/BaseCommandPlugin.java Datei anzeigen

@@ -24,12 +24,15 @@ package com.dmdirc.plugins.implementations;
24 24
 import com.dmdirc.commandparser.CommandInfo;
25 25
 import com.dmdirc.commandparser.commands.Command;
26 26
 import com.dmdirc.interfaces.CommandController;
27
+import com.dmdirc.plugins.PluginInfo;
27 28
 
28 29
 import java.util.HashMap;
29 30
 import java.util.Map;
30 31
 
31 32
 import lombok.AccessLevel;
32
-import lombok.Getter;
33
+import lombok.Setter;
34
+
35
+import dagger.ObjectGraph;
33 36
 
34 37
 /**
35 38
  * Implementation of {@link BasePlugin} that maintains commands.
@@ -39,28 +42,37 @@ public abstract class BaseCommandPlugin extends BasePlugin {
39 42
     /**
40 43
      * List of commands to load and unload.
41 44
      */
42
-    private final Map<CommandInfo, Command> commands =
43
-            new HashMap<CommandInfo, Command>();
45
+    private final Map<CommandInfo, Command> commands = new HashMap<>();
46
+
44 47
     /**
45 48
      * Command controller to register commands.
46 49
      */
47
-    @Getter(AccessLevel.PROTECTED)
48
-    private final CommandController commandController;
50
+    @Setter(AccessLevel.PROTECTED)
51
+    private CommandController commandController;
49 52
 
50 53
     /**
51 54
      * Creates a new instance of this plugin
52 55
      *
53
-     * @param commandController Command controller to register commands
56
+     * @param commandController Command controller to register commands.
57
+     * @deprecated No longer required, use empty constructor.
54 58
      */
59
+    @Deprecated
55 60
     public BaseCommandPlugin(final CommandController commandController) {
56 61
         this.commandController = commandController;
57 62
     }
58 63
 
64
+    /**
65
+     * Creates a new instance of this plugin.
66
+     */
67
+    public BaseCommandPlugin() {
68
+    }
69
+
59 70
     /**
60 71
      * {@inheritDoc}
61 72
      */
62 73
     @Override
63 74
     public void onLoad() {
75
+        super.onLoad();
64 76
         loadCommands();
65 77
     }
66 78
 
@@ -69,20 +81,47 @@ public abstract class BaseCommandPlugin extends BasePlugin {
69 81
      */
70 82
     @Override
71 83
     public void onUnload() {
84
+        super.onUnload();
72 85
         unloadCommands();
73 86
     }
74 87
 
88
+    /** {@inheritDoc} */
89
+    @Override
90
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
91
+        super.load(pluginInfo, graph);
92
+
93
+        commandController = graph.plus(new CommandHelper.CommandHelperModule())
94
+                .get(CommandHelper.class).getCommandController();
95
+    }
96
+
75 97
     /**
76 98
      * Registers a command from this plugin.
77 99
      *
78 100
      * @param command Command to register
79 101
      * @param commandInfo Command info to register
80 102
      */
81
-    protected void registerCommand(final Command command,
82
-            final CommandInfo commandInfo) {
103
+    protected void registerCommand(final Command command, final CommandInfo commandInfo) {
83 104
         commands.put(commandInfo, command);
84 105
     }
85 106
 
107
+    /**
108
+     * Registers a command from this plugin.
109
+     *
110
+     * <p>This method will create a new instance of the specified command class using the
111
+     * dependency-injection framework. It must only be called after
112
+     * {@link #setObjectGraph(dagger.ObjectGraph)}, and any command must be injectable using
113
+     * that object graph.
114
+     *
115
+     * @param <T> The type of the command that will be registered.
116
+     * @param command The class of the command to register.
117
+     * @param commandInfo Command info to register.
118
+     */
119
+    protected <T extends Command> void registerCommand(
120
+            final Class<T> command,
121
+            final CommandInfo commandInfo) {
122
+        commands.put(commandInfo, getObjectGraph().get(command));
123
+    }
124
+
86 125
     /**
87 126
      * Unregisters a command from this plugin.
88 127
      *
@@ -98,8 +137,7 @@ public abstract class BaseCommandPlugin extends BasePlugin {
98 137
      */
99 138
     private void loadCommands() {
100 139
         for (Map.Entry<CommandInfo, Command> command : commands.entrySet()) {
101
-            commandController.registerCommand(
102
-                    command.getValue(), command.getKey());
140
+            commandController.registerCommand(command.getValue(), command.getKey());
103 141
         }
104 142
     }
105 143
 
@@ -111,4 +149,14 @@ public abstract class BaseCommandPlugin extends BasePlugin {
111 149
             commandController.unregisterCommand(command);
112 150
         }
113 151
     }
152
+
153
+    /**
154
+     * Gets the command controller used.
155
+     *
156
+     * @return A command controller.
157
+     * @deprecated Plugins should inject their own controllers.
158
+     */
159
+    protected CommandController getCommandController() {
160
+        return commandController;
161
+    }
114 162
 }

+ 63
- 0
src/com/dmdirc/plugins/implementations/CommandHelper.java Datei anzeigen

@@ -0,0 +1,63 @@
1
+/*
2
+ * Copyright (c) 2006-2013 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.plugins.implementations;
24
+
25
+import com.dmdirc.ClientModule;
26
+import com.dmdirc.interfaces.CommandController;
27
+
28
+import javax.inject.Inject;
29
+
30
+import lombok.Getter;
31
+
32
+import dagger.Module;
33
+
34
+/**
35
+ * Helper class for {@link BaseCommandPlugin} to facilitate retrieving a {@link CommandController}.
36
+ *
37
+ * <p>Because the plugins themselves aren't dependency injected, but require a command controller
38
+ * themselves to sensibly register commands, we use this helper and module to obtain a reference.
39
+ */
40
+@SuppressWarnings("PMD.UnusedPrivateField")
41
+public class CommandHelper {
42
+
43
+    /** The command controller. */
44
+    @Getter
45
+    private final CommandController commandController;
46
+
47
+    /**
48
+     * Creates a new instance of this class.
49
+     *
50
+     * @param commandController The command controller to return.
51
+     */
52
+    @Inject
53
+    public CommandHelper(final CommandController commandController) {
54
+        this.commandController = commandController;
55
+    }
56
+
57
+    /** Module that allows inflation of {@link CommandHelper}. */
58
+    @Module(injects = CommandHelper.class, addsTo = ClientModule.class)
59
+    public static class CommandHelperModule {
60
+
61
+    }
62
+
63
+}

Laden…
Abbrechen
Speichern