Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial AnnotatedCommand API

  • Loading branch information...
commit 4079990fb0d91458c65404ca81e88fcb4bc8b4fd 1 parent f08da1a
@alta189 authored
View
13 src/main/java/com/alta189/tcommand/CommonCommandManager.java
@@ -25,6 +25,8 @@
import com.alta189.tcommand.cmd.CommandExecutor;
import com.alta189.tcommand.cmd.CommandManager;
import com.alta189.tcommand.cmd.CommandSource;
+import com.alta189.tcommand.cmd.annotation.AnnotatedCommandFactory;
+import com.alta189.tcommand.cmd.annotation.Injector;
import java.util.Arrays;
import java.util.List;
@@ -65,6 +67,17 @@ public void registerCommand(Command command) {
getCommandMap().addCommand(command);
}
+ @Override
+ public void registerCommands(Named owner, Class<?> clazz, Injector injector) {
+ AnnotatedCommandFactory factory = new AnnotatedCommandFactory(injector);
+ List<Command> commands = factory.createCommands(owner, clazz);
+ if (commands != null && commands.size() > 0) {
+ for (Command command : commands) {
+ registerCommand(command);
+ }
+ }
+ }
+
private static String[] getArgs(String raw) {
if (!raw.contains(" "))
return null;
View
4 src/main/java/com/alta189/tcommand/cmd/CommandManager.java
@@ -19,6 +19,8 @@
package com.alta189.tcommand.cmd;
+import com.alta189.tcommand.Named;
+import com.alta189.tcommand.cmd.annotation.Injector;
import lombok.Getter;
public abstract class CommandManager {
@@ -32,4 +34,6 @@
public abstract void registerCommand(Command command);
+ public abstract void registerCommands(Named owner, Class<?> clazz, Injector injector);
+
}
View
62 src/main/java/com/alta189/tcommand/cmd/annotation/AnnotatedCommandExecutor.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2012 CyborgDev <cyborg@alta189.com>
+ *
+ * This file is part of TerminalCommand
+ *
+ * TerminalCommand is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * TerminalCommand is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.alta189.tcommand.cmd.annotation;
+
+import com.alta189.tcommand.cmd.CommandContext;
+import com.alta189.tcommand.cmd.CommandException;
+import com.alta189.tcommand.cmd.CommandExecutor;
+import com.alta189.tcommand.cmd.CommandSource;
+import com.alta189.tcommand.cmd.WrappedCommandException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+
+@RequiredArgsConstructor
+public class AnnotatedCommandExecutor implements CommandExecutor {
+
+ private final Object instance;
+ private final Method method;
+
+ public boolean processCommand(CommandSource source, com.alta189.tcommand.cmd.Command command, CommandContext args) throws CommandException {
+ try {
+ List<Object> commandArgs = new ArrayList<Object>(4);
+ commandArgs.add(args);
+ commandArgs.add(source);
+ method.invoke(instance, commandArgs.toArray());
+ } catch (IllegalAccessException e) {
+ throw new WrappedCommandException(e);
+ } catch (InvocationTargetException e) {
+ if (e.getCause() == null) {
+ throw new WrappedCommandException(e);
+ } else {
+ Throwable cause = e.getCause();
+ if (cause instanceof CommandException) {
+ throw (CommandException) cause;
+ } else {
+ throw new WrappedCommandException(cause);
+ }
+ }
+ }
+ return true;
+ }
+
+}
View
63 src/main/java/com/alta189/tcommand/cmd/annotation/AnnotatedCommandFactory.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2012 CyborgDev <cyborg@alta189.com>
+ *
+ * This file is part of TerminalCommand
+ *
+ * TerminalCommand is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * TerminalCommand is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.alta189.tcommand.cmd.annotation;
+
+import com.alta189.tcommand.Named;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import lombok.RequiredArgsConstructor;
+
+@RequiredArgsConstructor
+public class AnnotatedCommandFactory {
+
+ private final Injector injector;
+
+ public List<com.alta189.tcommand.cmd.Command> createCommands(Named owner, Class<?> clazz) {
+ List<com.alta189.tcommand.cmd.Command> result = new ArrayList<com.alta189.tcommand.cmd.Command>();
+ Object instance = null;
+ if (injector != null) {
+ instance = injector.newInstance(clazz);
+ }
+
+ for (Method method : clazz.getMethods()) {
+ if (!Modifier.isStatic(method.getModifiers()) && instance == null) {
+ continue;
+ }
+ if (!method.isAnnotationPresent(Command.class)) {
+ continue;
+ }
+
+ Command command = method.getAnnotation(Command.class);
+ if (command.name() == null) {
+ continue;
+ }
+ com.alta189.tcommand.cmd.Command cmd = new com.alta189.tcommand.cmd.Command(owner, command.name());
+ cmd.getAliases().addAll(Arrays.asList(command.aliases()));
+ cmd.setDesc(command.desc());
+ cmd.setExecutor(new AnnotatedCommandExecutor(instance, method));
+ result.add(cmd);
+ }
+ return result;
+ }
+
+}
View
33 src/main/java/com/alta189/tcommand/cmd/annotation/Command.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2012 CyborgDev <cyborg@alta189.com>
+ *
+ * This file is part of TerminalCommand
+ *
+ * TerminalCommand is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * TerminalCommand is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.alta189.tcommand.cmd.annotation;
+
+import com.alta189.tcommand.Named;
+import java.util.Arrays;
+
+public @interface Command {
+
+ String name();
+
+ String desc();
+
+ String[] aliases() default {};
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.