Here you can find the source of findFieldFromGetter(Class> clazz, Method method)
Parameter | Description |
---|---|
clazz | the clazz |
method | the method |
public static Field findFieldFromGetter(Class<?> clazz, Method method)
//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); } }