Java Reflection Method Setter Get getSetterMethod(String getterName, Class returnType, Class containingClass)

Here you can find the source of getSetterMethod(String getterName, Class returnType, Class containingClass)

Description

Gets the setter name of a property given the name of the getter

License

Apache License

Declaration

public static Method getSetterMethod(String getterName, Class<?> returnType, Class<?> containingClass) 

Method Source Code

//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;*/
    }
}

Related

  1. getSetterMethod(Class cls, String property, Class valueCls)
  2. getSetterMethod(final Class clazz, final Class annotation, final String name, final String value)
  3. getSetterMethod(final Class clazz, final String propertyName, final Class type)
  4. getSetterMethod(final String methodName, final Class pojoClass, final Class attributeClass)
  5. getSetterMethod(Object obj, String name)
  6. getSetterMethod(String setterName, Object bean, Class setterParamType)
  7. getSetterMethodByFieldName(String fieldName, Field field)
  8. getSetterMethodByProperty(String propertyName, Class beanClass, Class setterParamType)
  9. getSetterMethodForClass(Class cls, String beanName, Class type)