get Field by field name - Android java.lang.reflect

Android examples for java.lang.reflect:Field Name

Description

get Field by field name

Demo Code

/*/*from w  ww  . j  a va 2  s . co m*/
 * Copyright (C) 2015 HouKx <hkx.aidream@gmail.com>
 *
 * 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.
 */
//package com.java2s;

import java.lang.reflect.Field;

public class Main {
    private static Object[] getField(Object obj, String elFieldName,
            boolean resolveParent) throws IllegalAccessException,
            IllegalArgumentException, NoSuchFieldException {
        if (obj == null) {
            return null;
        }
        String[] fieldNames = elFieldName.split("[.]");
        Object targetObj = obj;
        Class<?> targetClass = targetObj.getClass();
        Object val = null;
        int i = 0;
        Field field = null;
        Object[] rs = new Object[2];
        for (String fName : fieldNames) {
            i++;
            field = getField_(targetClass, fName, resolveParent);
            field.setAccessible(true);
            rs[0] = field;
            rs[1] = targetObj;
            val = field.get(targetObj);
            if (val == null) {
                if (i < fieldNames.length) {
                    throw new IllegalAccessException(
                            "can not getFieldValue as field '" + fName
                                    + "' value is null in '"
                                    + targetClass.getName() + "'");
                }
                break;
            }
            targetObj = val;
            targetClass = targetObj.getClass();
        }
        return rs;
    }

    public static Field getField_(Class<?> targetClass, String fieldName,
            boolean resolveParent) throws IllegalAccessException,
            IllegalArgumentException, NoSuchFieldException {
        NoSuchFieldException noSuchFieldExceptionOccor = null;
        Field rsField = null;
        try {
            Field field = targetClass.getDeclaredField(fieldName);
            rsField = field;
            if (!resolveParent) {
                field.setAccessible(true);
                return field;
            }
        } catch (NoSuchFieldException e) {
            noSuchFieldExceptionOccor = e;
        }
        if (noSuchFieldExceptionOccor != null) {
            if (resolveParent) {
                while (true) {
                    targetClass = targetClass.getSuperclass();
                    if (targetClass == null) {
                        break;
                    }
                    try {
                        Field field = targetClass
                                .getDeclaredField(fieldName);
                        field.setAccessible(true);
                        return rsField = field;
                    } catch (NoSuchFieldException e) {
                        if (targetClass.getSuperclass() == null) {
                            throw e;
                        }
                    }
                }
            } else {
                throw noSuchFieldExceptionOccor;
            }
        }
        return rsField;
    }
}

Related Tutorials