Java Reflection Method Setter Get getSetterOrGetter(Class clazz, String name, boolean isSetter)

Here you can find the source of getSetterOrGetter(Class clazz, String name, boolean isSetter)

Description

get Setter Or Getter

License

Apache License

Declaration

private static Method getSetterOrGetter(Class clazz, String name, boolean isSetter) 

Method Source Code


//package com.java2s;
/*//from   w  w  w  .  ja v a  2  s .  co  m
 *
 *  Copyright 2018 FJN Corp.
 *
 *  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.
 *
 *  Author                        Date                       Issue
 *  fs1194361820@163.com          2015-01-01                 Initial Version
 *
 */

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
    private static Method getSetterOrGetter(Class clazz, String name, boolean isSetter) {
        String exceptMethodName = getSetterOrGetterName(name, isSetter);

        Method[] methods = clazz.getMethods();
        if (methods != null) {
            for (Method method : methods) {
                if (method.getName().equals(exceptMethodName) && !Modifier.isAbstract(method.getModifiers())
                        && Modifier.isPublic(method.getModifiers())) {
                    return method;
                }
            }
        }
        return null;
    }

    private static String getSetterOrGetterName(String name, boolean isSetter) {
        String prefix = isSetter ? "set" : "get";
        if (name == null || name.isEmpty() || name.trim().isEmpty()) {
            return prefix;
        }
        name = name.trim();
        return prefix + ("" + name.charAt(0)).toUpperCase() + (name.length() > 1 ? name.substring(1) : "");
    }
}

Related

  1. getSetterName(final Field field)
  2. getSetterName(Method m)
  3. getSetterName(Method m)
  4. getSetterName(Method method)
  5. getSetterName(String name)
  6. getSetters(Class clazz)
  7. getSetters(Class clazz)
  8. getSetters(Class klass)
  9. getSetters(Class klass)