/** * Find the best method on the target class that matches the signature specified with the specified name and the list of * arguments. This method first attempts to find the method with the specified arguments; if no such method is found, a * NoSuchMethodException is thrown. * <P> * This method is unable to find methods with signatures that include both primitive arguments <i>and</i> arguments that are * instances of <code>Number</code> or its subclasses. * * @param methodName the name of the method that is to be invoked. * @param arguments the array of Object instances that correspond to the arguments passed to the method. * @return the Method object that references the method that satisfies the requirements, or null if no satisfactory method * could be found. * @throws NoSuchMethodException if a matching method is not found. * @throws SecurityException if access to the information is denied. */ public Method findBestMethodOnTarget( String methodName, Object... arguments ) throws NoSuchMethodException, SecurityException { Class<?>[] argumentClasses = buildArgumentClasses(arguments); return findBestMethodWithSignature(methodName, argumentClasses); }
/** * Find the method on the target class that matches the supplied method name. * * @param methodName the name of the method that is to be found. * @param caseSensitive true if the method name supplied should match case-sensitively, or false if case does not matter * @return the first Method object found that has a matching name, or null if there are no methods that have a matching name. */ public Method findFirstMethod( String methodName, boolean caseSensitive ) { Pattern pattern = caseSensitive ? Pattern.compile(methodName) : Pattern.compile(methodName, Pattern.CASE_INSENSITIVE); return findFirstMethod(pattern); }
/** * Find and execute the getter method on the target class for the supplied property name. If no such method is found, a * NoSuchMethodException is thrown. * * @param javaPropertyName the name of the property whose getter is to be invoked, in the order they are to be tried * @param target the object on which the method is to be invoked * @return the property value (the result of the getter method call) * @throws NoSuchMethodException if a matching method is not found. * @throws SecurityException if access to the information is denied. * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public Object invokeGetterMethodOnTarget( String javaPropertyName, Object target ) throws NoSuchMethodException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { String[] methodNamesArray = findMethodNames("get" + javaPropertyName); return invokeBestMethodOnTarget(methodNamesArray, target); }
for (Map.Entry<String, Object> entry : config.getProperties().entrySet()) { Reflection reflection = new Reflection(newInstance.getClass()); reflection.invokeSetterMethodOnTarget(entry.getKey(), newInstance, entry.getValue());
/** * Find the method on the target class that matches the supplied method name. * * @param methodName the name of the method that is to be found. * @param caseSensitive true if the method name supplied should match case-sensitively, or false if case does not matter * @return the Method objects that have a matching name, or an empty array if there are no methods that have a matching name. */ public Method[] findMethods( String methodName, boolean caseSensitive ) { Pattern pattern = caseSensitive ? Pattern.compile(methodName) : Pattern.compile(methodName, Pattern.CASE_INSENSITIVE); return findMethods(pattern); }
/** * Find the property names with getter methods on the target class. This method returns the property names for the methods * returned by {@link #findGetterMethods()}. * * @return the Java Bean property names for the getters; never null but possibly empty */ public String[] findGetterPropertyNames() { final Method[] getters = findGetterMethods(); final List<String> result = new ArrayList<String>(); for (int i = 0; i < getters.length; i++) { final Method m = getters[i]; String name = m.getName(); if (name.startsWith("get") && name.length() > 3) { result.add(name.substring(3)); } else if (name.startsWith("is") && name.length() > 2) { result.add(name.substring(2)); } } return result.toArray(new String[result.size()]); }
List<Class<?>> argumentsClassList = convertArgumentClassesToPrimitives(argumentsClasses); try { classArgs = argumentsClassList.toArray(new Class[argumentsClassList.size()]);
protected String[] findMethodNames( String methodName ) { Method[] methods = findMethods(methodName, false); Set<String> methodNames = new HashSet<String>(); for (Method method : methods) { String actualMethodName = method.getName(); methodNames.add(actualMethodName); } return methodNames.toArray(new String[methodNames.size()]); }
throws NoSuchMethodException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class<?>[] argumentClasses = buildArgumentClasses(arguments); int remaining = methodNames.length; Object result = null; --remaining; try { Method method = findBestMethodWithSignature(methodName, argumentClasses); result = method.invoke(target, arguments); break;
/** * Find and execute the best setter method on the target class for the supplied property name and the supplied list of * arguments. If no such method is found, a NoSuchMethodException is thrown. * <P> * This method is unable to find methods with signatures that include both primitive arguments <i>and</i> arguments that are * instances of <code>Number</code> or its subclasses. * </p> * * @param javaPropertyName the name of the property whose setter is to be invoked, in the order they are to be tried * @param target the object on which the method is to be invoked * @param argument the new value for the property * @return the result of the setter method, which is typically null (void) * @throws NoSuchMethodException if a matching method is not found. * @throws SecurityException if access to the information is denied. * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public Object invokeSetterMethodOnTarget( String javaPropertyName, Object target, Object argument ) throws NoSuchMethodException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { String[] methodNamesArray = findMethodNames("set" + javaPropertyName); return invokeBestMethodOnTarget(methodNamesArray, target, argument); }