Example usage for java.lang.reflect Field get

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Returns the value of the field represented by this Field , on the specified object.

Usage

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();/*from   w  w  w  .jav a  2s. c  o  m*/
        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:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c = null;//from  w  w  w . j a v a 2 s .c  om
    Object obj = null;
    Field field = null;
    int x = 0, sbar = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        sbar = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        Log.e("getStatusBarHight()", "get status bar height fail");
        e1.printStackTrace();
    }
    int statusBarHeight = sbar;
    Log.i("onPreDraw", "statusBarHeight: " + statusBarHeight);
    return statusBarHeight;
}

From source file:com.github.jknack.handlebars.helper.DefaultFilterHelper.java

private static <T> String getFieldValue(T input, String fielName) {
    String typeVal = "";
    try {// w w w . ja  v a  2s. c o m
        Class<T> clazz = (Class<T>) input.getClass();
        Field field = clazz.getDeclaredField(fielName);
        field.setAccessible(true);
        typeVal = (String) field.get(input);
    } catch (Exception ex) {

    }
    return typeVal;
}

From source file:Main.java

/**
 * getChildObjs//from  w w w .jav  a  2  s.  c  o m
 *
 * @param object the obj to save to db ,this object contains the property private List<Child> children;
 * @return the List<Child> value
 */
public static List<List> getChildObjs(Object object) {
    Field[] fields = object.getClass().getDeclaredFields();
    List<List> result = new ArrayList<>();
    for (Field field : fields) {
        if ("java.util.List".equals(field.getType().getName())) {
            List list = null;
            try {
                field.setAccessible(true);
                list = (List) field.get(object);
                result.add(list);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:org.mstc.zmq.json.Encoder.java

public static String encode(Object o) throws IOException {
    StringWriter writer = new StringWriter();
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
    JsonFactory factory = mapper.getFactory();
    try (JsonGenerator g = factory.createGenerator(writer)) {
        g.writeStartObject();/*from  w  w w .  java2  s  .  com*/
        for (Field f : o.getClass().getDeclaredFields()) {
            try {
                f.setAccessible(true);
                Object value = f.get(o);
                if (value != null) {
                    if (f.getType().isAssignableFrom(List.class)) {
                        String items = mapper.writeValueAsString(value);
                        g.writeStringField(f.getName(), items);
                    } else if (f.getType().isArray()) {
                        if (f.getType().getComponentType().isAssignableFrom(byte.class)) {
                            g.writeBinaryField(f.getName(), (byte[]) value);
                        } else {
                            int length = Array.getLength(value);
                            g.writeFieldName(f.getName());
                            g.writeStartArray();
                            for (int i = 0; i < length; i++) {
                                Object av = Array.get(value, i);
                                if (av instanceof Double) {
                                    g.writeNumber(new BigDecimal((Double) av).toPlainString());
                                } else if (av instanceof Float) {
                                    g.writeNumber(new BigDecimal((Float) av).toPlainString());
                                } else if (av instanceof Integer) {
                                    g.writeNumber(new BigDecimal((Integer) av).toPlainString());
                                } else {
                                    g.writeObject(av);
                                }
                                /*if (av instanceof Double)
                                g.writeNumber(new BigDecimal((Double) av));
                                else if (av instanceof Float)
                                g.writeNumber(new BigDecimal((Float) av));
                                else if (av instanceof Integer)
                                g.writeNumber((Integer) av);*/
                            }
                            g.writeEndArray();
                        }
                    } else {
                        g.writeObjectField(f.getName(), value);
                    }
                }

            } catch (IllegalAccessException e) {
                logger.warn("Could not get field: {}", f.getName(), e);
            }
        }
        g.writeEndObject();
    }
    if (logger.isDebugEnabled())
        logger.debug(writer.toString());
    return writer.toString();
}

From source file:net.refractions.udig.ui.internal.Messages.java

private static String bind(String fieldName) {
    Field field;
    try {//w  w  w.  j  ava2 s  .  com
        field = Messages.class.getDeclaredField(fieldName);
        return (String) field.get(null);
    } catch (Exception e) {
        UiPlugin.log("Error loading key " + fieldName, e); //$NON-NLS-1$
    }
    return null;
}

From source file:com.netflix.spinnaker.kork.jedis.JedisHealthIndicatorFactory.java

public static HealthIndicator build(Pool<Jedis> jedisPool) {
    try {//from w w  w  .ja  v  a 2s  . co  m
        final Pool<Jedis> src = jedisPool;
        final Field poolAccess = Pool.class.getDeclaredField("internalPool");
        poolAccess.setAccessible(true);
        GenericObjectPool<Jedis> internal = (GenericObjectPool<Jedis>) poolAccess.get(jedisPool);
        return () -> {
            Jedis jedis = null;
            Health.Builder health;
            try {
                jedis = src.getResource();
                if ("PONG".equals(jedis.ping())) {
                    health = Health.up();
                } else {
                    health = Health.down();
                }
            } catch (Exception ex) {
                health = Health.down(ex);
            } finally {
                if (jedis != null)
                    jedis.close();
            }
            health.withDetail("maxIdle", internal.getMaxIdle());
            health.withDetail("minIdle", internal.getMinIdle());
            health.withDetail("numActive", internal.getNumActive());
            health.withDetail("numIdle", internal.getNumIdle());
            health.withDetail("numWaiters", internal.getNumWaiters());

            return health.build();
        };
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new BeanCreationException("Error creating Redis health indicator", e);
    }
}

From source file:Main.java

public static int getStatusBarHeight(Context context) {
    Class<?> c = null;/*w  w w . j a va2  s  . co  m*/
    Object obj = null;
    Field field = null;
    int x = 0, statusBarHeight = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return statusBarHeight;
}

From source file:Main.java

public static int getStatusBarHeight1(Context activity) {
    Class<?> c = null;//from   w  ww .ja v  a 2s  .  co m
    Object obj = null;
    Field field = null;
    int x = 0, statusBarHeight = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        statusBarHeight = activity.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return statusBarHeight;
}

From source file:com.kenai.redminenb.repository.RedmineManagerFactoryHelper.java

public static Transport getTransportFromManager(final RedmineManager rm) {
    return AccessController.doPrivileged(new PrivilegedAction<Transport>() {

        @Override/*from  w  w  w  . j av  a 2 s.c om*/
        public Transport run() {
            try {
                Field transportField = RedmineManager.class.getDeclaredField("transport");
                transportField.setAccessible(true);
                return (Transport) transportField.get(rm);
            } catch (NoSuchFieldException | SecurityException | IllegalArgumentException
                    | IllegalAccessException ex) {
                throw new RuntimeException("Failed to get transport from redmine manager", ex);
            }
        }
    });
}