Example usage for java.lang Class getDeclaredField

List of usage examples for java.lang Class getDeclaredField

Introduction

In this page you can find the example usage for java.lang Class getDeclaredField.

Prototype

@CallerSensitive
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException 

Source Link

Document

Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.

Usage

From source file:org.jongo.model.IdSpecSet.java

public static Field idField(Class<?> spec) {
    try {//from  ww  w  .j  a v  a 2 s.c o m
        return spec.getDeclaredField("_id");
    } catch (NoSuchFieldException e) {
        try {
            return spec.getDeclaredField("id");
        } catch (NoSuchFieldException e1) {
            throw new RuntimeException("_id field missing", e1);
        }
    }
}

From source file:com.francetelecom.clara.cloud.presentation.utils.GetObjectsUtils.java

/**
 * Util method, which does getDeclaredField() on the class, and on its inheritance hierarchy if needed. Allows to access private fields
 * @param objectClass class of the object which contains the field
 * @param fieldName name of the field to find
 * @return a Field if it exists/*  w  ww .jav a2  s .c  o  m*/
 * @throws NoSuchFieldException raised when no field was found
 */
public static Field getAnyField(Class objectClass, String fieldName) throws NoSuchFieldException {
    try {
        return objectClass.getDeclaredField(fieldName);
    } catch (NoSuchFieldException e) {
        Class superClass = objectClass.getSuperclass();
        if (superClass == null) {
            throw e;
        } else {
            return getAnyField(superClass, fieldName);
        }
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Method getSetMethod(Class objectClass, String fieldName) {

    try {/*from ww  w. j  a  v  a  2 s  .co m*/

        Class[] parameterTypes = new Class[1];

        Field field = objectClass.getDeclaredField(fieldName);

        parameterTypes[0] = field.getType();

        StringBuffer sb = new StringBuffer();

        sb.append("set");

        sb.append(fieldName.substring(0, 1).toUpperCase());

        sb.append(fieldName.substring(1));

        Method method = objectClass.getMethod(sb.toString(), parameterTypes);

        return method;

    } catch (Exception e) {

        e.printStackTrace();

    }

    return null;

}

From source file:Main.java

public static File getExternalDownload() {
    try {//from   www  .j a  va 2s .c  o  m
        Class<?> c = Class.forName("android.os.Environment");
        Method m = c.getDeclaredMethod("getExternalStoragePublicDirectory", String.class);
        String download = c.getDeclaredField("DIRECTORY_DOWNLOADS").get(null).toString();
        return (File) m.invoke(null, download);
    } catch (Exception e) {
        return new File(Environment.getExternalStorageDirectory(), "Download");
    }
}

From source file:com.ikanow.infinit.e.data_model.custom.InfiniteEsInputFormat.java

private static void enableInputSplitDebugMode(List<InputSplit> list) {
    for (InputSplit is : list) {
        try {// w w w.ja v  a  2  s .  co  m
            Class<?> c = is.getClass();

            Field nodeIp = c.getDeclaredField("nodeIp");
            nodeIp.setAccessible(true);
            nodeIp.set(is, "127.0.0.1");
        } catch (Exception e) {
            //DEBUG
            //e.printStackTrace();
        }
    }
}

From source file:Main.java

public static Field getField(Class<?> targetClass, String name) {
    if ((targetClass == null) || (TextUtils.isEmpty(name))) {
        return null;
    }/*w w  w. ja v  a 2s  . c o  m*/

    try {
        return targetClass.getDeclaredField(name);
    } catch (Exception e) {
        if (DEBUG) {
            e.printStackTrace();
        }
    }

    return null;
}

From source file:Main.java

private static void setField(Object obj, Class<?> cl, String field, Object value)
        throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException {

    Field localField = cl.getDeclaredField(field);
    localField.setAccessible(true);/*from ww  w .j a  v a 2s  . c  o m*/
    localField.set(obj, value);
}

From source file:Main.java

private static Field getField(Class thisClass, String fieldName) throws NoSuchFieldException {

    if (fieldName == null) {
        throw new NoSuchFieldException("Error field !");
    }/*from ww w  . j  a v a2s.c o m*/

    Field field = thisClass.getDeclaredField(fieldName);
    return field;
}

From source file:com.isthari.spring.cloud.config.cassandra.CassandraEnvironmentRepositoryTest.java

@BeforeClass
public static void startCassandra() throws Exception {
    String embedded = System.getProperty("isthari.cassandra.test.embedded");
    useEmbeddedCassandra = embedded == null || "true".equals(useEmbeddedCassandra);

    if (useEmbeddedCassandra) {
        cleanUp();/*  w w  w.j a va2s .  c  om*/
        EmbeddedCassandraService cassandra = new EmbeddedCassandraService();
        cassandra.start();
    }

    repository = new CassandraEnvironmentRepository(null, "127.0.0.1", null, null, true);

    // LOAD TEST DATASET
    Class<?> clazz = repository.getClass();
    Field sessionField = clazz.getDeclaredField("session");
    sessionField.setAccessible(true);
    session = (Session) sessionField.get(repository);

    stmtApplicationLabelProfile = session.prepare(
            "insert into application_label_version (application, label , profile, version  ) VALUES (?,?,?,?)");
    stmtApplicationSnapshot = session
            .prepare("insert into configuration_snapshot (application, version, parameters) values (?,?,?)");

    createSnapshot("application", "master", "", new String[] { "param4" }, new String[] { "value4" });
}

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

public static Field getField(Class cls, String attrName) throws Throwable {
    Field field = null;/* w w w  .  j  av  a 2s  .com*/
    try {
        field = cls.getDeclaredField(attrName);
    } catch (NoSuchFieldException e) {
        try {
            // Se va a buscar en la superclase
            field = cls.getSuperclass().getDeclaredField(attrName);
        } catch (NoSuchFieldException e2) {
            try {
                // Se va a buscar en la superclase 2
                field = cls.getSuperclass().getSuperclass().getDeclaredField(attrName);
            } catch (NoSuchFieldException e3) {
                field = cls.getSuperclass().getSuperclass().getSuperclass().getDeclaredField(attrName);

            }
        }
    } catch (SecurityException e) {
        throw e;
    }
    return field;
}