Example usage for java.lang.reflect Field setAccessible

List of usage examples for java.lang.reflect Field setAccessible

Introduction

In this page you can find the example usage for java.lang.reflect Field setAccessible.

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:de.taimos.dvalin.mongo.Tester.java

@BeforeClass
public static void init() {
    try {/*  ww w . j  a v a 2s  .  co m*/
        Field mongoField = AbstractMongoDAO.class.getDeclaredField("mongo");
        mongoField.setAccessible(true);
        mongoField.set(Tester.dao, ABaseTest.mongo);

        Field jongoField = AbstractMongoDAO.class.getDeclaredField("jongo");
        jongoField.setAccessible(true);
        jongoField.set(Tester.dao, JongoFactory.createDefault(ABaseTest.mongo.getDB(ABaseTest.dbName)));

        Field dbField = AbstractMongoDAO.class.getDeclaredField("db");
        dbField.setAccessible(true);
        dbField.set(Tester.dao, ABaseTest.mongo.getDatabase(ABaseTest.dbName));

        Mongobee bee = new Mongobee(ABaseTest.mongo);
        bee.setChangeLogsScanPackage("de.taimos.dvalin.mongo.changelog");
        bee.setDbName(ABaseTest.dbName);
        bee.setEnabled(true);
        bee.execute();
        Tester.dao.init();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.pinterest.deployservice.common.ChangeFeedJob.java

private static String toStringRepresentation(Object object) throws IllegalAccessException {
    if (object == null) {
        return "None";
    }//from w  ww.ja  va  2  s .c  o m

    if (object instanceof List) {
        List<?> list = (List<?>) object;
        if (list.isEmpty()) {
            return "[]";
        }

        StringBuilder sb = new StringBuilder("[");
        for (Object element : list) {
            sb.append(toStringRepresentation(element));
            sb.append(", ");
        }

        sb.delete(sb.length() - 2, sb.length());
        sb.append(']');
        return sb.toString();
    }

    Field[] fields = object.getClass().getDeclaredFields();
    if (fields.length == 0) {
        return "{}";
    }

    StringBuilder sb = new StringBuilder("{");
    for (Field field : fields) {
        field.setAccessible(true);
        Object fieldItem = field.get(object);
        sb.append(field.getName());
        sb.append(":");
        sb.append(fieldItem);
        sb.append(", ");
    }

    sb.delete(sb.length() - 2, sb.length());
    sb.append('}');
    return sb.toString();
}

From source file:com.relicum.titleapi.Reflection.ReflectionUtil.java

public static void setPrivateField(Class<EntityArmorStand> clazz, Object handle, String fieldName, Object value)
        throws Exception {
    Field field = clazz.getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(handle, value);//from  w w w  .  j av a2s.co  m
}

From source file:com.u2apple.tool.util.AndroidDeviceUtils.java

public static String getPropertyByKey(AndroidDeviceRanking androidDevice, String key) {
    if (androidDevice == null || key == null) {
        return null;
    }//www.j av a 2  s.  co m
    for (Field field : androidDevice.getClass().getSuperclass().getDeclaredFields()) {
        field.setAccessible(true);
        if (field.isAnnotationPresent(Key.class)) {
            Key keyAnno = field.getAnnotation(Key.class);
            if (keyAnno.value().equals(key)) {
                try {
                    return (String) field.get(androidDevice);
                } catch (IllegalArgumentException | IllegalAccessException ex) {
                    Logger.getLogger(AndroidDeviceUtils.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    return null;
}

From source file:net.minecraftforge.fml.relauncher.ReflectionHelper.java

public static Field findField(Class<?> clazz, String... fieldNames) {
    Exception failed = null;/*from  ww w  .  j a  v  a2s.co m*/
    for (String fieldName : fieldNames) {
        try {
            Field f = clazz.getDeclaredField(fieldName);
            f.setAccessible(true);
            return f;
        } catch (Exception e) {
            failed = e;
        }
    }
    throw new UnableToFindFieldException(fieldNames, failed);
}

From source file:com.aw.support.reflection.AttributeAccessor.java

/**
 * @param target/*  ww  w  . j a v a2  s.co m*/
 * @param attrName
 * @return
 */
public static void set(Object target, String attrName, Object value) {
    Field field = null;
    try {
        Class cls = target.getClass();
        field = getField(cls, attrName);
        field.setAccessible(true);
        field.set(target, value);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("Error setting the value of the attribute:<" + attrName
                + "> of object:<" + target + "> check: Attribute Type:<" + field.getType() + "> value Type: <"
                + value.getClass() + ">");
    } catch (Throwable e) {
        e.printStackTrace();
        throw new IllegalStateException(
                "Error setting the value of the attribute:<" + attrName + "> of object:<" + target + ">");
    }
}

From source file:de.cbb.mplayer.util.ReflectionHelper.java

/**
 * Utility method to read a field value. If the field is not accessible, it will be set to be accessible.
 * @param object Instance in which the value should be read
 * @param name Name of the field who's value should be read
 * @return The value of the field//www  . jav  a 2 s  .co m
 */
public static Object getFieldValue(Object object, String name) {
    try {
        Field field = getField(object.getClass(), name);
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        return field.get(object);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        throw new RuntimeException(
                "Could not read field value: " + object.getClass().getSimpleName() + "." + name, e); //$NON-NLS-1$ //$NON-NLS-2$
    }
}

From source file:de.cbb.mplayer.util.ReflectionHelper.java

/**
 * Utility method to set a field to a value. If the field is not accessible, it will be set to be accessible.
 * @param object Instance in which the value should be set
 * @param name Name of the field who's value should be set
 * @param value The value to be set/*from ww  w  .ja v  a  2s.c  om*/
 */
public static void setFieldValue(Object object, String name, Object value) {
    try {
        Field field = getField(object.getClass(), name);
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        field.set(object, value);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        throw new RuntimeException(
                "Could not set field value: " + object.getClass().getSimpleName() + "." + name, e); //$NON-NLS-1$ //$NON-NLS-2$
    }

}

From source file:net.minecraftforge.fml.relauncher.ReflectionHelper.java

public static <T, E> void setPrivateValue(Class<? super T> classToAccess, T instance, E value, int fieldIndex) {
    try {//www.ja  v  a  2  s.  co  m
        Field f = classToAccess.getDeclaredFields()[fieldIndex];
        f.setAccessible(true);
        f.set(instance, value);
    } catch (Exception e) {
        throw new UnableToAccessFieldException(new String[0], e);
    }
}

From source file:net.minecraftforge.fml.relauncher.ReflectionHelper.java

@SuppressWarnings("unchecked")
public static <T, E> T getPrivateValue(Class<? super E> classToAccess, @Nullable E instance, int fieldIndex) {
    try {//w w w.j av  a  2 s.  c o  m
        Field f = classToAccess.getDeclaredFields()[fieldIndex];
        f.setAccessible(true);
        return (T) f.get(instance);
    } catch (Exception e) {
        throw new UnableToAccessFieldException(new String[0], e);
    }
}