T command = null;
try {
command = commandInstance.get();
}
catch (Exception e) {
throw new InvocationException(e);
}
//
if (owner != null) {
bind(match.owner(), owner.getParameters(), command, Util.EMPTY_ARGS);
}
// Prepare invocation
Method m = getMethod();
Class<?>[] parameterTypes = m.getParameterTypes();
Object[] mArgs = new Object[parameterTypes.length];
// Bind method parameter first
bind(match, getParameters(), command, mArgs);
// Fill missing contextual parameters and make primitive check
for (int i = 0;i < mArgs.length;i++) {
Class<?> parameterType = parameterTypes[i];
if (mArgs[i] == null) {
Object v = commandInstance.resolve(parameterType);
if (v != null) {
mArgs[i] = v;
}
}
if (mArgs[i] == null && parameterType.isPrimitive()) {
throw new SyntaxException("Method argument at position " + i + " of " + m + " is missing");
}
}
// Perform method invocation
try {
Object ret = m.invoke(command, mArgs);
return returnType.cast(ret);
}
catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t instanceof Error) {
throw (Error)t;
} else {
throw new InvocationException(t);
}
}
catch (IllegalAccessException t) {
throw new InvocationException(t);
}
}
};
}