Java Code Examples for org.apache.commons.lang3.ClassUtils#toClass()

The following examples show how to use org.apache.commons.lang3.ClassUtils#toClass() . 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: ReflectionUtil.java    From j360-dubbo-app-all with Apache License 2.0 4 votes vote down vote up
/**
 * 直接调用对象方法, 无视private/protected修饰符.
 * 
 * 根据传入参数的实际类型进行匹配
 */
public static <T> T invokeMethod(Object obj, String methodName, Object... args) {
	Object[] theArgs = ArrayUtils.nullToEmpty(args);
	final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs);
	return (T) invokeMethod(obj, methodName, theArgs, parameterTypes);
}
 
Example 2
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Invokes a static method whose parameter types match exactly the object
 * types.</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
 * @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(Class<?> cls, String methodName,
        Object... args) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeExactStaticMethod(cls, methodName, args, parameterTypes);
}
 
Example 3
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Invokes a method whose parameter types match exactly the object
 * types.</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
 * @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) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeExactMethod(object, methodName, args, parameterTypes);
}
 
Example 4
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 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>
 *
 * <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;
    }
    Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeMethod(object, methodName, args, parameterTypes);
}
 
Example 5
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 6
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 7
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Invokes a static method whose parameter types match exactly the object
 * types.</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
 * @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) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    final Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeExactStaticMethod(cls, methodName, args, parameterTypes);
}
 
Example 8
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 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>
 *
 * <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(final Class<?> cls, final String methodName,
        Object... args) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    final Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeStaticMethod(cls, methodName, args, parameterTypes);
}
 
Example 9
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Invokes a method whose parameter types match exactly the object
 * types.</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
 * @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(final Object object, final String methodName,
        Object... args) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    final Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeExactMethod(object, methodName, args, parameterTypes);
}
 
Example 10
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 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>
 *
 * <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(final Object object, final String methodName,
        Object... args) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    final Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeMethod(object, methodName, args, parameterTypes);
}
 
Example 11
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(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 invokeExactConstructor(cls, args, parameterTypes);
}
 
Example 12
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Invokes a static method whose parameter types match exactly the object
 * types.</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
 * @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(Class<?> cls, String methodName,
        Object... args) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeExactStaticMethod(cls, methodName, args, parameterTypes);
}
 
Example 13
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 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>
 *
 * <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;
    }
    Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeStaticMethod(cls, methodName, args, parameterTypes);
}
 
Example 14
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * <p>Invokes a method whose parameter types match exactly the object
 * types.</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
 * @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) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {
    if (args == null) {
        args = ArrayUtils.EMPTY_OBJECT_ARRAY;
    }
    Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeExactMethod(object, methodName, args, parameterTypes);
}
 
Example 15
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 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>
 *
 * <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;
    }
    Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeMethod(object, methodName, 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 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 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(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 18
Source File: MethodUtils.java    From astor with GNU General Public License v2.0 3 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>
 *
 * <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;
    }
    Class<?>[] parameterTypes = ClassUtils.toClass(args);
    return invokeStaticMethod(cls, methodName, args, parameterTypes);
}
 
Example 19
Source File: ReflectionUtil.java    From vjtools with Apache License 2.0 2 votes vote down vote up
/**
 * 反射调用对象方法, 无视private/protected修饰符.
 * 
 * 根据传入参数的实际类型进行匹配, 支持方法参数定义是接口,父类,原子类型等情况
 * 
 * 性能较差,仅用于单次调用.
 */
public static <T> T invokeMethod(Object obj, String methodName, Object... args) {
	Object[] theArgs = ArrayUtils.nullToEmpty(args);
	final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs);
	return invokeMethod(obj, methodName, theArgs, parameterTypes);
}
 
Example 20
Source File: ReflectionUtil.java    From vjtools with Apache License 2.0 2 votes vote down vote up
/**
 * 反射调用对象方法, 无视private/protected修饰符.
 * 
 * 根据传入参数的实际类型进行匹配, 支持方法参数定义是接口,父类,原子类型等情况
 * 
 * 性能较差,仅用于单次调用.
 */
public static <T> T invokeMethod(Object obj, String methodName, Object... args) {
	Object[] theArgs = ArrayUtils.nullToEmpty(args);
	final Class<?>[] parameterTypes = ClassUtils.toClass(theArgs);
	return invokeMethod(obj, methodName, theArgs, parameterTypes);
}