/** * Utility method for safety getting an object present at a certain * position in the list of arguments composed by arrays. * * Useful for Deferred chains where result of each resolved * promise is set as an array in the arguments list. * * When the object found in the array doesn't match the type required it returns a null. * * Note: If type is null, we don't check the class of the object found andd you could * eventually get a casting exception. * */ @SuppressWarnings("unchecked") public <T> T getArgument(int argIdx, int pos, Class<? extends T> type) { Object[] objs = getArgumentArray(argIdx); Object o = objs.length > pos ? objs[pos] : null; if (o != null && ( // When type is null we don't safety check type == null || // The object is an instance of the type requested o.getClass() == type || // Overlay types type == JavaScriptObject.class && o instanceof JavaScriptObject )) { return (T) o; } return null; }