Updated to use simplified method signature for commands. Also improved SimpleInjector.

This commit is contained in:
zml2008
2011-12-17 23:45:12 -08:00
parent 6fb8085692
commit 4dc3c035c1
19 changed files with 366 additions and 371 deletions

View File

@ -3,22 +3,30 @@ package com.sk89q.minecraft.util.commands;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class SimpleInjector<T> implements Injector {
private final T injectionObject;
public class SimpleInjector implements Injector {
private Object[] args;
private Class<?>[] argClasses;
public SimpleInjector(T injectionObject) {
this.injectionObject = injectionObject;
}
public Object getInstance(Class<?> cls) throws InvocationTargetException,
IllegalAccessException, InstantiationException {
try {
Constructor<?> construct = cls.getConstructor(injectionObject.getClass());
return construct.newInstance(injectionObject);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
public SimpleInjector(Object... args) {
this.args = args;
argClasses = new Class[args.length];
for (int i = 0; i < args.length; ++i) {
argClasses[i] = args[i].getClass();
}
}
public Object getInstance(Class<?> clazz) {
try {
Constructor<?> ctr = clazz.getConstructor(argClasses);
return ctr.newInstance(args);
} catch (NoSuchMethodException e) {
return null;
} catch (InvocationTargetException e) {
return null;
} catch (InstantiationException e) {
return null;
} catch (IllegalAccessException e) {
return null;
}
}
}