Java Reflection Field Value Set setFieldValue(Object object, String fieldName, Object fieldValue)

Here you can find the source of setFieldValue(Object object, String fieldName, Object fieldValue)

Description

set Field Value

License

Apache License

Declaration

public static void setFieldValue(Object object, String fieldName, Object fieldValue) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   www.j a v a  2 s  .  c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.io.IOException;

import java.lang.reflect.Field;

public class Main {
    public static void setFieldValue(Object object, String fieldName, Object fieldValue) throws IOException {
        try {
            if (object instanceof Class)
                getField((Class<?>) object, fieldName).set(null, fieldValue);
            else
                getField(object.getClass(), fieldName).set(object, fieldValue);
        } catch (Throwable t) {
            throw new IOException(t);
        }
    }

    public static Field getField(Class<?> clazz, String fieldName) throws IOException {
        if (clazz == null)
            throw new IOException("clazz is null");
        if (fieldName == null)
            throw new IOException("fieldName is null");
        if (fieldName.isEmpty())
            throw new IOException("fieldName is empty");
        Field field = null;
        try {
            field = clazz.getDeclaredField(fieldName);
        } catch (Throwable t0) {
            try {
                field = clazz.getField(fieldName);
            } catch (Throwable t1) {
                if (clazz.getSuperclass() != null) {
                    field = getField(clazz.getSuperclass(), fieldName);
                } else
                    throw new IOException("fail to get field for " + clazz.getName() + "." + fieldName, t1);
            }
        }
        field.setAccessible(true);
        return field;
    }
}

Related

  1. setFieldValue(Object obj, String fieldName, String fieldValue)
  2. setFieldValue(Object obj, String name, Object value)
  3. setFieldValue(Object object, Field field, Object value)
  4. setFieldValue(Object object, String fieldName, Object fieldValue)
  5. setFieldValue(Object object, String fieldName, Object fieldValue)
  6. setFieldValue(Object object, String fieldName, Object value)
  7. setFieldValue(Object object, String fieldName, Object value)
  8. setFieldValue(Object object, String fieldName, Object value)
  9. setFieldValue(Object object, String fieldName, Object value)