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:com.freecast.LudoCast.MainActivity.java

private void getOverflowMenu() {
    try {/*from  ww w.j  a  v a2s  . c o  m*/
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.syncany.plugins.transfer.TransferSettings.java

/**
 * Set the value of a field in the settings class.
 *
 * @param key The field name as it is used in the {@link TransferSettings}
 * @param value The object which should be the setting's value. The object's type must match the field type.
 *              {@link Integer}, {@link String}, {@link Boolean}, {@link File} and implementation of
 *              {@link TransferSettings} are converted.
 * @throws StorageException Thrown if the field either does not exist or isn't accessible or
 *         conversion failed due to invalid field types.
 *///from  ww  w .ja  va 2s.  c  o  m
@SuppressWarnings({ "rawtypes", "unchecked" })
public final void setField(String key, Object value) throws StorageException {
    try {
        Field[] elementFields = ReflectionUtil.getAllFieldsWithAnnotation(this.getClass(), Element.class);

        for (Field field : elementFields) {
            field.setAccessible(true);

            String fieldName = field.getName();
            Type fieldType = field.getType();

            if (key.equalsIgnoreCase(fieldName)) {
                if (value == null) {
                    field.set(this, null);
                } else if (fieldType == Integer.TYPE && (value instanceof Integer || value instanceof String)) {
                    field.setInt(this, Integer.parseInt(String.valueOf(value)));
                } else if (fieldType == Boolean.TYPE && (value instanceof Boolean || value instanceof String)) {
                    field.setBoolean(this, Boolean.parseBoolean(String.valueOf(value)));
                } else if (fieldType == String.class && value instanceof String) {
                    field.set(this, value);
                } else if (fieldType == File.class && value instanceof String) {
                    field.set(this, new File(String.valueOf(value)));
                } else if (ReflectionUtil.getClassFromType(fieldType).isEnum() && value instanceof String) {
                    Class<? extends Enum> enumClass = (Class<? extends Enum>) ReflectionUtil
                            .getClassFromType(fieldType);
                    String enumValue = String.valueOf(value).toUpperCase();

                    Enum translatedEnum = Enum.valueOf(enumClass, enumValue);
                    field.set(this, translatedEnum);
                } else if (TransferSettings.class.isAssignableFrom(value.getClass())) {
                    field.set(this, ReflectionUtil.getClassFromType(fieldType).cast(value));
                } else {
                    throw new RuntimeException("Invalid value type: " + value.getClass());
                }
            }
        }
    } catch (Exception e) {
        throw new StorageException("Unable to parse value because its format is invalid: " + e.getMessage(), e);
    }
}

From source file:com.cttapp.bby.mytlc.layer8apps.MyTlc.java

/************
 *  PURPOSE: Primary thread that starts everything
 *  ARGUMENTS: <Bundle> savedInstanceState
 *  RETURNS: void/* w w w.ja  v a  2 s.  c om*/
 *  AUTHOR: Casey Stark <starkca90@gmail.com>, Devin Collins <agent14709@gmail.com>, Bobby Ore <bob1987@gmail.com>
 ************/
@Override
public void onCreate(Bundle savedInstanceState) {

    /*************
     * The following try statement makes the app think that the user doesn't
     * have a hard settings button.  This allows the options menu to always be
     * visible
     ************/

    currentMyTLCActivity = this;

    try {
        // Get the configuration of the device
        ViewConfiguration config = ViewConfiguration.get(this);
        // Check if the phone has a hard settings key
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        // If it does...
        if (menuKeyField != null) {
            // Make our app think it doesn't!
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {
        // Do nothing
    }

    // Create a new preferences manager
    pf = new Preferences(this);
    // Set the theme of the app
    int logo = applyTheme();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView imgLogo = (ImageView) findViewById(R.id.logo);

    imgLogo.setImageDrawable(getResources().getDrawable(logo));

    txtUsername = (EditText) findViewById(R.id.txtUsername);
    txtUsername.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    txtPassword = (EditText) findViewById(R.id.txtPassword);
    //        remember = (CheckBox) findViewById(R.id.remember);
    results = (TextView) findViewById(R.id.results);

    // Get a reference to our last handler
    final Object handler = getLastCustomNonConfigurationInstance();
    // If the last handler existed
    if (handler != null) {
        // Cast the handler to MyHandler so we can use it
        mHandler = (MyHandler) handler;
    } else {
        // Otherwise create a new handler
        mHandler = new MyHandler();
    }
    // Assign the activity for our handler so it knows the proper reference
    mHandler.setActivity(this);

    // Load all of the users saved options
    checkSavedSettings();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    System.out.println("Saved Shifts:\n" + pf.getJSONString());

    loginButton = (Button) findViewById(R.id.loginButton);
    loginButton.setOnClickListener(this);

    // Show the ads
    //showAds();
}

From source file:org.openqa.selenium.server.SeleniumDriverResourceHandler.java

/**
 * Perl and Ruby hang forever when they see "Connection: close" in the HTTP headers. They see that
 * and they think that Jetty will close the socket connection, but Jetty doesn't appear to do that
 * reliably when we're creating a process while handling the HTTP response! So, removing the
 * "Connection: close" header so that Perl and Ruby think we're morons and hang up on us in
 * disgust.//  ww  w. j a va 2 s. c  o m
 *
 * @param res the HTTP response
 */
private void hackRemoveConnectionCloseHeader(HttpResponse res) {
    // First, if Connection has been added, remove it.
    res.removeField(HttpFields.__Connection);
    // Now, claim that this connection is *actually* persistent
    Field[] fields = HttpConnection.class.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        if (fields[i].getName().equals("_close")) {
            Field _close = fields[i];
            _close.setAccessible(true);
            try {
                _close.setBoolean(res.getHttpConnection(), false);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        if (fields[i].getName().equals("_persistent")) {
            Field _close = fields[i];
            _close.setAccessible(true);
            try {
                _close.setBoolean(res.getHttpConnection(), true);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.sonar.api.checks.AnnotationCheckFactory.java

private void configureField(Object check, Field field, String value) {
    try {//from www  . jav  a 2 s.com
        field.setAccessible(true);

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

        } else if ("int".equals(field.getType().getSimpleName())) {
            field.setInt(check, Integer.parseInt(value));

        } else if ("short".equals(field.getType().getSimpleName())) {
            field.setShort(check, Short.parseShort(value));

        } else if ("long".equals(field.getType().getSimpleName())) {
            field.setLong(check, Long.parseLong(value));

        } else if ("double".equals(field.getType().getSimpleName())) {
            field.setDouble(check, Double.parseDouble(value));

        } else if ("boolean".equals(field.getType().getSimpleName())) {
            field.setBoolean(check, Boolean.parseBoolean(value));

        } else if ("byte".equals(field.getType().getSimpleName())) {
            field.setByte(check, Byte.parseByte(value));

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

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

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

        } else if (field.getType().equals(Boolean.class)) {
            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.api.checks.checkers.AnnotationCheckerFactory.java

private void configureField(Object checker, Field field, Map.Entry<String, String> parameter)
        throws IllegalAccessException {
    field.setAccessible(true);// w w w.j  av a 2s.  com

    if (field.getType().equals(String.class)) {
        field.set(checker, parameter.getValue());

    } else if (field.getType().getSimpleName().equals("int")) {
        field.setInt(checker, Integer.parseInt(parameter.getValue()));

    } else if (field.getType().getSimpleName().equals("short")) {
        field.setShort(checker, Short.parseShort(parameter.getValue()));

    } else if (field.getType().getSimpleName().equals("long")) {
        field.setLong(checker, Long.parseLong(parameter.getValue()));

    } else if (field.getType().getSimpleName().equals("double")) {
        field.setDouble(checker, Double.parseDouble(parameter.getValue()));

    } else if (field.getType().getSimpleName().equals("boolean")) {
        field.setBoolean(checker, Boolean.parseBoolean(parameter.getValue()));

    } else if (field.getType().getSimpleName().equals("byte")) {
        field.setByte(checker, Byte.parseByte(parameter.getValue()));

    } else if (field.getType().equals(Integer.class)) {
        field.set(checker, new Integer(Integer.parseInt(parameter.getValue())));

    } else if (field.getType().equals(Long.class)) {
        field.set(checker, new Long(Long.parseLong(parameter.getValue())));

    } else if (field.getType().equals(Double.class)) {
        field.set(checker, new Double(Double.parseDouble(parameter.getValue())));

    } else if (field.getType().equals(Boolean.class)) {
        field.set(checker, Boolean.valueOf(Boolean.parseBoolean(parameter.getValue())));

    } else {
        throw new UnvalidCheckerException(
                "The type of the field " + field + " is not supported: " + field.getType());
    }
}

From source file:com.sikulix.core.SX.java

private static void setOptions(PropertiesConfiguration someOptions) {
    if (isNull(someOptions) || someOptions.size() == 0) {
        return;//from  w  w w. j a v  a2s.co  m
    }
    Iterator<String> allKeys = someOptions.getKeys();
    List<String> sxSettings = new ArrayList<>();
    while (allKeys.hasNext()) {
        String key = allKeys.next();
        if (key.startsWith("Settings.")) {
            sxSettings.add(key);
            continue;
        }
        trace("!setOptions: %s = %s", key, someOptions.getProperty(key));
    }
    if (sxSettings.size() > 0) {
        Class cClass = null;
        try {
            cClass = Class.forName("org.sikuli.basics.Settings");
        } catch (ClassNotFoundException e) {
            error("!setOptions: %s", cClass);
        }
        if (!isNull(cClass)) {
            for (String sKey : sxSettings) {
                String sAttr = sKey.substring("Settings.".length());
                Field cField = null;
                Class ccField = null;
                try {
                    cField = cClass.getField(sAttr);
                    ccField = cField.getType();
                    if (ccField.getName() == "boolean") {
                        cField.setBoolean(null, someOptions.getBoolean(sKey));
                    } else if (ccField.getName() == "int") {
                        cField.setInt(null, someOptions.getInt(sKey));
                    } else if (ccField.getName() == "float") {
                        cField.setFloat(null, someOptions.getFloat(sKey));
                    } else if (ccField.getName() == "double") {
                        cField.setDouble(null, someOptions.getDouble(sKey));
                    } else if (ccField.getName() == "String") {
                        cField.set(null, someOptions.getString(sKey));
                    }
                    trace("!setOptions: %s = %s", sAttr, someOptions.getProperty(sKey));
                    someOptions.clearProperty(sKey);
                } catch (Exception ex) {
                    error("!setOptions: %s = %s", sKey, sxOptions.getProperty(sKey));
                }
            }
        }
    }
}

From source file:lineage2.gameserver.Config.java

/**
 * Method setField.//  w w  w .  j a va 2 s .  c o m
 * @param fieldName String
 * @param value String
 * @return boolean
 */
public static boolean setField(String fieldName, String value) {
    Field field = FieldUtils.getField(Config.class, fieldName);
    if (field == null) {
        return false;
    }
    try {
        if (field.getType() == boolean.class) {
            field.setBoolean(null, BooleanUtils.toBoolean(value));
        } else if (field.getType() == int.class) {
            field.setInt(null, NumberUtils.toInt(value));
        } else if (field.getType() == long.class) {
            field.setLong(null, NumberUtils.toLong(value));
        } else if (field.getType() == double.class) {
            field.setDouble(null, NumberUtils.toDouble(value));
        } else if (field.getType() == String.class) {
            field.set(null, value);
        } else {
            return false;
        }
    } catch (IllegalArgumentException e) {
        return false;
    } catch (IllegalAccessException e) {
        return false;
    }
    return true;
}

From source file:com.klinker.android.twitter.activities.tweet_viewer.TweetPager.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    overridePendingTransition(R.anim.activity_slide_up, R.anim.activity_slide_down);

    try {//from  w w w. j a va2s . co  m
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
    } catch (Exception e) {

    }

    context = this;
    settings = AppSettings.getInstance(this);

    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {
        // Ignore
    }

    getFromIntent();

    mSectionsPagerAdapter = new TweetPagerAdapter(getFragmentManager(), context, name, screenName, tweet, time,
            retweeter, webpage, proPic, tweetId, picture, users, hashtags, otherLinks, isMyTweet, isMyRetweet,
            secondAcc, animatedGif);

    // methods for advancing windowed
    boolean settingsVal = settings.advanceWindowed;
    boolean fromWidget = getIntent().getBooleanExtra("from_widget", false);
    final boolean youtube;

    youtube = mSectionsPagerAdapter.getHasYoutube() || mSectionsPagerAdapter.getHasGif()
            || mSectionsPagerAdapter.hasVine();

    if (fromWidget || settingsVal) {
        setUpWindow(youtube);
    }

    setUpTheme();

    int currentOrientation = getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }

    if (getIntent().getBooleanExtra("clicked_youtube", false)) {
        IntentFilter i = new IntentFilter("com.klinker.android.twitter.YOUTUBE_READY");
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                TweetYouTubeFragment.resume();

                context.unregisterReceiver(this);
            }
        }, i);
    }

    setContentView(R.layout.tweet_pager);
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(mSectionsPagerAdapter);
    pager.setOffscreenPageLimit(5);

    final int numberOfPages = mSectionsPagerAdapter.getCount();

    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i2) {

        }

        @Override
        public void onPageSelected(int i) {
            if (youtube) {
                try {
                    switch (numberOfPages) {
                    case 3:
                    case 4:
                        if (i != 0) {
                            TweetYouTubeFragment.pause();
                        } else {
                            TweetYouTubeFragment.resume();
                        }
                        break;
                    case 5:
                        if (i != 1) {
                            TweetYouTubeFragment.pause();
                        } else {
                            TweetYouTubeFragment.resume();
                        }
                        break;
                    }
                } catch (Exception e) {

                }
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });

    switch (numberOfPages) {
    case 2:
        if (settings.pageToOpen == AppSettings.PAGE_CONVO) {
            pager.setCurrentItem(1);
        } else {
            pager.setCurrentItem(0);
        }
        break;
    case 3:
        if (mSectionsPagerAdapter.getHasWebpage()) {
            switch (settings.pageToOpen) {
            case AppSettings.PAGE_CONVO:
                pager.setCurrentItem(2);
                break;
            case AppSettings.PAGE_WEB:
                pager.setCurrentItem(0);
                break;
            default:
                pager.setCurrentItem(1);
                break;
            }
        } else { // no web page
            switch (settings.pageToOpen) {
            case AppSettings.PAGE_CONVO:
                pager.setCurrentItem(2);
                break;
            default:
                pager.setCurrentItem(1);
                break;
            }
        }
        break;
    case 4: // webpage and youtube
        switch (settings.pageToOpen) {
        case AppSettings.PAGE_CONVO:
            pager.setCurrentItem(3);
            break;
        case AppSettings.PAGE_WEB:
            pager.setCurrentItem(1);
            break;
        default:
            pager.setCurrentItem(0);
            break;
        }
        break;
    }

    if (getIntent().getBooleanExtra("clicked_youtube", false)) {
        pager.setCurrentItem(0);
    }

    if (settings.addonTheme) {
        PagerTitleStrip strip = (PagerTitleStrip) findViewById(R.id.pager_title_strip);
        strip.setBackgroundColor(settings.pagerTitleInt);

        if (!settings.showTitleStrip) {
            strip.setVisibility(View.GONE);
        }
    }

}

From source file:com.klinker.android.twitter.ui.tweet_viewer.TweetPager.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    overridePendingTransition(R.anim.activity_slide_up, R.anim.activity_slide_down);

    try {// w  w w . j  a v  a 2s. com
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
    } catch (Exception e) {

    }

    context = this;
    settings = AppSettings.getInstance(this);

    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {
        // Ignore
    }

    getFromIntent();

    // methods for advancing windowed
    boolean settingsVal = settings.advanceWindowed;
    boolean fromWidget = getIntent().getBooleanExtra("from_widget", false);
    final boolean youtube;
    if (webpage != null && linkString != null) {
        youtube = webpage.contains("youtu") || linkString.contains("youtu");
    } else {
        youtube = true;
    }

    // cases: (youtube will ALWAYS be full screen...)
    // from widget
    // the user set the preference to advance windowed
    // has a webview and want to advance windowed
    if (fromWidget || settingsVal) {
        setUpWindow(youtube);
    }

    setUpTheme();

    int currentOrientation = getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }

    if (getIntent().getBooleanExtra("clicked_youtube", false)) {
        IntentFilter i = new IntentFilter("com.klinker.android.twitter.YOUTUBE_READY");
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                TweetYouTubeFragment.resume();

                context.unregisterReceiver(this);
            }
        }, i);
    }

    setContentView(R.layout.tweet_pager);
    pager = (ViewPager) findViewById(R.id.pager);
    mSectionsPagerAdapter = new TweetPagerAdapter(getFragmentManager(), context, name, screenName, tweet, time,
            retweeter, webpage, proPic, tweetId, picture, users, hashtags, otherLinks, isMyTweet, isMyRetweet);
    pager.setAdapter(mSectionsPagerAdapter);
    pager.setOffscreenPageLimit(5);

    final int numberOfPages = mSectionsPagerAdapter.getCount();

    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i2) {

        }

        @Override
        public void onPageSelected(int i) {
            if (youtube) {
                try {
                    switch (numberOfPages) {
                    case 3:
                    case 4:
                        if (i != 0) {
                            TweetYouTubeFragment.pause();
                        } else {
                            TweetYouTubeFragment.resume();
                        }
                        break;
                    case 5:
                        if (i != 1) {
                            TweetYouTubeFragment.pause();
                        } else {
                            TweetYouTubeFragment.resume();
                        }
                        break;
                    }
                } catch (Exception e) {

                }
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });

    switch (numberOfPages) {
    case 2:
        if (settings.pageToOpen == AppSettings.PAGE_CONVO) {
            pager.setCurrentItem(1);
        } else {
            pager.setCurrentItem(0);
        }
        break;
    case 3:
        if (mSectionsPagerAdapter.getHasWebpage()) {
            switch (settings.pageToOpen) {
            case AppSettings.PAGE_CONVO:
                pager.setCurrentItem(2);
                break;
            case AppSettings.PAGE_WEB:
                pager.setCurrentItem(0);
                break;
            default:
                pager.setCurrentItem(1);
                break;
            }
        } else { // no web page
            switch (settings.pageToOpen) {
            case AppSettings.PAGE_CONVO:
                pager.setCurrentItem(2);
                break;
            default:
                pager.setCurrentItem(1);
                break;
            }
        }
        break;
    case 4: // webpage and youtube
        switch (settings.pageToOpen) {
        case AppSettings.PAGE_CONVO:
            pager.setCurrentItem(3);
            break;
        case AppSettings.PAGE_WEB:
            pager.setCurrentItem(1);
            break;
        default:
            pager.setCurrentItem(0);
            break;
        }
        break;
    }

    if (getIntent().getBooleanExtra("clicked_youtube", false)) {
        pager.setCurrentItem(0);
    }

    if (settings.addonTheme) {
        PagerTitleStrip strip = (PagerTitleStrip) findViewById(R.id.pager_title_strip);
        strip.setBackgroundColor(settings.pagerTitleInt);

        if (!settings.showTitleStrip) {
            strip.setVisibility(View.GONE);
        }
    }

}