Created
April 18, 2020 14:15
-
-
Save HeathLoganCampbell/8ea604d5049085d736caeeb55c935849 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Parameter; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
/** | |
* Bootleg IoC DI command system | |
* V2 | |
*/ | |
public class Main | |
{ | |
private static List<RegisteredCommand> registeredCommands = new ArrayList<>(); | |
public static void main(String[] margs) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { | |
Main main = new Main(); | |
for (Method method : Main.class.getDeclaredMethods()) | |
{ | |
if(method.isAnnotationPresent(Command.class)) | |
{ | |
Command commandAnno = method.getDeclaredAnnotation(Command.class); | |
String syntax = commandAnno.syntax(); | |
HashMap<String, Class<?>> clazzTypes = new HashMap<>(); | |
Parameter[] parameters = method.getParameters(); | |
for (Parameter parameter : parameters) | |
{ | |
if(parameter.isAnnotationPresent(Param.class)) | |
{ | |
Param param = parameter.getAnnotation(Param.class); | |
clazzTypes.put(param.name(), parameter.getType()); | |
} | |
} | |
String[] syntaxArgs = Patterns.SPACE.split(syntax); | |
RegisteredCommand registeredCommand = new RegisteredCommand(clazzTypes, syntaxArgs, method, parameters, main); | |
registeredCommands.add(registeredCommand); | |
} | |
} | |
String command = "sprock toot BIG"; | |
execute(command); | |
} | |
public static void execute(String command) throws InvocationTargetException, IllegalAccessException | |
{ | |
String[] args = Patterns.SPACE.split(command); | |
registeredCheck: | |
for (RegisteredCommand registeredCommand : registeredCommands) | |
{ | |
HashMap<Class<?>, Object> passArgs = new HashMap<>(); | |
for (int i = 0; i < registeredCommand.getSyntaxArgs().length; i++) | |
{ | |
String syntaxArg = registeredCommand.getSyntaxArgs()[i]; | |
String arg = args[i]; | |
if(!syntaxArg.equalsIgnoreCase(arg)) | |
{ | |
if(Patterns.ARGUMENT_PLACEHOLDER.matcher(syntaxArg).find()) | |
{ | |
//is place holder | |
String bindName = syntaxArg.substring(1, syntaxArg.length() - 1); | |
Class<?> aClass = registeredCommand.getClazzTypes().get(bindName); | |
Object resolve = resolve(arg, aClass); | |
passArgs.put(aClass, resolve); | |
} | |
else | |
{ | |
continue registeredCheck; | |
} | |
} | |
} | |
Object[] injectedObj = new Object[registeredCommand.getParameters().length]; | |
int i = 0; | |
for (Parameter parameter : registeredCommand.getParameters()) | |
{ | |
Class<?> clazzType = parameter.getType(); | |
Object obj = passArgs.get(clazzType); | |
injectedObj[i] = obj; | |
i++; | |
} | |
registeredCommand.getExecutor().invoke(registeredCommand.getInstance(), injectedObj); | |
} | |
} | |
public static void onHelp() | |
{ | |
System.out.println("Help Meh pls"); | |
} | |
public static Object resolve(String str, Class<?> clazz) | |
{ | |
if(clazz == String.class) | |
{ | |
return str; | |
} | |
if(clazz == User.class) | |
{ | |
return new User(); | |
} | |
if(clazz == Gift.class) | |
{ | |
return Gift.valueOf(str.toUpperCase()); | |
} | |
return null; | |
} | |
@Command(syntax = "gift <user> add <gift>") | |
public void onAdd(@Param(name = "user") User user,@Param(name = "gift") Gift gift) | |
{ | |
System.out.println(user + " I was gifted " + gift); | |
} | |
@Command(syntax = "gift <user> remove <gift>") | |
public void onRemove(@Param(name = "user") User user, @Param(name = "gift") Gift gift) | |
{ | |
System.out.println(user + " I removed gifted " + gift); | |
} | |
@Command(syntax = "<user> toot <gift>") | |
public void onRoot(@Param(name = "user") User user, @Param(name = "gift") Gift gift) | |
{ | |
System.out.println(user + " I toot gifted " + gift); | |
} | |
} | |
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Parameter; | |
import java.util.HashMap; | |
@Getter | |
@AllArgsConstructor | |
public class RegisteredCommand | |
{ | |
private final HashMap<String, Class<?>> clazzTypes; | |
private final String[] syntaxArgs; | |
private final Method executor; | |
private final Parameter[] parameters; | |
private final Object instance; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment