Example usage for java.lang.reflect Field getBoolean

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

Introduction

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

Prototype

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

Source Link

Document

Gets the value of a static or instance boolean field.

Usage

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getBoolean(x));

    f.setBoolean(x, false);/*ww  w .java2s.c o m*/
    System.out.println(f.getBoolean(x));

}

From source file:MyClass.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getChar(x));

    f.setChar(x, 'a');
    System.out.println(f.getBoolean(x));

}

From source file:Main.java

public static Activity getCurrentTopActivity()
        throws ClassNotFoundException, IllegalArgumentException, SecurityException, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException, NoSuchFieldException {
    Class<?> activityThreadClass = Class.forName("android.app.ActivityThread");
    Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
    Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
    activitiesField.setAccessible(true);
    Map<?, ?> activities = (Map<?, ?>) activitiesField.get(activityThread);
    for (Object activityRecord : activities.values()) {
        Class<?> activityRecordClass = activityRecord.getClass();
        Field pausedField = activityRecordClass.getDeclaredField("paused");
        pausedField.setAccessible(true);
        if (!pausedField.getBoolean(activityRecord)) {
            Field activityField = activityRecordClass.getDeclaredField("activity");
            activityField.setAccessible(true);
            Activity activity = (Activity) activityField.get(activityRecord);
            return activity;
        }//w  w w . j  av  a2 s.  c  o m
    }
    return null;
}

From source file:api_proto3.TestElf.java

