Here you can find the source of getSetterMethod(String getterName, Class> returnType, Class> containingClass)
public static Method getSetterMethod(String getterName, Class<?> returnType, Class<?> containingClass)
//package com.java2s; /* /*from w w w.ja v a 2 s . c o m*/ * Copyright 2008-2013 Mohawk College of Applied Arts and Technology * * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 * * 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. * * User: Justin Fyfe * Date: 09-25-2012 */ import java.lang.reflect.Method; import java.util.HashMap; public class Main { private static HashMap<Class<?>, HashMap<String, Method>> s_setMethods = new HashMap<Class<?>, HashMap<String, Method>>(); /** * Gets the setter name of a property given the name of the getter */ public static Method getSetterMethod(String getterName, Class<?> returnType, Class<?> containingClass) { // Has this class been scanned before? HashMap<String, Method> cachedSetters = s_setMethods.get(containingClass); // yes? if (cachedSetters != null && cachedSetters.containsKey(getterName)) return cachedSetters.get(getterName); else synchronized (s_setMethods) { cachedSetters = new HashMap<String, Method>(); if (!s_setMethods.containsKey(containingClass)) s_setMethods.put(containingClass, cachedSetters); } // The more expensive operations if (!getterName.startsWith("get")) return null; // cannot get for a non getter String propertyName = getterName.substring(3); Method rv = null; try { rv = containingClass.getMethod(String.format("set%s", propertyName), returnType); } catch (NoSuchMethodException e) { try { rv = containingClass.getMethod(String.format("override%s", propertyName), returnType); } catch (NoSuchMethodException e1) { rv = null; } } // register synchronized (cachedSetters) { cachedSetters.put(getterName, rv); } return rv; /* String setMethod = getterName.replaceAll("get(.*)", "set$1"), overrideMethod = getterName.replaceAll("get(.*)", "override$1"); Method retVal; try { retVal = containingClass.getMethod(setMethod, returnType); return retVal; } catch (NoSuchMethodException e) { } catch (SecurityException e) { } try { retVal = containingClass.getMethod(overrideMethod, returnType); return retVal; } catch (NoSuchMethodException e) { } catch (SecurityException e) { } return null;*/ } }