Java tutorial
//package com.java2s; /* * Copyright 2006 Abdulla G. Abdurakhmanov (abdulla.abdurakhmanov@gmail.com). * * Licensed under the LGPL, Version 2 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.gnu.org/copyleft/lgpl.html * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * With any your questions welcome to my e-mail * or blog at http://abdulla-a.blogspot.com. */ import java.lang.reflect.*; public class Main { public static Method findSetterMethodForField(Field field, Class<?> objectClass, Class<?> paramClass) throws NoSuchMethodException { String methodName = "set" + field.getName().toUpperCase().substring(0, 1) + field.getName().substring(1); return findMethodForField(methodName, objectClass, paramClass); } public static Method findMethodForField(String methodName, Class<?> objectClass, Class<?> paramClass) throws NoSuchMethodException { try { return objectClass.getMethod(methodName, new Class[] { paramClass }); } catch (NoSuchMethodException ex) { Method[] methods = objectClass.getMethods(); for (Method method : methods) { if (method.getName().equalsIgnoreCase(methodName)) { return method; } } throw ex; } } }