/** * Returns the constructor of a given class with the given parameter types * * @param clazz Target class * @param parameterTypes Parameter types of the desired constructor * @return The constructor of the target class with the specified parameter types * @throws NoSuchMethodException If the desired constructor with the specified parameter types * cannot be found * @see DataType * @see DataType#getPrimitive(Class[]) * @see DataType#compare(Class[], Class[]) */ public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... parameterTypes) throws NoSuchMethodException { Class<?>[] primitiveTypes = DataType.getPrimitive(parameterTypes); for (Constructor<?> constructor : clazz.getConstructors()) { if (!DataType.compare( DataType.getPrimitive(constructor.getParameterTypes()), primitiveTypes)) { continue; } return constructor; } throw new NoSuchMethodException( "There is no such constructor in this class with the specified parameter types"); }
/** * Returns a method of a class with the given parameter types * * @param clazz Target class * @param methodName Name of the desired method * @param parameterTypes Parameter types of the desired method * @return The method of the target class with the specified name and parameter types * @throws NoSuchMethodException If the desired method of the target class with the specified name * and parameter types cannot be found * @see DataType#getPrimitive(Class[]) * @see DataType#compare(Class[], Class[]) */ public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException { Class<?>[] primitiveTypes = DataType.getPrimitive(parameterTypes); for (Method method : clazz.getMethods()) { if (!method.getName().equals(methodName) || !DataType.compare(DataType.getPrimitive(method.getParameterTypes()), primitiveTypes)) { continue; } return method; } throw new NoSuchMethodException( "There is no such method in this class with the specified name and parameter types"); }
/** * Invokes a method of the target class on an object with the given arguments * * @param instance Target object * @param clazz Target class * @param methodName Name of the desired method * @param arguments Arguments which are used to invoke the desired method * @return The result of invoking the desired method on the target object * @throws IllegalAccessException If the desired method cannot be accessed due to certain * circumstances * @throws IllegalArgumentException If the types of the arguments do not match the parameter types * of the method (this should not occur since it searches for a method with the types of the * arguments) * @throws InvocationTargetException If the desired method cannot be invoked on the target object * @throws NoSuchMethodException If the desired method of the target class with the specified name * and arguments cannot be found * @see #getMethod(Class, String, Class...) * @see DataType#getPrimitive(Object[]) */ public static Object invokeMethod( Object instance, Class<?> clazz, String methodName, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException { return getMethod(clazz, methodName, DataType.getPrimitive(arguments)) .invoke(instance, arguments); }
/** * Returns an instance of a class with the given arguments * * @param clazz Target class * @param arguments Arguments which are used to construct an object of the target class * @return The instance of the target class with the specified arguments * @throws InstantiationException If you cannot create an instance of the target class due to * certain circumstances * @throws IllegalAccessException If the desired constructor cannot be accessed due to certain * circumstances * @throws IllegalArgumentException If the types of the arguments do not match the parameter types * of the constructor (this should not occur since it searches for a constructor with the * types of the arguments) * @throws InvocationTargetException If the desired constructor cannot be invoked * @throws NoSuchMethodException If the desired constructor with the specified arguments cannot be * found */ public static Object instantiateObject(Class<?> clazz, Object... arguments) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException { return getConstructor(clazz, DataType.getPrimitive(arguments)).newInstance(arguments); }
/** * Returns the primitive class of the data type with the given reference class * * @param clazz Reference class of the data type * @return The primitive class */ public static Class<?> getPrimitive(Class<?> clazz) { DataType type = fromClass(clazz); return type == null ? clazz : type.getPrimitive(); }