Java Code Examples for org.apache.commons.lang3.ArrayUtils#EMPTY_OBJECT_ARRAY

The following examples show how to use org.apache.commons.lang3.ArrayUtils#EMPTY_OBJECT_ARRAY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLHTTPRequest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the state as specified and invokes the state change handler if one has been set.
 * @param state the new state
 * @param context the context within which the state change handler is to be invoked;
 *     if {@code null}, the current thread's context is used
 */
private void setState(final int state, Context context) {
    state_ = state;

    if (stateChangeHandler_ != null && !openedMultipleTimes_) {
        final Scriptable scope = stateChangeHandler_.getParentScope();
        final JavaScriptEngine jsEngine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();

        if (LOG.isDebugEnabled()) {
            LOG.debug("Calling onreadystatechange handler for state " + state);
        }
        final Object[] params = ArrayUtils.EMPTY_OBJECT_ARRAY;

        jsEngine.callFunction(containingPage_, stateChangeHandler_, scope, this, params);
        if (LOG.isDebugEnabled()) {
            if (context == null) {
                context = Context.getCurrentContext();
            }
            LOG.debug("onreadystatechange handler: " + context.decompileFunction(stateChangeHandler_, 4));
            LOG.debug("Calling onreadystatechange handler for state " + state + ". Done.");
        }
    }
}
 
Example 2
Source File: ConstructorUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Returns new instance of <code>klazz</code> created using constructor
 * with signature <code>parameterTypes</code> and actual arguments
 * <code>args</code>.</p>
 *
 * <p>The signatures should match exactly.</p>
 *
 * @param cls the class to be constructed.
 * @param args actual argument array
 * @param parameterTypes parameter types array
 * @return new instance of <code>klazz</code>
 *
 * @throws NoSuchMethodException if matching constructor cannot be found
 * @throws IllegalAccessException thrown on the constructor's invocation
 * @throws InvocationTargetException thrown on the constructor's invocation
 * @throws InstantiationException thrown on the constructor's invocation
 * @see Constructor#newInstance
 */
public static Object invokeExactConstructor(Class<?> cls, Object[] args,
        Class<?>[] parameterTypes) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException,
        InstantiationException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    Constructor<?> ctor = getAccessibleConstructor(cls, parameterTypes);
    if (null == ctor) {
        throw new NoSuchMethodException(
                "No such accessible constructor on object: "
                        + cls.getName());
    }
    return ctor.newInstance(args);
}
 
Example 3
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Invokes a named method whose parameter type matches the object type.</p>
 *
 * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 *
 * <p>This method supports calls to methods taking primitive parameters 
 * via passing in wrapping classes. So, for example, a <code>Boolean</code> object
 * would match a <code>boolean</code> primitive.</p>
 *
 * @param object invoke method on this object
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the method invoked
 * @throws IllegalAccessException if the requested method is not accessible via reflection
 */
