Created
April 18, 2020 13:48
-
-
Save HeathLoganCampbell/d22c8f8e32c903820beaec03af1f9abe 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.HashMap; | |
/** | |
* Bootleg IoC DI command system | |
* | |
*/ | |
public class Main | |
{ | |
public static void main(String[] margs) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { | |
String command = "f gift sprock add BIG"; | |
boolean commandExecuted = false; | |
methodCheck: | |
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); | |
String[] args = Patterns.SPACE.split(command); | |
HashMap<Class<?>, Object> passArgs = new HashMap<>(); | |
argCheck: | |
for (int i = 0; i < syntaxArgs.length; i++) | |
{ | |
String syntaxArg = syntaxArgs[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 = clazzTypes.get(bindName); | |
Object resolve = resolve(arg, aClass); | |
passArgs.put(aClass, resolve); | |
} | |
else | |
{ | |
continue methodCheck; | |
} | |
} | |
} | |
Object[] injectedObj = new Object[parameters.length]; | |
int i = 0; | |
for (Parameter parameter : parameters) | |
{ | |
Class<?> clazzType = parameter.getType(); | |
Object obj = passArgs.get(clazzType); | |
injectedObj[i] = obj; | |
i++; | |
} | |
method.invoke(null, injectedObj); | |
commandExecuted = true; | |
} | |
} | |
if(!commandExecuted) | |
{ | |
onHelp(); | |
} | |
} | |
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 = "f gift <user> add <gift>") | |
public static void onAdd(@Param(name = "user") User user,@Param(name = "gift") Gift gift) | |
{ | |
System.out.println(user + " I was gifted " + gift); | |
} | |
@Command(syntax = "f gift <user> remove <gift>") | |
public static void onRemove(@Param(name = "user") User user, @Param(name = "gift") Gift gift) | |
{ | |
System.out.println(user + " I removed gifted " + gift); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment