Example usage for java.lang.reflect Field setBoolean

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public void setBoolean(Object obj, boolean z) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Sets the value of a field as a boolean on the specified object.

Usage

From source file:semRewrite.QAOutputGenerator.java

/************************************************************
 * Generate output from an array of strings
 *//*from  ww  w . j a  v a 2s .c o  m*/
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:org.sonar.api.batch.rule.Checks.java

private static void configureField(Object check, Field field, String value) {
    try {/*  w  w  w.j  a  v  a2  s. c  om*/
        field.setAccessible(true);

        if (field.getType().equals(String.class)) {
            field.set(check, value);

        } else if (int.class == field.getType()) {
            field.setInt(check, Integer.parseInt(value));

        } else if (short.class == field.getType()) {
            field.setShort(check, Short.parseShort(value));

        } else if (long.class == field.getType()) {
            field.setLong(check, Long.parseLong(value));

        } else if (double.class == field.getType()) {
            field.setDouble(check, Double.parseDouble(value));

        } else if (boolean.class == field.getType()) {
            field.setBoolean(check, Boolean.parseBoolean(value));

        } else if (byte.class == field.getType()) {
            field.setByte(check, Byte.parseByte(value));

        } else if (Integer.class == field.getType()) {
            field.set(check, Integer.parseInt(value));

        } else if (Long.class == field.getType()) {
            field.set(check, Long.parseLong(value));

        } else if (Double.class == field.getType()) {
            field.set(check, Double.parseDouble(value));

        } else if (Boolean.class == field.getType()) {
            field.set(check, Boolean.parseBoolean(value));

        } else {
            throw new SonarException(
                    "The type of the field " + field + " is not supported: " + field.getType());
        }
    } catch (IllegalAccessException e) {
        throw new SonarException(
                "Can not set the value of the field " + field + " in the class: " + check.getClass().getName(),
                e);
    }
}

From source file:org.sonar.plugins.openedge.foundation.OpenEdgeComponents.java

private static void configureField(Object check, Field field, String value) {
    try {/*w  ww .j av  a 2  s  .  c om*/
        field.setAccessible(true);

        if (field.getType().equals(String.class)) {
            field.set(check, value);
        } else if (int.class == field.getType()) {
            field.setInt(check, Integer.parseInt(value));
        } else if (short.class == field.getType()) {
            field.setShort(check, Short.parseShort(value));
        } else if (long.class == field.getType()) {
            field.setLong(check, Long.parseLong(value));
        } else if (double.class == field.getType()) {
            field.setDouble(check, Double.parseDouble(value));
        } else if (boolean.class == field.getType()) {
            field.setBoolean(check, Boolean.parseBoolean(value));
        } else if (byte.class == field.getType()) {
            field.setByte(check, Byte.parseByte(value));
        } else if (Integer.class == field.getType()) {
            field.set(check, new Integer(Integer.parseInt(value)));
        } else if (Long.class == field.getType()) {
            field.set(check, new Long(Long.parseLong(value)));
        } else if (Double.class == field.getType()) {
            field.set(check, new Double(Double.parseDouble(value)));
        } else if (Boolean.class == field.getType()) {
            field.set(check, Boolean.valueOf(Boolean.parseBoolean(value)));
        } else {
            throw MessageException
                    .of("The type of the field " + field + " is not supported: " + field.getType());
        }
    } catch (IllegalAccessException e) {
        throw MessageException.of(
                "Can not set the value of the field " + field + " in the class: " + check.getClass().getName(),
                e);
    }
}

From source file:com.gigaspaces.internal.utils.ClassLoaderCleaner.java

private static void clearReferencesStopTimerThread(Thread thread) {

    // Need to get references to:
    // - newTasksMayBeScheduled field
    // - queue field
    // - queue.clear()

    try {/*from w  ww .ja v a  2s  .c  o m*/
        Field newTasksMayBeScheduledField = thread.getClass().getDeclaredField("newTasksMayBeScheduled");
        newTasksMayBeScheduledField.setAccessible(true);
        Field queueField = thread.getClass().getDeclaredField("queue");
        queueField.setAccessible(true);

        Object queue = queueField.get(thread);

        Method clearMethod = queue.getClass().getDeclaredMethod("clear");
        clearMethod.setAccessible(true);

        synchronized (queue) {
            newTasksMayBeScheduledField.setBoolean(thread, false);
            clearMethod.invoke(queue);
            queue.notify(); // In case queue was already empty.
        }

        if (logger.isLoggable(Level.FINE))
            logger.fine("A web application appears to have started a TimerThread named [" + thread.getName()
                    + "] via the java.util.Timer API but has failed to stop it. To prevent a memory leak, the timer (and hence the associated thread) has been forcibly cancelled.");

    } catch (Exception e) {
        logger.log(Level.WARNING, "Failed to terminate TimerThread named [" + thread.getName() + "]", e);
    }
}

