    001package org.apache.log4j;
    002
    003import java.lang.reflect.Field;
    004import java.lang.reflect.Method;
    005
    006import org.apache.log4j.helpers.ThreadLocalMap;
    007
    008public class MDCFriend {
    009
    010        public static void fixForJava9() {
    011                if (MDC.mdc.tlm == null) {
    012                        MDC.mdc.tlm = new ThreadLocalMap();
    013                        MDC.mdc.java1 = false;
    014                        setRemoveMethod(MDC.mdc);
    015                }
    016
    017        }
    018
    019        private static void setRemoveMethod(MDC mdc) {
    020                try {
    021                        Method removeMethod = ThreadLocal.class.getMethod("remove");
    022                        Field removeMethodField = MDC.class.getDeclaredField("removeMethod");
    023                        removeMethodField.setAccessible(true);
    024                        removeMethodField.set(mdc, removeMethod);
    025                } catch (NoSuchMethodException e) {
    026                } catch (SecurityException e) {
    027                } catch (NoSuchFieldException e) {
    028                } catch (IllegalArgumentException e) {
    029                } catch (IllegalAccessException e) {
    030                }
    031
    032        }
    033}