mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-06-16 22:03:53 +00:00
Updated to use simplified method signature for commands. Also improved SimpleInjector.
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user