From source file:android.reflect.ClazzLoader.java

/**
 * ????/*from w  w w  .j  a  va 2 s  . co m*/
 */
public static <O, V> void setFieldValue(O o, Field field, V v) {
    if (field != null) {
        try {
            field.setAccessible(true);

            if (v == null) {
                field.set(o, null);
            } else {
                Class<?> vType = v.getClass();
                if (vType == Integer.TYPE) {
                    field.setInt(o, (Integer) v);
                } else if (vType == Long.TYPE) {
                    field.setLong(o, (Long) v);
                } else if (vType == Boolean.TYPE) {
                    field.setBoolean(o, (Boolean) v);
                } else if (vType == Float.TYPE) {
                    field.setFloat(o, (Float) v);
                } else if (vType == Short.TYPE) {
                    field.setShort(o, (Short) v);
                } else if (vType == Byte.TYPE) {
                    field.setByte(o, (Byte) v);
                } else if (vType == Double.TYPE) {
                    field.setDouble(o, (Double) v);
                } else if (vType == Character.TYPE) {
                    field.setChar(o, (Character) v);
                } else {
                    field.set(o, v);
                }
            }
        } catch (Throwable t) {
            Log.e(TAG, t);
        }
    }
}

From source file:org.apache.tika.server.resource.TikaResource.java

/**
 * Utility method to set a property on a class via reflection.
 *
 * @param httpHeaders the HTTP headers set.
 * @param object      the <code>Object</code> to set the property on.
 * @param key         the key of the HTTP Header.
 * @param prefix      the name of the HTTP Header prefix used to find property.
 * @throws WebApplicationException thrown when field cannot be found.
 *///  w  w w  .  jav  a  2 s.c o  m
private static void processHeaderConfig(MultivaluedMap<String, String> httpHeaders, Object object, String key,
        String prefix) {
    try {
        String property = StringUtils.removeStart(key, prefix);
        Field field = object.getClass().getDeclaredField(StringUtils.uncapitalize(property));

        field.setAccessible(true);
        if (field.getType() == String.class) {
            field.set(object, httpHeaders.getFirst(key));
        } else if (field.getType() == int.class) {
            field.setInt(object, Integer.parseInt(httpHeaders.getFirst(key)));
        } else if (field.getType() == double.class) {
            field.setDouble(object, Double.parseDouble(httpHeaders.getFirst(key)));
        } else if (field.getType() == boolean.class) {
            field.setBoolean(object, Boolean.parseBoolean(httpHeaders.getFirst(key)));
        } else {
            //couldn't find a directly accessible field
            //try for setX(String s)
            String setter = StringUtils.uncapitalize(property);
            setter = "set" + setter.substring(0, 1).toUpperCase(Locale.US) + setter.substring(1);
            Method m = null;
            try {
                m = object.getClass().getMethod(setter, String.class);
            } catch (NoSuchMethodException e) {
                //swallow
            }
            if (m != null) {
                m.invoke(object, httpHeaders.getFirst(key));
            }
        }
    } catch (Throwable ex) {
        throw new WebApplicationException(
                String.format(Locale.ROOT, "%s is an invalid %s header", key, X_TIKA_OCR_HEADER_PREFIX));
    }
}

From source file:org.apache.openjpa.enhance.Reflection.java

/**
 * Set the value of the given field in the given object.
 *//* w w w. ja v  a2 s . c o m*/
public static void set(Object target, Field field, boolean value) {
    if (target == null || field == null)
        return;
    makeAccessible(field, field.getModifiers());
    try {
        field.setBoolean(target, value);
    } catch (Throwable t) {
        throw wrapReflectionException(t,
                _loc.get("set-field", new Object[] { target, field, value, "boolean" }));
    }
}

From source file:com.fjn.helper.common.io.file.common.FileUpAndDownloadUtil.java

/**
 * ???????/* w ww . j  a v a  2s  .  co m*/
 * @param klass   ???klass?Class
 * @param filepath   ?
 * @param sizeThreshold   ??
 * @param isFileNameBaseTime   ????
 * @return bean
 */
