Java Reflection Method Getter Get getGettersAndSetters(Object obj)

Here you can find the source of getGettersAndSetters(Object obj)

Description

get Getters And Setters

License

Open Source License

Declaration

public static Method[] getGettersAndSetters(Object obj) 

Method Source Code

//package com.java2s;
/*//from  w w  w . j  av a 2 s .c o m
 *   $Id$
 *
 *   Copyright 2006 University of Dundee. All rights reserved.
 *   Use is subject to license terms supplied in LICENSE.txt
 */

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

import java.util.List;

public class Main {
    public static Method[] getGettersAndSetters(Object obj) {
        Method[] methods, superMethods = null;
        methods = obj.getClass().getDeclaredMethods();
        Package pkg = obj.getClass().getPackage();
        if (null != pkg && pkg.toString().indexOf("ome.model2") > -1) {// FIXME
                                                                       // not
                                                                       // valid
            superMethods = obj.getClass().getSuperclass().getDeclaredMethods();
        }
        List goodMethods = checkGettersAndSetters(methods);
        goodMethods.addAll(checkGettersAndSetters(superMethods));
        return (Method[]) goodMethods.toArray(new Method[goodMethods.size()]);
    }

    static List checkGettersAndSetters(Method[] methods) {
        List goodMethods = new ArrayList();

        if (null == methods) {
            return goodMethods;
        }

        for (int i = 0; i < methods.length; i++) {
            boolean ok = true;
            Method method = methods[i];
            int mod = method.getModifiers();
            if (!Modifier.isPublic(mod) || Modifier.isStatic(mod)) {
                ok = false;
            }

            if (method.getName().startsWith("get")) {
                if (0 != method.getParameterTypes().length) {
                    ok = false;
                }
            } else if (method.getName().startsWith("set")) {
                // No constaints yet on setters.
            } else {
                ok = false;
            }

            if (ok) {
                goodMethods.add(method);
            }
        }
        return goodMethods;
    }
}

Related

  1. getGetters(Class clazz)
  2. getGetters(Class clazz)
  3. getGetters(final Class clazz)
  4. getGetters(Object bean)
  5. getGetters(Object obj)
  6. getGetterSetterMethodsParameterType(Field f)
  7. getGetterShorthandName(Method method)
  8. getGettersMethods(Object object)
  9. getGetterValue(Method method, Object o)