public static Object invokeMethod(Object object, String methodName,
        Object[] args, Class<?>[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Method method = getMatchingAccessibleMethod(object.getClass(),
            methodName, parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException("No such accessible method: "
                + methodName + "() on object: "
                + object.getClass().getName());
    }
    return method.invoke(object, args);
}
 
Example 4
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Invokes a method whose parameter types match exactly the parameter
 * types given.</p>
 *
 * <p>This uses reflection to invoke the method obtained from a call to
 * <code>getAccessibleMethod()</code>.</p>
 *
 * @param object invoke method on this object
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the
 *  method invoked
 * @throws IllegalAccessException if the requested method is not accessible
 *  via reflection
 */
public static Object invokeExactMethod(Object object, String methodName,
        Object[] args, Class<?>[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    Method method = getAccessibleMethod(object.getClass(), methodName,
            parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException("No such accessible method: "
                + methodName + "() on object: "
                + object.getClass().getName());
    }
    return method.invoke(object, args);
}
 
Example 5
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Invoke a method whose parameter types match exactly the parameter
 * types given.</p>
 *
 * <p>This uses reflection to invoke the method obtained from a call to
 * <code>getAccessibleMethod()</code>.</p>
 *
 * @param object invoke method on this object
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the
 *  method invoked
 * @throws IllegalAccessException if the requested method is not accessible
 *  via reflection
 */
public static Object invokeExactMethod(Object object, String methodName,
        Object[] args, Class<?>[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    Method method = getAccessibleMethod(object.getClass(), methodName,
            parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException("No such accessible method: "
                + methodName + "() on object: "
                + object.getClass().getName());
    }
    return method.invoke(object, args);
}
 
Example 6
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Invoke a method whose parameter types match exactly the parameter
 * types given.</p>
 *
 * <p>This uses reflection to invoke the method obtained from a call to
 * <code>getAccessibleMethod()</code>.</p>
 *
 * @param object invoke method on this object
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the
 *  method invoked
 * @throws IllegalAccessException if the requested method is not accessible
 *  via reflection
 */
public static Object invokeExactMethod(Object object, String methodName,
        Object[] args, Class<?>[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    Method method = getAccessibleMethod(object.getClass(), methodName,
            parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException("No such accessible method: "
                + methodName + "() on object: "
                + object.getClass().getName());
    }
    return method.invoke(object, args);
}
 
Example 7
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Invokes a method whose parameter types match exactly the parameter
 * types given.</p>
 *
 * <p>This uses reflection to invoke the method obtained from a call to
 * <code>getAccessibleMethod()</code>.</p>
 *
 * @param object invoke method on this object
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the
 *  method invoked
 * @throws IllegalAccessException if the requested method is not accessible
 *  via reflection
 */
public static Object invokeExactMethod(Object object, String methodName,
        Object[] args, Class<?>[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    Method method = getAccessibleMethod(object.getClass(), methodName,
            parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException("No such accessible method: "
                + methodName + "() on object: "
                + object.getClass().getName());
    }
    return method.invoke(object, args);
}
 
Example 8
Source File: QueueStub.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public Object[] toArray() {
	return ArrayUtils.EMPTY_OBJECT_ARRAY;
}
 
Example 9
Source File: BeanUtil.java    From fastquery with Apache License 2.0 4 votes vote down vote up
/**
 * 如果这个bean已经包含主键的值,就以bean的主键值为准 <br>
 * [0]: 更新语句, [1]:参数值集合类型:List&lt;Object&gt; [2](是否存在第3个值取决于toSQL是否设置true): 根据主键查的sql语句
 * 
 * @param bean 待更新的实体
 * @param dbName 数据库名称,可以为null
 * @param toSQL 是否返回根据主键查的sql语句
 * @return 更新语句信息
 */
public static Object[] toUpdateSQL(Object bean, String dbName, boolean toSQL) {
	List<Object> args = new ArrayList<>();
	Class<?> cls = bean.getClass();
	// 表名称
	String tableName = getTableName(dbName, cls);

	String keyFeild;
	Object key;
	Object[] objs = getKeyAndVal(bean, getFields(cls), null);
	keyFeild = (String) objs[0];
	key = objs[1];

	if (keyFeild == null || key == null) {
		throw new RepositoryException(cls + NOT_ID);
	}

	// update UserInfo set name=?,age=? where id=?4
	StringBuilder sb = new StringBuilder(UPDATE);
	sb.append(tableName);
	sb.append(" set");
	int len = sb.length();
	try {
		Field[] fields = getFields(cls);
		for (Field field : fields) {
			if (allowField(field)) {
				field.setAccessible(true);
				Object val = field.get(bean);
				Id id = field.getAnnotation(Id.class);
				if (val != null && id == null) {
					args.add(val);
					sb.append(' ');
					sb.append(field.getName());
					sb.append("=?,");

				}
			}
		}

		if (sb.length() == len) {
			LOG.warn("传递的实体,没有什么可以修改,{}", bean);
			return ArrayUtils.EMPTY_OBJECT_ARRAY;
		} else {
			// 去掉sb最后的一个字符
			sb.deleteCharAt(sb.length() - 1);
			sb.append(WHERE);
			sb.append(keyFeild);
			sb.append("=?");
			args.add(key);
			Object[] updateinfo = new Object[3];
			updateinfo[0] = sb.toString();
			updateinfo[1] = args;
			if (toSQL) {
				updateinfo[2] = String.format("select * from %s where %s = %s", tableName, keyFeild, key.toString());
			}
			return updateinfo;
		}
	} catch (Exception e) {
		throw new RepositoryException(e);
	}
}
 
Example 10
Source File: ConstructorUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Returns new instance of <code>klazz</code> created using constructor
 * with signature <code>parameterTypes</code> and actual arguments
 * <code>args</code>.</p>
 * 
 * <p>The signatures should be assignment compatible.</p>
 * 
 * @param cls the class to be constructed.
 * @param args actual argument array
 * @param parameterTypes parameter types array
 * @return new instance of <code>klazz</code>
 * 
 * @throws NoSuchMethodException if matching constructor cannot be found
 * @throws IllegalAccessException thrown on the constructor's invocation
 * @throws InvocationTargetException thrown on the constructor's invocation
 * @throws InstantiationException thrown on the constructor's invocation
 * @see Constructor#newInstance
 */
public static <T> T invokeConstructor(Class<T> cls, Object[] args, Class<?>[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
        InstantiationException {
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Constructor<T> ctor = getMatchingAccessibleConstructor(cls, parameterTypes);
    if (null == ctor) {
        throw new NoSuchMethodException("No such accessible constructor on object: "
                + cls.getName());
    }
    return ctor.newInstance(args);
}
 
Example 11
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Invoke a named static method whose parameter type matches the object type.</p>
 *
 * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 *
 * <p>This method supports calls to methods taking primitive parameters 
 * via passing in wrapping classes. So, for example, a <code>Boolean</code> class
 * would match a <code>boolean</code> primitive.</p>
 *
 *
 * @param cls invoke static method on this class
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the
 *  method invoked
 * @throws IllegalAccessException if the requested method is not accessible
 *  via reflection
 */
public static Object invokeStaticMethod(Class<?> cls, String methodName,
        Object[] args, Class<?>[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Method method = getMatchingAccessibleMethod(cls, methodName,
            parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException("No such accessible method: "
                + methodName + "() on class: " + cls.getName());
    }
    return method.invoke(null, args);
}
 
Example 12
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Invokes a static method whose parameter types match exactly the parameter
 * types given.</p>
 *
 * <p>This uses reflection to invoke the method obtained from a call to
 * {@link #getAccessibleMethod(Class, String, Class[])}.</p>
 *
 * @param cls invoke static method on this class
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the
 *  method invoked
 * @throws IllegalAccessException if the requested method is not accessible
 *  via reflection
 */
public static Object invokeExactStaticMethod(final Class<?> cls, final String methodName,
        Object[] args, Class<?>[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    final Method method = getAccessibleMethod(cls, methodName, parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException("No such accessible method: "
                + methodName + "() on class: " + cls.getName());
    }
    return method.invoke(null, args);
}
 
Example 13
Source File: ConstructorUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Returns a new instance of the specified class choosing the right constructor
 * from the list of parameter types.</p>
 *
 * <p>This locates and calls a constructor.
 * The constructor signature must match the parameter types exactly.</p>
 *
 * @param <T> the type to be constructed
 * @param cls  the class to be constructed, not null
 * @param args  the array of arguments, null treated as empty
 * @param parameterTypes  the array of parameter types, null treated as empty
 * @return new instance of <code>cls</code>, not null
 *
 * @throws NoSuchMethodException if a matching constructor cannot be found
 * @throws IllegalAccessException if invocation is not permitted by security
 * @throws InvocationTargetException if an error occurs on invocation
 * @throws InstantiationException if an error occurs on instantiation
 * @see Constructor#newInstance
 */
public static <T> T invokeExactConstructor(Class<T> cls, Object[] args,
        Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException, InstantiationException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    Constructor<T> ctor = getAccessibleConstructor(cls, parameterTypes);
    if (ctor == null) {
        throw new NoSuchMethodException(
            "No such accessible constructor on object: "+ cls.getName());
    }
    return ctor.newInstance(args);
}
 
Example 14
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * <p>Invokes a named static method whose parameter type matches the object type.</p>
 *
 * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 *
 * <p>This method supports calls to methods taking primitive parameters 
 * via passing in wrapping classes. So, for example, a <code>Boolean</code> class
 * would match a <code>boolean</code> primitive.</p>
 *
 *
 * @param cls invoke static method on this class
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @param parameterTypes match these parameters - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the
 *  method invoked
 * @throws IllegalAccessException if the requested method is not accessible
 *  via reflection
 */
public static Object invokeStaticMethod(Class<?> cls, String methodName,
        Object[] args, Class<?>[] parameterTypes)
        throws NoSuchMethodException, IllegalAccessException,
        InvocationTargetException {
    if (parameterTypes == null) {
        parameterTypes = ArrayUtils.EMPTY_CLASS_ARRAY;
    }
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Method method = getMatchingAccessibleMethod(cls, methodName,
            parameterTypes);
    if (method == null) {
        throw new NoSuchMethodException("No such accessible method: "
                + methodName + "() on class: " + cls.getName());
    }
    return method.invoke(null, args);
}
 
Example 15
Source File: ConstructorUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Returns a new instance of the specified class inferring the right constructor
 * from the types of the arguments.</p>
 *
 * <p>This locates and calls a constructor.
 * The constructor signature must match the argument types exactly.</p>
 *
 * @param <T> the type to be constructed
 * @param cls  the class to be constructed, not null
 * @param args  the array of arguments, null treated as empty
 * @return new instance of <code>cls</code>, not null
 *
 * @throws NoSuchMethodException if a matching constructor cannot be found
 * @throws IllegalAccessException if invocation is not permitted by security
 * @throws InvocationTargetException if an error occurs on invocation
 * @throws InstantiationException if an error occurs on instantiation
 * @see #invokeExactConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
 */
public static <T> T invokeExactConstructor(Class<T> cls, Object... args)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
        InstantiationException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Class<?> parameterTypes[] = ClassUtils.toClass(args);
    return invokeExactConstructor(cls, args, parameterTypes);
}
 
Example 16
Source File: ConstructorUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Returns a new instance of the specified class inferring the right constructor
 * from the types of the arguments.</p>
 * 
 * <p>This locates and calls a constructor.
 * The constructor signature must match the argument types by assignment compatibility.</p>
 *
 * @param <T> the type to be constructed
 * @param cls  the class to be constructed, not null
 * @param args  the array of arguments, null treated as empty
 * @return new instance of <code>cls</code>, not null
 *
 * @throws NoSuchMethodException if a matching constructor cannot be found
 * @throws IllegalAccessException if invocation is not permitted by security
 * @throws InvocationTargetException if an error occurs on invocation
 * @throws InstantiationException if an error occurs on instantiation
 * @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
 */
public static <T> T invokeConstructor(Class<T> cls, Object... args)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
        InstantiationException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Class<?> parameterTypes[] = ClassUtils.toClass(args);
    return invokeConstructor(cls, args, parameterTypes);
}
 
Example 17
Source File: ConstructorUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Returns a new instance of the specified class inferring the right constructor
 * from the types of the arguments.</p>
 * 
 * <p>This locates and calls a constructor.
 * The constructor signature must match the argument types by assignment compatibility.</p>
 *
 * @param <T> the type to be constructed
 * @param cls  the class to be constructed, not null
 * @param args  the array of arguments, null treated as empty
 * @return new instance of <code>cls</code>, not null
 *
 * @throws NoSuchMethodException if a matching constructor cannot be found
 * @throws IllegalAccessException if invocation is not permitted by security
 * @throws InvocationTargetException if an error occurs on invocation
 * @throws InstantiationException if an error occurs on instantiation
 * @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
 */
public static <T> T invokeConstructor(final Class<T> cls, Object... args)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
        InstantiationException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    final Class<?> parameterTypes[] = ClassUtils.toClass(args);
    return invokeConstructor(cls, args, parameterTypes);
}
 
Example 18
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Invoke a named static method whose parameter type matches the object type.</p>
 *
 * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 *
 * <p>This method supports calls to methods taking primitive parameters 
 * via passing in wrapping classes. So, for example, a <code>Boolean</code> class
 * would match a <code>boolean</code> primitive.</p>
 *
 * <p> This is a convenient wrapper for
 * {@link #invokeStaticMethod(Class objectClass,String methodName,Object [] args,Class[] parameterTypes)}.
 * </p>
 *
 * @param cls invoke static method on this class
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the
 *  method invoked
 * @throws IllegalAccessException if the requested method is not accessible
 *  via reflection
 */
public static Object invokeStaticMethod(Class<?> cls, String methodName,
        Object... args) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    int arguments = args.length;
    Class<?>[] parameterTypes = new Class[arguments];
    for (int i = 0; i < arguments; i++) {
        parameterTypes[i] = args[i].getClass();
    }
    return invokeStaticMethod(cls, methodName, args, parameterTypes);
}
 
Example 19
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Invoke a named method whose parameter type matches the object type.</p>
 *
 * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p>
 *
 * <p>This method supports calls to methods taking primitive parameters 
 * via passing in wrapping classes. So, for example, a <code>Boolean</code> object
 * would match a <code>boolean</code> primitive.</p>
 *
 * <p> This is a convenient wrapper for
 * {@link #invokeMethod(Object object,String methodName, Object[] args, Class[] parameterTypes)}.
 * </p>
 *
 * @param object invoke method on this object
 * @param methodName get method with this name
 * @param args use these arguments - treat null as empty array
 * @return The value returned by the invoked method
 *
 * @throws NoSuchMethodException if there is no such accessible method
 * @throws InvocationTargetException wraps an exception thrown by the method invoked
 * @throws IllegalAccessException if the requested method is not accessible via reflection
 */
public static Object invokeMethod(Object object, String methodName,
        Object[] args) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    int arguments = args.length;
    Class<?>[] parameterTypes = new Class[arguments];
    for (int i = 0; i < arguments; i++) {
        parameterTypes[i] = args[i].getClass();
    }
    return invokeMethod(object, methodName, args, parameterTypes);
}
 
Example 20
Source File: ConstructorUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Returns a new instance of the specified class inferring the right constructor
 * from the types of the arguments.</p>
 * 
 * <p>This locates and calls a constructor.
 * The constructor signature must match the argument types by assignment compatibility.</p>
 *
 * @param <T> the type to be constructed
 * @param cls  the class to be constructed, not null
 * @param args  the array of arguments, null treated as empty
 * @return new instance of <code>cls</code>, not null
 *
 * @throws NoSuchMethodException if a matching constructor cannot be found
 * @throws IllegalAccessException if invocation is not permitted by security
 * @throws InvocationTargetException if an error occurs on invocation
 * @throws InstantiationException if an error occurs on instantiation
 * @see #invokeConstructor(java.lang.Class, java.lang.Object[], java.lang.Class[])
 */
public static <T> T invokeConstructor(Class<T> cls, Object... args)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
        InstantiationException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Class<?> parameterTypes[] = ClassUtils.toClass(args);
    return invokeConstructor(cls, args, parameterTypes);
}