public static <T> Object upload(HttpServletRequest request, Class<T> klass, String filepath, int sizeThreshold,
        boolean isFileNameBaseTime) throws Exception {
    FileItemFactory fileItemFactory = null;
    if (sizeThreshold > 0) {
        File repository = new File(filepath);
        fileItemFactory = new DiskFileItemFactory(sizeThreshold, repository);
    } else {
        fileItemFactory = new DiskFileItemFactory();
    }
    ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
    ServletRequestContext requestContext = new ServletRequestContext(request);
    T bean = null;
    if (klass != null) {
        bean = klass.newInstance();
    }
    // 
    if (ServletFileUpload.isMultipartContent(requestContext)) {
        File parentDir = new File(filepath);

        List<FileItem> fileItemList = upload.parseRequest(requestContext);
        for (int i = 0; i < fileItemList.size(); i++) {
            FileItem item = fileItemList.get(i);
            // ??
            if (item.isFormField()) {
                String paramName = item.getFieldName();
                String paramValue = item.getString("UTF-8");
                log.info("?" + paramName + "=" + paramValue);
                request.setAttribute(paramName, paramValue);
                // ?bean
                if (klass != null) {
                    Field field = null;
                    try {
                        field = klass.getDeclaredField(paramName);

                        if (field != null) {
                            field.setAccessible(true);
                            Class type = field.getType();
                            if (type == Integer.TYPE) {
                                field.setInt(bean, Integer.valueOf(paramValue));
                            } else if (type == Double.TYPE) {
                                field.setDouble(bean, Double.valueOf(paramValue));
                            } else if (type == Float.TYPE) {
                                field.setFloat(bean, Float.valueOf(paramValue));
                            } else if (type == Boolean.TYPE) {
                                field.setBoolean(bean, Boolean.valueOf(paramValue));
                            } else if (type == Character.TYPE) {
                                field.setChar(bean, paramValue.charAt(0));
                            } else if (type == Long.TYPE) {
                                field.setLong(bean, Long.valueOf(paramValue));
                            } else if (type == Short.TYPE) {
                                field.setShort(bean, Short.valueOf(paramValue));
                            } else {
                                field.set(bean, paramValue);
                            }
                        }
                    } catch (NoSuchFieldException e) {
                        log.info("" + klass.getName() + "?" + paramName);
                    }
                }
            }
            // 
            else {
                // <input type='file' name='xxx'> xxx
                String paramName = item.getFieldName();
                log.info("?" + item.getSize());

                if (sizeThreshold > 0) {
                    if (item.getSize() > sizeThreshold)
                        continue;
                }
                String clientFileName = item.getName();
                int index = -1;
                // ?IE?
                if ((index = clientFileName.lastIndexOf("\\")) != -1) {
                    clientFileName = clientFileName.substring(index + 1);
                }
                if (clientFileName == null || "".equals(clientFileName))
                    continue;

                String filename = null;
                log.info("" + paramName + "\t??" + clientFileName);
                if (isFileNameBaseTime) {
                    filename = buildFileName(clientFileName);
                } else
                    filename = clientFileName;

                request.setAttribute(paramName, filename);

                // ?bean
                if (klass != null) {
                    Field field = null;
                    try {
                        field = klass.getDeclaredField(paramName);
                        if (field != null) {
                            field.setAccessible(true);
                            field.set(bean, filename);
                        }
                    } catch (NoSuchFieldException e) {
                        log.info("" + klass.getName() + "?  " + paramName);
                        continue;
                    }
                }

                if (!parentDir.exists()) {
                    parentDir.mkdirs();
                }

                File newfile = new File(parentDir, filename);
                item.write(newfile);
                String serverPath = newfile.getPath();
                log.info("?" + serverPath);
            }
        }
    }
    return bean;
}

From source file:org.acoveo.tools.Reflection.java

/**
 * Set the value of the given field in the given object.
 *//*from w  ww.j  a v  a  2  s .c om*/
public static void set(Object target, Field field, boolean value) {
    if (target == null || field == null)
        return;
    makeAccessible(field, field.getModifiers());
    try {
        field.setBoolean(target, value);
    } catch (Throwable t) {
        throw wrapReflectionException(t);
    }
}

From source file:org.dkf.jmule.MainApplication.java

private void ignoreHardwareMenu() {
    try {//  w  w  w .j  a va  2  s. co m
        ViewConfiguration config = ViewConfiguration.get(this);
        Field f = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if (f != null) {
            f.setAccessible(true);
            f.setBoolean(config, false);
        }
    } catch (Exception e) {
        // ignore
    }
}