Example usage for java.lang.reflect Field setInt

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

Introduction

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

Prototype

@CallerSensitive
@ForceInline 
public void setInt(Object obj, int i) throws IllegalArgumentException, IllegalAccessException 

Source Link

Document

Sets the value of a field as an int on the specified object.

Usage

From source file:Main.java

public static void main(String[] args) {
    Rectangle r = new Rectangle(100, 325);
    Class c = r.getClass();//from   ww w .  j ava 2 s.  c o  m
    try {
        Field heightField = c.getField("height");
        heightField.setInt(r, 1000);
        Integer heightValue = (Integer) heightField.get(r);
        System.out.println("Height: " + heightValue.toString());
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:X.java

public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("X");
    X x = (X) clazz.newInstance();//from  w w  w  .  java  2 s  .  c o m
    Field f = clazz.getField("i");
    System.out.println(f.getInt(x)); // Output: 10
    f.setInt(x, 20);
    System.out.println(f.getInt(x)); // Output: 20
    f = clazz.getField("PI");
    System.out.println(f.getDouble(null)); // Output: 3.14
    f.setDouble(x, 20);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class cls = java.awt.Point.class;

    Field field = cls.getField("x");

    // Get value//w  w w .  j  a va  2  s .com
    //    field.getInt(object);

    // Set value
    //  field.setInt(object, 123);

    // Get value of a static field
    field.getInt(null);

    // Set value of a static field
    field.setInt(null, 123);

}

From source file:de.vanita5.twittnuker.util.accessor.ViewDragHelperAccessor.java

public static boolean setEdgeSize(final ViewDragHelper helper, final int edgeSize) {
    try {//from ww  w.  j a v a 2 s . c  o  m
        final Field f = helper.getClass().getField("mEdgeSize");
        f.setAccessible(true);
        f.setInt(helper, edgeSize);
        return true;
    } catch (final Exception e) {
        return false;
    }
}

From source file:Main.java

public static void keep_setInt(Field field, Object obj, Cursor cursor, int i) {
    try {//  w w w . j  a  v a  2 s .  co m
        if (field.getType().equals(Integer.TYPE))
            field.setInt(obj, cursor.getInt(i));
        else
            field.set(obj, Integer.valueOf(cursor.getInt(i)));
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

From source file:Main.java

/**
 * Creates a SyncFlush Deflater for use on pre-KitKat Android
 *
 * @return The modified Deflater, or null if the creation failed
 *///from  w w w . j  a  v a2 s  . c om
@Nullable
private static Deflater createSyncFlushDeflater() {
    Deflater def = new Deflater();
    try {
        Field f = def.getClass().getDeclaredField("flushParm");
        f.setAccessible(true);
        f.setInt(def, 2); // Z_SYNC_FLUSH
    } catch (Exception e) {
        return null;
    }
    return def;
}

From source file:Main.java

public static void setProgressBarAnimationDuration(ProgressBar progressBar, int duration) {
    if (progressBar == null) {
        return;/*from ww w  . j  av a2 s  .c  o m*/
    }
    try {
        Field mCursorDrawableRes = ProgressBar.class.getDeclaredField("mDuration");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.setInt(progressBar, duration);
    } catch (Exception e) {
        //            FileLog.e("tmessages", e);
    }
}

From source file:Main.java

public static void clearCursorDrawable(EditText editText) {
    if (editText == null || Build.VERSION.SDK_INT < 12) {
        return;/*from  w  ww .  j  av  a2 s. c  o  m*/
    }
    try {
        Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.setInt(editText, 0);
    } catch (Exception e) {
        //            FileLog.e("tmessages", e);
    }
}

From source file:testinfrastructure.testutils.LogInjector.java

private static void setFinalStaticField(Field field, Object newValue) {
    try {//ww w .ja  va  2 s .c o m
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(null, newValue);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.atlas.repository.graph.TitanGraphProvider.java

/**
 * Titan loads index backend name to implementation using StandardIndexProvider.ALL_MANAGER_CLASSES
 * But StandardIndexProvider.ALL_MANAGER_CLASSES is a private static final ImmutableMap
 * Only way to inject Solr5Index is to modify this field. So, using hacky reflection to add Sol5Index
 *///from   w  ww.j a v a  2 s. c  o  m
private static void addSolr5Index() {
    try {
        Field field = StandardIndexProvider.class.getDeclaredField("ALL_MANAGER_CLASSES");
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        Map<String, String> customMap = new HashMap(StandardIndexProvider.getAllProviderClasses());
        customMap.put("solr5", Solr5Index.class.getName());
        ImmutableMap<String, String> immap = ImmutableMap.copyOf(customMap);
        field.set(null, immap);

        LOG.debug("Injected solr5 index - {}", Solr5Index.class.getName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}