Codebase list libslf4j-java / upstream/latest slf4j-log4j12 / src / main / java / org / apache / log4j / MDCFriend.java
upstream/latest

Tree @upstream/latest (Download .tar.gz)

MDCFriend.java @upstream/latestraw · history · blame

package org.apache.log4j;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.apache.log4j.helpers.ThreadLocalMap;

public class MDCFriend {

	public static void fixForJava9() {
		if (MDC.mdc.tlm == null) {
			MDC.mdc.tlm = new ThreadLocalMap();
			MDC.mdc.java1 = false;
			setRemoveMethod(MDC.mdc);
		}

	}

	private static void setRemoveMethod(MDC mdc) {
		try {
			Method removeMethod = ThreadLocal.class.getMethod("remove");
			Field removeMethodField = MDC.class.getDeclaredField("removeMethod");
			removeMethodField.setAccessible(true);
			removeMethodField.set(mdc, removeMethod);
		} catch (NoSuchMethodException e) {
		} catch (SecurityException e) {
		} catch (NoSuchFieldException e) {
		} catch (IllegalArgumentException e) {
		} catch (IllegalAccessException e) {
		}

	}
}