Java Reflection Field Set setFieldAccessible(Class clazz, String... names)

Here you can find the source of setFieldAccessible(Class clazz, String... names)

Description

set Field Accessible

License

Open Source License

Declaration

public static void setFieldAccessible(Class<?> clazz, String... names) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Field;

public class Main {
    public static void setFieldAccessible(Class<?> clazz, String... names) {
        try {//  w  w w. j  a  v a  2  s  . c om
            Field field = getField(clazz, names);
            if (field != null)
                field.setAccessible(true);
        } catch (Exception e) {
        }
    }

    public static Field getField(Class<?> clazz, String... names) {
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                for (String fieldName : names) {
                    if (field.getName().equalsIgnoreCase(fieldName)) {
                        field.setAccessible(true);
                        return field;
                    }
                }
            }
        } catch (Exception e) {
            throw e;
        }
        return null;
    }
}

Related

  1. setField(String fieldName, Object instance, Class instanceClass, Object value)
  2. setField(String fieldName, Object instance, Object value)
  3. setField(String name, Object target, Object value)
  4. setField(String name, Object target, Object value)
  5. setField(T object, Field field, ResourceBundle bundle)
  6. setFieldAccessible(Field field)
  7. setFieldBySomeMethod(List setMethods, Object object, Object value)
  8. setFieldContent(final Object obj, final String name, final Object value)
  9. setFieldEditable(Class clazz, String fieldName)