Java Reflection Field Set setField(Object object, String fieldName, Object newValue, boolean isFindDeclaredField, boolean isUpwardFind)

Here you can find the source of setField(Object object, String fieldName, Object newValue, boolean isFindDeclaredField, boolean isUpwardFind)

Description

set Field

License

Apache License

Declaration

public static boolean setField(Object object, String fieldName, Object newValue, boolean isFindDeclaredField,
        boolean isUpwardFind) 

Method Source Code

//package com.java2s;
/*/*from   w ww  . j a  va  2  s . co m*/
 * Copyright (C) 2013 Peng fei Pan <sky@xiaopan.me>
 *
 * 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.Field;

public class Main {

    public static boolean setField(Object object, String fieldName, Object newValue, boolean isFindDeclaredField,
            boolean isUpwardFind) {
        boolean result = false;
        Field field = getField(object.getClass(), fieldName, isFindDeclaredField, isUpwardFind);
        if (field != null) {
            try {
                field.setAccessible(true);
                field.set(object, newValue);
                result = true;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                result = false;
            }
        }
        return result;
    }

    public static Field getField(Class<?> sourceClass, String fieldName, boolean isFindDeclaredField,
            boolean isUpwardFind) {
        Field field = null;
        try {
            field = isFindDeclaredField ? sourceClass.getDeclaredField(fieldName) : sourceClass.getField(fieldName);
        } catch (NoSuchFieldException e1) {
            if (isUpwardFind) {
                Class<?> classs = sourceClass.getSuperclass();
                while (field == null && classs != null) {
                    try {
                        field = isFindDeclaredField ? classs.getDeclaredField(fieldName)
                                : classs.getField(fieldName);
                    } catch (NoSuchFieldException e11) {
                        classs = classs.getSuperclass();
                    }
                }
            }
        }
        return field;
    }

    public static Field getField(Class<?> sourceClass, String fieldName) {
        return getField(sourceClass, fieldName, true, true);
    }
}

Related

  1. setField(Object object, Field field, Object value)
  2. setField(Object object, Object value, String name)
  3. setField(Object object, String field, Object value)
  4. setField(Object object, String fieldName, int value)
  5. setField(Object object, String fieldName, Object newValue)
  6. setField(Object object, String fieldName, Object value)
  7. setField(Object object, String fieldName, Object value)
  8. setField(Object object, String fieldName, Object value)
  9. setField(Object object, String fieldName, Object value)