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:api_proto3.TestElf.java

public static void setSlf4jLogLevel(Class<?> clazz, Level logLevel) {
    try {//from ww  w  . j a  v  a 2s.  com
        //Log4jLogger log4Jlogger = (Log4jLogger) LoggerFactory.getLogger(clazz);
        Log4JLogger log4Jlogger = (Log4JLogger) LoggerFactory.getLogger(clazz);

        //Field field = clazz.getClassLoader().loadClass("org.apache.logging.slf4j.Log4jLogger").getDeclaredField("logger");
        Field field = clazz.getClassLoader().loadClass("org.apache.commons.logging.impl.Log4JLogger")
                .getDeclaredField("logger");
        field.setAccessible(true);

        Logger logger = (Logger) field.get(log4Jlogger);
        logger.setLevel(logLevel);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.lily.dap.util.ReflectionUtils.java

/**
 * , DeclaredField,    ./* w ww .j av  a2s  . com*/
 * 
 * Object, null.
 */
public static Field getAccessibleField(final Object obj, final String fieldName) {
    for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
            .getSuperclass()) {
        try {
            Field field = superClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException e) {//NOSONAR
            // Field,
        }
    }
    return null;
}

From source file:Main.java

public static void traverseAndRecolor(View root, int color, boolean withStates,
        boolean setClickableItemBackgrounds) {
    Context context = root.getContext();

    if (setClickableItemBackgrounds && root.isClickable()) {
        StateListDrawable selectableItemBackground = new StateListDrawable();
        selectableItemBackground.addState(new int[] { android.R.attr.state_pressed },
                new ColorDrawable((color & 0xffffff) | 0x33000000));
        selectableItemBackground.addState(new int[] { android.R.attr.state_focused },
                new ColorDrawable((color & 0xffffff) | 0x44000000));
        selectableItemBackground.addState(new int[] {}, null);
        root.setBackground(selectableItemBackground);
    }/*from   ww  w .j  a  va  2  s  .c  om*/

    if (root instanceof ViewGroup) {
        ViewGroup parent = (ViewGroup) root;
        for (int i = 0; i < parent.getChildCount(); i++) {
            traverseAndRecolor(parent.getChildAt(i), color, withStates, setClickableItemBackgrounds);
        }

    } else if (root instanceof ImageView) {
        ImageView imageView = (ImageView) root;
        Drawable sourceDrawable = imageView.getDrawable();
        if (withStates && sourceDrawable != null && sourceDrawable instanceof BitmapDrawable) {
            imageView.setImageDrawable(
                    makeRecoloredDrawable(context, (BitmapDrawable) sourceDrawable, color, true));
        } else {
            imageView.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
        }

    } else if (root instanceof TextView) {
        TextView textView = (TextView) root;
        if (withStates) {
            int sourceColor = textView.getCurrentTextColor();
            ColorStateList colorStateList = new ColorStateList(
                    new int[][] { new int[] { android.R.attr.state_pressed },
                            new int[] { android.R.attr.state_focused }, new int[] {} },
                    new int[] { sourceColor, sourceColor, color });
            textView.setTextColor(colorStateList);
        } else {
            textView.setTextColor(color);
        }

    } else if (root instanceof AnalogClock) {
        AnalogClock analogClock = (AnalogClock) root;
        try {
            Field hourHandField = AnalogClock.class.getDeclaredField("mHourHand");
            hourHandField.setAccessible(true);
            Field minuteHandField = AnalogClock.class.getDeclaredField("mMinuteHand");
            minuteHandField.setAccessible(true);
            Field dialField = AnalogClock.class.getDeclaredField("mDial");
            dialField.setAccessible(true);
            BitmapDrawable hourHand = (BitmapDrawable) hourHandField.get(analogClock);
            if (hourHand != null) {
                Drawable d = makeRecoloredDrawable(context, hourHand, color, withStates);
                d.setCallback(analogClock);
                hourHandField.set(analogClock, d);
            }
            BitmapDrawable minuteHand = (BitmapDrawable) minuteHandField.get(analogClock);
            if (minuteHand != null) {
                Drawable d = makeRecoloredDrawable(context, minuteHand, color, withStates);
                d.setCallback(analogClock);
                minuteHandField.set(analogClock, d);
            }
            BitmapDrawable dial = (BitmapDrawable) dialField.get(analogClock);
            if (dial != null) {
                Drawable d = makeRecoloredDrawable(context, dial, color, withStates);
                d.setCallback(analogClock);
                dialField.set(analogClock, d);
            }
        } catch (NoSuchFieldException ignored) {
        } catch (IllegalAccessException ignored) {
        } catch (ClassCastException ignored) {
        } // TODO: catch all exceptions?
    }
}

From source file:com.jdy.ddj.common.utils.Reflections.java

/**
 * ?, ?DeclaredField,    ?.//from  w w w  . j  av  a  2 s .  com
 *
 * ?Object?, null.
 */
public static Field getAccessibleField(final Object obj, final String fieldName) {
    Validate.notNull(obj, "object can't be null");
    Validate.notBlank(fieldName, "fieldName can't be blank");
    for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
            .getSuperclass()) {
        try {
            Field field = superClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException e) {//NOSONAR
            // Field??,?
        }
    }
    return null;
}

