Java Reflection Method Getter Get getGetterSetterMethodsParameterType(Field f)

Here you can find the source of getGetterSetterMethodsParameterType(Field f)

Description

get Getter Setter Methods Parameter Type

License

Open Source License

Declaration

public static Class getGetterSetterMethodsParameterType(Field f) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Field;

import java.lang.reflect.Method;

public class Main {
    public static Class getGetterSetterMethodsParameterType(Field f) {
        Method getter = prepareGetterMethod(f);
        if (getter == null) {
            return null;
        }/*  w w  w .j ava  2 s .c  o  m*/
        Class rt = getter.getReturnType();
        Method setter = prepareSetterMethod(f, rt);

        return (setter == null) ? null : rt;
    }

    public static Method prepareGetterMethod(Field f) {
        String t = prepareAccessorMainPartName(f.getName());
        @SuppressWarnings("unused")
        String sm;
        String gm;
        if (f.getType().isPrimitive() && f.getType().equals(boolean.class)) {
            sm = "set" + t;
            gm = "is" + t;
        } else {
            sm = "set" + t;
            gm = "get" + t;
        }

        try {
            Method m = f.getDeclaringClass().getMethod(gm);
            return m;
        } catch (NoSuchMethodException e) {
            return null;
        }
    }

    public static Method prepareSetterMethod(Field f) {
        return prepareSetterMethod(f, f.getType());
    }

    public static Method prepareSetterMethod(Field f, Class type) {
        String t = prepareAccessorMainPartName(f.getName());
        String sm;
        @SuppressWarnings("unused")
        String gm;
        if (f.getType().isPrimitive() && f.getType().equals(boolean.class)) {
            sm = "set" + t;
            gm = "is" + t;
        } else {
            sm = "set" + t;
            gm = "get" + t;
        }

        try {
            Method m = f.getDeclaringClass().getMethod(sm, type);
            return m;
        } catch (NoSuchMethodException e) {
            return null;
            // throw new KernelException("Class " +
            // f.getDeclaringClass().getName() + " has no setter of field " +
            // f.getName(), e);
        }
    }

    public static String prepareAccessorMainPartName(final String fieldName) {
        if (fieldName.length() == 1) {
            return fieldName.toUpperCase();
        }

        String r;
        if (Character.isUpperCase(fieldName.charAt(1))) {
            r = fieldName.substring(0, 1);
        } else {
            r = fieldName.substring(0, 1).toUpperCase();
        }

        r += fieldName.substring(1);

        return r;
    }
}

Related

  1. getGetters(Class clazz)
  2. getGetters(final Class clazz)
  3. getGetters(Object bean)
  4. getGetters(Object obj)
  5. getGettersAndSetters(Object obj)
  6. getGetterShorthandName(Method method)
  7. getGettersMethods(Object object)
  8. getGetterValue(Method method, Object o)
  9. getGetterWithPrefix(final Class target, final String property, final String prefix)