Example usage for java.lang.reflect Field setAccessible

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

Introduction

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

Prototype

@Override
@CallerSensitive
public void setAccessible(boolean flag) 

Source Link

Usage

From source file:Main.java

public static int hashCode(Object paramObject) {
    int i = 1;//from w w w. j  a  va2  s  . c o m
    Field[] arrayOfField = paramObject.getClass().getDeclaredFields();
    int j = arrayOfField.length;
    for (int k = 0;; k++) {
        int m;
        int i1;
        if (k < j) {
            Field localField = arrayOfField[k];
            localField.setAccessible(true);
            try {
                Object localObject = localField.get(paramObject);
                m = i * 31;
                if (localObject == null) {
                    i1 = 0;
                } else {
                    int n = localObject.hashCode();
                    i1 = n;
                }
            } catch (IllegalArgumentException localIllegalArgumentException) {
                localIllegalArgumentException.printStackTrace();
                continue;
            } catch (IllegalAccessException localIllegalAccessException) {
                localIllegalAccessException.printStackTrace();
                continue;
            }
        } else {
            return i;
        }
        i = m + i1;
    }
}

From source file:Main.java

private static int getImageViewFieldValue(Object object, String fieldName) {
    int value = 0;
    if (object instanceof ImageView) {
        try {/*from  w w  w. j  a  v  a  2s.com*/
            Field field = ImageView.class.getDeclaredField(fieldName);
            field.setAccessible(true);
            int fieldValue = (Integer) field.get(object);
            if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) {
                value = fieldValue;
            }
        } catch (Throwable e) {
        }
    }
    return value;
}

From source file:Main.java

protected static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) {
    try {//from w ww .ja v  a  2  s. c  o m
        final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
        staticField.setAccessible(true);
        staticField.set(null, newTypeface);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Object getDeclaredField(Object obj, Class<?> cls, String fieldName) throws NoSuchFieldException {
    if (obj == null || cls == null || fieldName == null) {
        throw new IllegalArgumentException("parameter can not be null!");
    }/*from ww w  .j  a  va 2s. com*/
    try {
        Field declaredField = cls.getDeclaredField(fieldName);
        declaredField.setAccessible(true);
        return declaredField.get(obj);
    } catch (Exception e) {
        throw new NoSuchFieldException(fieldName);
    }
}

From source file:Main.java

public static Object getReflection(Object itemToGetObject, String objectName) {
    try {/*from www . java 2  s  .  c  o  m*/
        Class<?> clazz = itemToGetObject.getClass();
        Field field;
        field = clazz.getDeclaredField(objectName);
        field.setAccessible(true);
        return field.get(itemToGetObject);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static <T> T converMap2Object(Map<String, String> map, Class<T> cls) {

    Field[] fields = cls.getDeclaredFields();

    T t = null;/*w w w .ja  v  a  2s .c  o m*/
    try {
        t = (T) cls.newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    for (Field f : fields) {
        f.setAccessible(true);
        invokeSet(t, f.getName(), map.get(f.getName()));
    }
    return t;
}

From source file:Main.java

public static int getFd(FileDescriptor fileDescriptor) {
    int fdInt = -1;
    try {/*from w  w  w.  j ava 2 s. c  o  m*/
        if (fileDescriptor != null) {
            Field descriptor = fileDescriptor.getClass().getDeclaredField("descriptor");
            descriptor.setAccessible(true);
            fdInt = descriptor.getInt(fileDescriptor);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return fdInt;
}

From source file:Main.java

/**
 * Gets the field with the given name within the class, or {@code null} if not found. If found,
 * the field is made accessible./*from   w  w  w.j  a  v  a  2s .  com*/
 */
private static Field field(Class<?> clazz, String fieldName) {
    Field field;
    try {
        field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
    } catch (Throwable t) {
        // Failed to access the fields.
        field = null;
    }
    return field;
}

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. ja  v a  2 s.com
@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   w  ww.ja v  a  2s  . c  o m
    }
    try {
        Field mCursorDrawableRes = ProgressBar.class.getDeclaredField("mDuration");
        mCursorDrawableRes.setAccessible(true);
        mCursorDrawableRes.setInt(progressBar, duration);
    } catch (Exception e) {
        //            FileLog.e("tmessages", e);
    }
}