Java examples for Reflection:Method Name
get Setter Ignore Name Case And Type Assigned
//package com.java2s; import java.lang.reflect.Method; import java.util.Arrays; public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; Class fieldClass = String.class; System.out.println(getSetterIgnoreNameCaseAndTypeAssigned(clazz, fieldClass));//from www . j av a2 s . com } public static Method getSetterIgnoreNameCaseAndTypeAssigned( final Class<?> clazz, final Class<?> fieldClass) {// ignore case and // type return findMethodIgnoreCaseAndArgsTypesAssigned(clazz.getMethods(), toSetterName(fieldClass.getSimpleName()), fieldClass); } public static Method findMethodIgnoreCaseAndArgsTypesAssigned( final Method[] methods, final String methodName, final Class<?>... argsClasses) { Method method = null; for (final Method m : methods) { final String name = m.getName(); final Class<?>[] argTypes = m.getParameterTypes(); if (argsClasses.length != argTypes.length) { continue; } if (name.equalsIgnoreCase(methodName)) { if (Arrays.equals(argTypes, argsClasses)) { return m; } int i = 0; for (final Class<?> argType : argTypes) { if (argType.isAssignableFrom(argsClasses[i])) { ++i; } else { break; } } if (i == argsClasses.length) { method = m; } } } return method; } public static String toSetterName(final String fieldName) { return "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); } }