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:Main.java

public static void mapToAndroidLayoutMapper(Class classObj, Map map, String prefixStr, View view) {

    for (Object keyObj : map.keySet().toArray()) {
        String keyStr = keyObj.toString();
        Field fieldObj = null;
        try {//from   w  ww.  j ava  2 s .c  o m
            fieldObj = classObj.getField(prefixStr + "_" + keyStr);
        } catch (NoSuchFieldException e) {
            continue;
        }
        Object layoutObj = null;
        try {
            layoutObj = fieldObj.get(fieldObj);
        } catch (IllegalAccessException e) {
            continue;
        }
        Integer layoutIntvalue = (Integer) layoutObj;

        View selectedView = view.findViewById(layoutIntvalue);

        if (selectedView instanceof TextView) {
            TextView textView = (TextView) selectedView;
            textView.setText(String.valueOf(map.get(keyStr)));
        } else if (selectedView instanceof ImageView) {
            ImageView imageView = (ImageView) selectedView;

        }

    }

}

From source file:Main.java

public static int getPlatformVersion() {
    try {/*from   ww w.j  ava 2s.  c  o m*/
        Field verField = Class.forName("android.os.Build$VERSION").getField("SDK_INT");
        int ver = verField.getInt(verField);
        return ver;
    } catch (Exception e) {
        try {
            Field verField = Class.forName("android.os.Build$VERSION").getField("SDK");
            String verString = (String) verField.get(verField);
            return Integer.parseInt(verString);
        } catch (Exception ex) {
            return -1;
        }
    }
}

From source file:se.berazy.api.examples.App.java

/**
 * Writes out all public fields.//from  w w  w .j av a 2 s  .  c om
 * @param T response
 */
static <T> void outPutResponse(T response) {
    String retval = "\nResponse: \n";
    Class<?> clazz = response.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (!field.getName().equalsIgnoreCase("ExtensionData")) {
            try {
                retval += String.format("%s: %s\n", field.getName(), field.get(response));
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    System.out.println(retval);
}

From source file:Main.java

public static void mapToAndroidLayoutMapper(Class classObj, Map map, String prefixStr, Activity view) {

    if (map == null) {
        return;//from   w ww.  j  av  a2s .  c  o m
    }

    for (Object keyObj : map.keySet().toArray()) {
        String keyStr = keyObj.toString();
        Field fieldObj = null;
        try {
            fieldObj = classObj.getField(prefixStr + "_" + keyStr);
        } catch (NoSuchFieldException e) {
            continue;
        }
        Object layoutObj = null;
        try {
            layoutObj = fieldObj.get(fieldObj);
        } catch (IllegalAccessException e) {
            continue;
        }
        Integer layoutIntvalue = (Integer) layoutObj;

        View selectedView = view.findViewById(layoutIntvalue);

        if (selectedView instanceof TextView) {
            TextView textView = (TextView) selectedView;
            textView.setText((String) map.get(keyStr));
        } else if (selectedView instanceof ImageView) {
            ImageView imageView = (ImageView) selectedView;

        }

    }

}

From source file:com.palantir.atlasdb.keyvalue.remoting.Utils.java

public static void setupRuleHacks(DropwizardClientRule rule) {
    try {//from  w w  w . j a va  2s  .c  om
        Field field = rule.getClass().getDeclaredField("testSupport");
        field.setAccessible(true);
        @SuppressWarnings("unchecked")
        DropwizardTestSupport<Configuration> testSupport = (DropwizardTestSupport<Configuration>) field
                .get(rule);
        ObjectMapper mapper = testSupport.getEnvironment().getObjectMapper();
        mapper.registerModule(Utils.module);
        mapper.registerModule(new GuavaModule());
        testSupport.getApplication();
    } catch (Exception e) {
        throw Throwables.throwUncheckedException(e);
    }
}

From source file:de.axelfaust.alfresco.nashorn.repo.web.scripts.RepositoryScriptLocation.java

protected static String tryAndGetField(final ScriptContent content, final String fieldName) {
    String result;/*from   w ww .  j  a v a2 s  .c om*/

    try {
        final Field field = content.getClass().getDeclaredField(fieldName);
        if (field.getType().isAssignableFrom(String.class)) {
            field.setAccessible(true);
            result = (String) field.get(content);
        } else {
            result = null;
        }
    } catch (final IllegalAccessException | NoSuchFieldException e) {
        result = null;
    }
    return result;
}

From source file:com.siberhus.ngai.core.CrudHelper.java

@SuppressWarnings("unchecked")
public final static List<Object> findByExample(EntityManager em, Class<?> entityClass, Object example) {
    if (em == null) {
        throw new IllegalArgumentException("EntityManager is null");
    }//from w w w . j a  v  a 2 s  .co m
    EntityInfo entityInfo = getEntityInfo(entityClass);
    List<Object> params = new ArrayList<Object>();
    StringBuilder sql = new StringBuilder(255);
    sql.append("from ").append(entityInfo.getEntityName()).append(" e where 1=1 ");
    try {
        for (Field field : entityInfo.getFieldSet()) {
            sql.append("and e.").append(field.getName()).append("=? ");
            params.add(field.get(example));
        }
    } catch (IllegalAccessException e) {
        throw new NgaiRuntimeException("Unable to get field value from example model: " + example, e);
    }
    Query query = em.createQuery(sql.toString());
    for (int i = 0; i < params.size(); i++) {
        query.setParameter(i + 1, params.get(i));
    }
    return query.getResultList();
}

From source file:Main.java

private static boolean apiLevelIsAtLeastEclair() {
    // we need to at least be running on cupcake for QuickContactBadges.
    boolean isAtLeastEclair = false;
    try {//ww w . ja v  a2  s  .c  o  m
        Field verField = Class.forName("android.os.Build$VERSION").getField("SDK_INT");
        int sdkInt = verField.getInt(verField);
        isAtLeastEclair = (sdkInt >= 5);
    } catch (Exception e) {
        try {
            Field verField = Class.forName("android.os.Build$VERSION").getField("SDK");
            String sdk = (String) verField.get(verField);
            isAtLeastEclair = (Integer.parseInt(sdk) >= 5);
        } catch (Exception e2) {
            isAtLeastEclair = false;
        }
    }
    Log.i("CompatibilityHelp", "api level at least eclair? " + isAtLeastEclair);
    return isAtLeastEclair;
}

From source file:com.mewmew.fairy.v1.cli.AnnotatedCLI.java

private static Unsafe stealUnsafe() {
    try {//from ww w  .  ja v  a 2 s . c  o m
        Class objectStreamClass = Class.forName("java.io.ObjectStreamClass$FieldReflector");
        Field unsafeField = objectStreamClass.getDeclaredField("unsafe");
        unsafeField.setAccessible(true);
        return (Unsafe) unsafeField.get(null);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

From source file:jfix.util.Reflections.java

/**
 * Retrieves all objects of given class-type which are referred by all the
 * objects in given collection.//  ww  w . jav  a2 s.  c  o m
 */
public static Set<Object> getReferredObjects(Collection<?> objects, Class<?> objectClass) {
    try {
        Set<Object> result = new HashSet<>();
        for (Object object : objects) {
            for (Field field : Reflections.getFields(object.getClass())) {
                if (isAssignable(objectClass, field)) {
                    Object fieldValue = field.get(object);
                    if (fieldValue != null) {
                        if (field.getType().isArray()) {
                            for (Object arrayElement : (Object[]) fieldValue) {
                                result.add(arrayElement);
                            }
                        } else {
                            result.add(fieldValue);
                        }
                    }
                }
            }
        }
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}