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:eu.riscoss.RemoteRiskAnalyserMain.java

static void loadJSmile() throws Exception {
    File dir = new File(FileUtils.getTempDirectory(), "jnativelibs-" + RandomStringUtils.randomAlphabetic(30));
    if (!dir.mkdir()) {
        throw new Exception("failed to make dir");
    }/*from  w  w w  . ja va2  s  .c  om*/
    File jsmile = new File(dir, "libjsmile.so");
    URL jsmURL = Thread.currentThread().getContextClassLoader().getResource("libjsmile.so");
    FileUtils.copyURLToFile(jsmURL, jsmile);
    System.setProperty("java.library.path",
            System.getProperty("java.library.path") + ":" + dir.getAbsolutePath());

    // flush the library paths
    Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
    sysPathsField.setAccessible(true);
    sysPathsField.set(null, null);

    // Check that it works...
    System.loadLibrary("jsmile");

    dir.deleteOnExit();
    jsmile.deleteOnExit();
}

From source file:Main.java

/**
 * Copy fields from parent object to child object.
 *
 * @param parent parent object//from  w  w w .ja  va 2  s . c o  m
 * @param child child object
 * @param <T> child class
 * @return filled child object
 */
public static <T> T shallowCopy(Object parent, T child) {
    try {
        List<Field> fields = new ArrayList<>();
        Class clazz = parent.getClass();
        do {
            fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
        } while (!(clazz = clazz.getSuperclass()).equals(Object.class));

        for (Field field : fields) {
            field.setAccessible(true);
            field.set(child, field.get(parent));
        }

        return child;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void setProperty(Object paramObject1, String paramString, Object paramObject2) {
    try {/*w ww .j  a v a  2  s.  c o  m*/
        Field localField = paramObject1.getClass().getDeclaredField(paramString);
        localField.setAccessible(true);
        localField.set(paramObject1, paramObject2);
    } catch (Exception var4) {
        ;
    }

}

From source file:Main.java

public static Field findFiledWithName(Field[] fields, String filedName) {
    if (fields != null) {
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.getName().equals(filedName)) {
                return field;
            }/*from  w  w  w.j a  v  a 2s.  c o m*/
        }
    }
    return null;
}

From source file:Main.java

public static Field findFiledWithName(Class clazz, String filedName) {
    Field[] fields = getAllFiedFromClassAndSuper(clazz, false);
    if (fields != null) {
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.getName().equals(filedName)) {
                return field;
            }// w  w w . j  av a  2s  . co m
        }
    }
    return null;
}

From source file:Main.java

static Map<String, Object> objectToMap(Object object) {
    Map<String, Object> map = new HashMap<>();

    Class cls = object.getClass();
    while (cls != null) {
        for (Field field : cls.getDeclaredFields()) {
            field.setAccessible(true);

            Object value = null;/*from  w ww.  j a  v  a 2 s . c  o  m*/
            try {
                value = field.get(object);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            if (value != null)
                map.put(field.getName(), value);
        }

        cls = cls.getSuperclass();
    }

    return map;
}

From source file:Main.java

public static <T> T getObjInList(Class<T> clazz, Collection<T> all, String valueStr, String propName)
        throws Exception {
    if (all.isEmpty() || valueStr == null) {
        return null;
    }/*from ww w .j  a v  a2  s.  c om*/
    T obj = null;
    Iterator<T> iter = all.iterator();
    for (; iter.hasNext();) {
        T temp = iter.next();
        Object match = null;
        if (propName == null) {
            match = temp.toString();
        } else {
            Field field = clazz.getField(propName);
            field.setAccessible(true);
            match = field.get(temp);
        }
        if (valueStr.equals(match)) {
            obj = temp;
            break;
        }
    }
    return obj;
}

From source file:Main.java

/** 
 * Returns the KeyEvent-code (only for VK_a..Z).
 * If no key-event could be found, {@link Integer#MIN_VALUE} is returned. 
 *//*from   ww w  . j  ava2s  .com*/
public static int getKeyEvent(Character ch) {

    int ke = Integer.MIN_VALUE;
    try {
        Field f = KeyEvent.class.getField("VK_" + Character.toUpperCase(ch));
        f.setAccessible(true);
        ke = (Integer) f.get(null);
    } catch (Exception e) {
    }
    return ke;
}

From source file:Main.java

/**
 * Extracts a field from a class using reflection.
 *
 * @param clazz from which to get the field object
 * @param name the name of the field object
 * @return the field object.//w  w  w .  ja  v a 2  s . c om
 * @throws PrivilegedActionException
 */
static Field getField(final Class<?> clazz, final String name) throws PrivilegedActionException {
    final PrivilegedExceptionAction<Field> action = new PrivilegedExceptionAction<Field>() {
        public Field run() throws Exception {
            Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            return field;
        }
    };

    return AccessController.doPrivileged(action);
}

From source file:Main.java

/**
 * Gets the value of a static field.//www.j  a  v a2 s  . c o  m
 *
 * @param clazz from which to get the field value
 * @param name the name of the field
 * @return the value of the field.
 * @throws PrivilegedActionException
 */
static <T> T getStaticFieldValue(final Class<?> clazz, final String name) throws PrivilegedActionException {
    final PrivilegedExceptionAction<T> action = new PrivilegedExceptionAction<T>() {
        @SuppressWarnings("unchecked")
        public T run() throws Exception {
            Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            return (T) field.get(null);
        }
    };

    return AccessController.doPrivileged(action);
}