Reflective setAccessible(true) will produce scary warnings on the console when using java9+, dont do it

Motivation:

Reflective setAccessible(true) will produce scary warnings on the console when using java9+, while netty still works. That said users may feel uncomfortable with these warnings, we should not try to do it by default when using java9+.

Modifications:

Add io.netty.tryReflectionSetAccessible  system property which controls if setAccessible(...) will be used. By default it will bet set to false when using java9+.

Result:

Fixes [#7254].
This commit is contained in:
Norman Maurer 2018-01-29 15:30:21 +01:00
parent 16239d4ea3
commit c62345f3a9
4 changed files with 23 additions and 9 deletions

View File

@ -46,6 +46,8 @@ final class PlatformDependent0 {
private static final Throwable UNSAFE_UNAVAILABILITY_CAUSE;
private static final Object INTERNAL_UNSAFE;
private static final boolean IS_EXPLICIT_TRY_REFLECTION_SET_ACCESSIBLE = explicitTryReflectionSetAccessible0();
static final Unsafe UNSAFE;
/**
@ -79,7 +81,9 @@ final class PlatformDependent0 {
public Object run() {
try {
final Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
Throwable cause = ReflectionUtil.trySetAccessible(unsafeField);
// We always want to try using Unsafe as the access still works on java9 as well and
// we need it for out native-transports and many optimizations.
Throwable cause = ReflectionUtil.trySetAccessible(unsafeField, false);
if (cause != null) {
return cause;
}
@ -213,7 +217,7 @@ final class PlatformDependent0 {
try {
final Constructor<?> constructor =
direct.getClass().getDeclaredConstructor(long.class, int.class);
Throwable cause = ReflectionUtil.trySetAccessible(constructor);
Throwable cause = ReflectionUtil.trySetAccessible(constructor, true);
if (cause != null) {
return cause;
}
@ -261,7 +265,7 @@ final class PlatformDependent0 {
Class<?> bitsClass =
Class.forName("java.nio.Bits", false, getSystemClassLoader());
Method unalignedMethod = bitsClass.getDeclaredMethod("unaligned");
Throwable cause = ReflectionUtil.trySetAccessible(unalignedMethod);
Throwable cause = ReflectionUtil.trySetAccessible(unalignedMethod, true);
if (cause != null) {
return cause;
}
@ -664,6 +668,15 @@ final class PlatformDependent0 {
return android;
}
private static boolean explicitTryReflectionSetAccessible0() {
// we disable reflective access
return SystemPropertyUtil.getBoolean("io.netty.tryReflectionSetAccessible", javaVersion() < 9);
}
static boolean isExplicitTryReflectionSetAccessible() {
return IS_EXPLICIT_TRY_REFLECTION_SET_ACCESSIBLE;
}
static int javaVersion() {
return JAVA_VERSION;
}

View File

@ -26,7 +26,10 @@ public final class ReflectionUtil {
* {@link java.lang.reflect.InaccessibleObjectException} and return it.
* The caller must check if it returns {@code null} and if not handle the returned exception.
*/
public static Throwable trySetAccessible(AccessibleObject object) {
public static Throwable trySetAccessible(AccessibleObject object, boolean checkAccessible) {
if (checkAccessible && !PlatformDependent0.isExplicitTryReflectionSetAccessible()) {
return new UnsupportedOperationException("Reflective setAccessible(true) disabled");
}
try {
object.setAccessible(true);
return null;

View File

@ -17,7 +17,6 @@ package io.netty.util.internal.logging;
import static org.junit.Assert.assertEquals;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
@ -29,7 +28,6 @@ import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;
import org.hamcrest.CoreMatchers;
import org.junit.Assume;
import org.junit.Test;
import io.netty.util.internal.ReflectionUtil;
@ -68,7 +66,7 @@ public class Log4J2LoggerTest extends AbstractInternalLoggerTest<Logger> {
Method method = mockLog.getClass().getDeclaredMethod("setLevel", Level.class);
if (!method.isAccessible()) {
Assume.assumeThat(ReflectionUtil.trySetAccessible(method), CoreMatchers.nullValue());
Assume.assumeThat(ReflectionUtil.trySetAccessible(method, true), CoreMatchers.nullValue());
}
method.invoke(mockLog, targetLevel);
}

View File

@ -215,11 +215,11 @@ public final class NioEventLoop extends SingleThreadEventLoop {
Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");
Throwable cause = ReflectionUtil.trySetAccessible(selectedKeysField);
Throwable cause = ReflectionUtil.trySetAccessible(selectedKeysField, true);
if (cause != null) {
return cause;
}
cause = ReflectionUtil.trySetAccessible(publicSelectedKeysField);
cause = ReflectionUtil.trySetAccessible(publicSelectedKeysField, true);
if (cause != null) {
return cause;
}