Java Reflection Field Find findFieldFromGetter(Class clazz, Method method)

Here you can find the source of findFieldFromGetter(Class clazz, Method method)

Description

Find field from getter.

License

Apache License

Parameter

Parameter Description
clazz the clazz
method the method

Return

the field

Declaration

public static Field findFieldFromGetter(Class<?> clazz, Method method) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.lang.reflect.Field;

import java.lang.reflect.Method;

public class Main {
    /**/*from   w w w .ja  va 2s . c o m*/
     * Find field from getter.
     *
     * @param clazz the clazz
     * @param method the method
     * @return the field
     */
    public static Field findFieldFromGetter(Class<?> clazz, Method method) {
        String fieldName = getFieldNameFromGetter(method.getName());
        Field found = findField(clazz, fieldName);
        return found;
    }

    /**
     * For a given getVariable return variable.
     *
     * @param getter the getter
     * @return null if not found
     */
    public static String getFieldNameFromGetter(String getter) {
        if (getter == null) {
            return null;
        }
        if (getter.startsWith("get") == true && getter.length() >= 4) {
            return getter.substring(3, 4).toLowerCase() + getter.substring(4);
        }
        if (getter.startsWith("is") == true && getter.length() >= 3) {
            return getter.substring(2, 3).toLowerCase() + getter.substring(3);
        }
        return null;
    }

    /**
     * Find a field of given bean.
     *
     * @param bean the bean
     * @param fieldName the field name
     * @return null if not found
     */
    public static Field findField(Object bean, String fieldName) {
        return findField(bean.getClass(), fieldName);
    }

    /**
     * Try find a field with given name. Goes throw class hierarchie.
     *
     * @param cls the cls
     * @param fieldName the field name
     * @return the field
     */
    public static Field findField(Class<?> cls, String fieldName) {
        Field f = null;
        try {
            f = cls.getDeclaredField(fieldName);
        } catch (SecurityException ex) {
        } catch (NoSuchFieldException ex) {
        }
        if (f != null) {
            return f;
        }
        if (cls == Object.class || cls.getSuperclass() == null) {
            return null;
        }
        return findField(cls.getSuperclass(), fieldName);
    }
}

Related

  1. findField(String string, Class clazz)
  2. findField0(Class clazz, String name, String methodName)
  3. findFieldByName(Class owner, String name)
  4. findFieldEx(Class type, Class annotationClass)
  5. findFieldFromClassHierarchy(Class clazz, String fieldName)
  6. findFieldIn(Class type, String name)
  7. findFieldInClass(Class clazz, String fieldName)
  8. findFieldInClassHierarchy(Class clazz, String fieldName)
  9. findFieldIncludeSuperclass(String fieldName, Class clazz)