Java Reflection Field Find findField(Object obj, String fieldName, Class type)

Here you can find the source of findField(Object obj, String fieldName, Class type)

Description

find Field

License

Apache License

Declaration

public static Field findField(Object obj, String fieldName, Class<?> type) 

Method Source Code

//package com.java2s;
/*//  w w  w.j  a  v a 2s  .c o  m
 * Copyright 2013-2015 the original author or authors.
 *
 * 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.
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

import java.lang.reflect.Member;
import java.lang.reflect.Method;

public class Main {
    public static Field findField(Object target, String fieldName) {
        Class<?> targetClass = getClass(target);
        Field theField;
        try {
            theField = targetClass.getDeclaredField(fieldName);
            return setAccess(theField);
        } catch (NoSuchFieldException e) {
            if (targetClass.getSuperclass() != null)
                return findField(targetClass.getSuperclass(), fieldName);
            else
                return null;
        }
    }

    public static Field findField(Object obj, String fieldName, Class<?> type) {
        if (obj == null)
            return null;
        Field theField = findField(obj, fieldName);
        if (isFieldType(theField, type))
            return theField;
        return null;
    }

    public static Class<?> getClass(Object target) {
        return (target instanceof Class ? (Class<?>) target : target.getClass());
    }

    @SuppressWarnings("unchecked")
    public static <T extends Member> T setAccess(final Member theMember) {
        if (theMember instanceof Method) {
            Method real = (Method) theMember;
            if (!real.isAccessible())
                real.setAccessible(true);
        } else if (theMember instanceof Constructor) {
            @SuppressWarnings("rawtypes")
            Constructor real = (Constructor) theMember;
            if (!real.isAccessible())
                real.setAccessible(true);
        } else if (theMember instanceof Field) {
            Field real = (Field) theMember;
            if (!real.isAccessible())
                real.setAccessible(true);
        }
        return (T) theMember;
    }

    public static boolean isFieldType(Field field, Class<?> clz) {
        if (field != null) {
            Class<?> type = field.getType();
            if (type.isAssignableFrom(clz)) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. findField(final String className, final String fieldName)
  2. findField(Object container, String memberName)
  3. findField(Object instance, String name)
  4. findField(Object obj, String beanPath)
  5. findField(Object obj, String fieldName)
  6. findField(Object obj, String name)
  7. findField(Object object, String fieldName)
  8. findField(Object object, String name, Class klass)
  9. findField(Object propertyId, Class classOfItem)