提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
获取设置器忽略名称大小写和类型分配
//package com.nowjava; import java.lang.reflect.Method; /** from * nowjava.com - 时代Java **/ 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)); } public static Method getSetterIgnoreNameCaseAndTypeAssigned( final Class<?> clazz, final Class<?> fieldClass) {// ignore case and // type return findMethodIgnoreCaseAndArgsTypesAssigned(clazz.getMethods(), /** 来自 nowjava - 时 代 Java**/ 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; } }