public static boolean getConnectionCommitDirtyState(Connection connection) {
    try {//  w w  w .  j a v  a 2s  .  c  o  m
        Field field = ProxyConnection.class.getDeclaredField("isCommitStateDirty");
        field.setAccessible(true);
        return field.getBoolean(connection);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:szjy.advtech.BuildInfo.java

/**
 * Get boolean of field from Class// www  . j av a 2  s  .  co  m
 * @param c
 * @param fieldName
 * @param defaultReturn
  * @return
  */
private static boolean getClassFieldBoolean(Class c, String fieldName, boolean defaultReturn) {
    boolean ret = defaultReturn;
    Field field = getClassField(c, fieldName);

    if (null != field) {
        try {
            ret = field.getBoolean(c);
        } catch (IllegalAccessException iae) {
            iae.printStackTrace();
        }
    }

    return ret;
}

From source file:Main.java

public static Object getStaticField(Class clz, String fieldName, int type) {
    if (null != clz) {
        try {//from  ww  w .j  ava  2  s  . co m
            Field field = clz.getField(fieldName);
            switch (type) {
            case TYPE_OBJECT:
                return field.get(clz);
            case TYPE_INT:
                return field.getInt(clz);
            case TYPE_SHORT:
                return field.getShort(clz);
            case TYPE_BYTE:
                return field.getByte(clz);
            case TYPE_BOOLEAN:
                return field.getBoolean(clz);
            case TYPE_FLOAT:
                return field.getFloat(clz);
            case TYPE_LONG:
                return field.getLong(clz);
            case TYPE_DOUBLE:
                return field.getDouble(clz);
            default:
                return field.get(clz);
            }
        } catch (Exception e) {
        }
        return (clz == Object.class ? getDefault(type) : getStaticField(clz.getSuperclass(), fieldName, type));
    }
    return getDefault(type);
}

From source file:com.google.dart.tools.update.core.internal.UpdateUtils.java

private static boolean is64bitSWT() throws Exception {
    Class<Library> swtLibraryClass = Library.class;
    Field is64 = swtLibraryClass.getDeclaredField("IS_64");
    is64.setAccessible(true);/*from w  w  w . java2  s .  c  o m*/
    return is64.getBoolean(swtLibraryClass);
}

From source file:org.evosuite.regression.ObjectFields.java

private static Object getFieldValue(Field field, Object p) {
    try {/*w  w  w.  j a v  a2 s.  c  o m*/
        /*Class objClass = p.getClass();
        if(p instanceof java.lang.String){
           ((String) p).hashCode();
        }*/
        Class<?> fieldType = field.getType();
        field.setAccessible(true);
        if (fieldType.isPrimitive()) {
            if (fieldType.equals(Boolean.TYPE)) {
                return field.getBoolean(p);
            }
            if (fieldType.equals(Integer.TYPE)) {
                return field.getInt(p);
            }
            if (fieldType.equals(Byte.TYPE)) {
                return field.getByte(p);
            }
            if (fieldType.equals(Short.TYPE)) {
                return field.getShort(p);
            }
            if (fieldType.equals(Long.TYPE)) {
                return field.getLong(p);
            }
            if (fieldType.equals(Double.TYPE)) {
                return field.getDouble(p);
            }
            if (fieldType.equals(Float.TYPE)) {
                return field.getFloat(p);
            }
            if (fieldType.equals(Character.TYPE)) {
                return field.getChar(p);
            }
            throw new UnsupportedOperationException("Primitive type " + fieldType + " not implemented!");
        }
        return field.get(p);
    } catch (IllegalAccessException exc) {
        throw new RuntimeException(exc);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        if (MAX_RECURSION != 0)
            MAX_RECURSION = 0;
        else
            throw new RuntimeErrorException(e);
        return getFieldValue(field, p);
    }
}

From source file:semRewrite.QAOutputGenerator.java

/************************************************************
 * Generate output from an array of strings
 *//*from www.j ava 2s  .  c om*/
public static List<Record> getRecordFromStringSet(String[] strs, Interpreter inter) {

    ArrayList<Record> res = new ArrayList<Record>();
    Field questionfield = null;
    try {
        questionfield = inter.getClass().getDeclaredField("question");
        questionfield.setAccessible(true);
        Field userInputsfield = inter.getClass().getDeclaredField("userInputs");
        userInputsfield.setAccessible(true);
        Document userInputs = (Document) userInputsfield.get(inter);
        int i = 0;
        for (String s : strs) {
            Record r = new Record(i++, s);
            s = s.trim();
            if (s.endsWith("?"))
                questionfield.setBoolean(inter, true);
            else
                questionfield.setBoolean(inter, false);

            if (!questionfield.getBoolean(inter)) {
                inter.tfidf.addInput(s);
            }
            ArrayList<String> kifClauses;
            try {
                ArrayList<CNF> inputs = Lists.newArrayList(inter.interpretGenCNF(s));
                r.CNF = inputs.get(0);
                kifClauses = inter.interpretCNF(inputs);
            } catch (Exception e) {
                System.out.println("Paring error in sentence :" + s);
                r.CNF = new CNF();
                r.result = "Parsing error";
                res.add(r);
                continue;
            }
            String s1 = inter.toFOL(kifClauses);
            String s2 = inter.postProcess(s1);
            String s3 = inter.addQuantification(s2);
            r.KIF = new Formula(s3).toString();

            String result = inter.fromKIFClauses(kifClauses);
            System.out
                    .println("INFO in Interpreter.interpretSingle(): Theorem proving result: '" + result + "'");

            if (questionfield.getBoolean(inter)) {
                if (("I don't know.".equals(result) && inter.autoir) || inter.ir) {
                    if (inter.autoir) {
                        System.out.println("Interpreter had no response so trying TFIDF");
                    }
                    result = inter.tfidf.matchInput(s).toString();
                }
            } else {
                // Store processed sentence
                userInputs.add(s);
            }

            //System.out.println("INFO in Interpreter.interpretSingle(): combined result: " + result);
            r.result = result;
            res.add(r);
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        System.out.println("IO error");
        return null;
    }

    return res;
}

From source file:Main.java

public static ContentValues objectToContentValues(Object object) {
    if (object == null) {
        throw new IllegalArgumentException("please check your argument");
    }//from www.  j a v  a2 s . com
    ContentValues contentValues = new ContentValues();
    try {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            Type type = field.getType();
            field.setAccessible(true);
            if (type.equals(String.class)) {
                contentValues.put(fieldName, field.get(object).toString());
            } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) {
                contentValues.put(fieldName, field.getInt(object));
            } else if (type.equals(Float.class) || type.equals(Float.TYPE)) {
                contentValues.put(fieldName, field.getFloat(object));
            } else if (type.equals(Long.class) || type.equals(Long.TYPE)) {
                contentValues.put(fieldName, field.getLong(object));
            } else if (type.equals(Double.class) || type.equals(Double.TYPE)) {
                contentValues.put(fieldName, field.getDouble(object));
            } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) {
                contentValues.put(fieldName, field.getBoolean(object));
            }
        }
    } catch (IllegalAccessException | IllegalArgumentException e) {
        e.printStackTrace();
    }
    return contentValues;
}