From source file:api_proto3.TestElf.java

public static void setSlf4jTargetStream(Class<?> clazz, PrintStream stream) {
    try {/*from  w ww .j  a  v a  2  s .  com*/
        //Log4jLogger log4Jlogger = (Log4jLogger) LoggerFactory.getLogger(clazz);
        Log4JLogger log4Jlogger = (Log4JLogger) LoggerFactory.getLogger(clazz);

        //Field field = clazz.getClassLoader().loadClass("org.apache.logging.slf4j.Log4jLogger").getDeclaredField("logger");
        Field field = clazz.getClassLoader().loadClass("org.apache.commons.logging.impl.Log4JLogger")
                .getDeclaredField("logger");
        field.setAccessible(true);

        Logger logger = (Logger) field.get(log4Jlogger);
        if (logger.getAppenders().containsKey("string")) {
            Appender appender = logger.getAppenders().get("string");
            logger.removeAppender(appender);
        }

        logger.addAppender(new StringAppender("string", stream));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:cn.hxh.springside.utils.ReflectionUtils.java

/**
 * ?, ?DeclaredField,    ?.//from   w w  w . j a  va 2 s.c  o m
 * 
 * ?Object?, null.
 */
public static Field getAccessibleField(final Object obj, final String fieldName) {
    AssertUtils.notNull(obj, "object?");
    AssertUtils.hasText(fieldName, "fieldName");
    for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
            .getSuperclass()) {
        try {
            Field field = superClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            return field;
        } catch (NoSuchFieldException e) {//NOSONAR
            // Field??,?
        }
    }
    return null;
}

From source file:Main.java

public static void fixInputMethodManagerLeak(Context context) {
    if (context == null) {
        return;/*w  ww  .  j ava  2 s.  c o  m*/
    }

    try {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) {
            return;
        }

        Field f_mCurRootView = imm.getClass().getDeclaredField("mCurRootView");
        Field f_mServedView = imm.getClass().getDeclaredField("mServedView");
        Field f_mNextServedView = imm.getClass().getDeclaredField("mNextServedView");

        f_mCurRootView.setAccessible(true);
        f_mCurRootView.set(imm, null);

        f_mServedView.setAccessible(true);
        f_mServedView.set(imm, null);

        f_mNextServedView.setAccessible(true);
        f_mNextServedView.set(imm, null);
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

From source file:de.Keyle.MyPet.util.Util.java

public static Field getField(Class<?> clazz, String field) {
    try {/*ww  w.  j a v a 2 s . co m*/
        Field f = clazz.getDeclaredField(field);
        f.setAccessible(true);
        return f;
    } catch (Exception ignored) {
        return null;
    }
}

From source file:com.all.testing.AppContextTestUtils.java

public static void checkField(Object object, String name) throws Exception {
    log.debug("Checking... " + object.getClass().getName() + ":" + name);
    Field declaredField = object.getClass().getDeclaredField(name);
    declaredField.setAccessible(true);
    assertNotNull("Field " + name + " on " + object.getClass().getName() + " is null",
            declaredField.get(object));/*from w  w  w .  ja v a  2  s  . c o  m*/
}

From source file:com.atlassian.connector.eclipse.internal.crucible.ui.operations.CrucibleFileInfoCompareEditorInput.java

private static Viewer findContentViewer(Viewer contentViewer, ICompareInput input, Composite parent,
        CrucibleCompareAnnotationModel annotationModel) {

    // FIXME: hack
    if (contentViewer instanceof TextMergeViewer) {
        TextMergeViewer textMergeViewer = (TextMergeViewer) contentViewer;
        try {/*from ww w. ja v a 2 s  .  c o  m*/
            Class<TextMergeViewer> clazz = TextMergeViewer.class;
            Field declaredField = clazz.getDeclaredField("fLeft");
            declaredField.setAccessible(true);
            final MergeSourceViewer fLeft = (MergeSourceViewer) declaredField.get(textMergeViewer);

            declaredField = clazz.getDeclaredField("fRight");
            declaredField.setAccessible(true);
            final MergeSourceViewer fRight = (MergeSourceViewer) declaredField.get(textMergeViewer);

            annotationModel.attachToViewer(textMergeViewer, fLeft, fRight);
            annotationModel.focusOnComment();
            annotationModel.registerContextMenu();

            Method setActiveViewer = clazz.getDeclaredMethod("setActiveViewer", MergeSourceViewer.class,
                    boolean.class);
            setActiveViewer.setAccessible(true);
            setActiveViewer.invoke(textMergeViewer, fRight, true);

            hackGalileo(contentViewer, textMergeViewer, fLeft, fRight);
        } catch (Throwable t) {
            StatusHandler.log(new Status(IStatus.WARNING, AtlassianTeamUiPlugin.PLUGIN_ID,
                    "Could not initialize annotation model for " + input.getName(), t));
        }
    }
    return contentViewer;
}