Skip to content

Commit

Permalink
#83: Failed to install socket instrumentation code with Java 16 (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
jowerner authored Apr 22, 2021
1 parent cb191f3 commit 08b32a6
Showing 1 changed file with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemUtils;

import com.xceptance.common.lang.ReflectionUtils;
import com.xceptance.xlt.engine.RequestExecutionContext;

/**
Expand All @@ -43,6 +42,10 @@
*/
class InstrumentedSocketImpl extends SocketImpl
{
private static final Constructor<?> CONSTRUCTOR;

private static final Method FACTORY_METHOD;

private static final Method ACCEPT_METHOD;

private static final Method AVAILABLE_METHOD;
Expand All @@ -57,8 +60,6 @@ class InstrumentedSocketImpl extends SocketImpl

private static final Method CONNECT_SOCKETADDRESS_METHOD;

private static final Constructor<?> CONSTRUCTOR;

private static final Method CREATE_METHOD;

private static final Method GETFILEDESCRIPTOR_METHOD;
Expand Down Expand Up @@ -89,22 +90,22 @@ class InstrumentedSocketImpl extends SocketImpl
{
try
{
// get the constructor of the chosen SocketImpl subclass and make it accessible
// get the constructor/the factory method to create a platform SocketImpl and make it accessible
if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_13))
{
final boolean usePlainSocketImpl = ReflectionUtils.readStaticField(SocketImpl.class, "USE_PLAINSOCKETIMPL");
final String socketImplClassName = usePlainSocketImpl ? "java.net.PlainSocketImpl" : "sun.nio.ch.NioSocketImpl";

CONSTRUCTOR = Class.forName(socketImplClassName).getDeclaredConstructor(boolean.class);
// use SocketImpl.createPlatformSocketImpl() instead of messing around with internal SocketImpl classes
FACTORY_METHOD = SocketImpl.class.getDeclaredMethod("createPlatformSocketImpl", boolean.class);
FACTORY_METHOD.setAccessible(true);
CONSTRUCTOR = null;
}
else
{
CONSTRUCTOR = Class.forName("java.net.PlainSocketImpl").getDeclaredConstructor();
CONSTRUCTOR.setAccessible(true);
FACTORY_METHOD = null;
}

CONSTRUCTOR.setAccessible(true);

// get the methods of SocketImpl and make them callable even though they are abstract
// get the protected methods of SocketImpl and make them callable
final Class<?> ABSTRACT_CLASS = Class.forName("java.net.SocketImpl");

ACCEPT_METHOD = ABSTRACT_CLASS.getDeclaredMethod("accept", SocketImpl.class);
Expand Down Expand Up @@ -181,7 +182,7 @@ class InstrumentedSocketImpl extends SocketImpl
}

/**
* The actual socket implementation, i.e. a PlainSocketImpl.
* The actual socket implementation.
*/
private final SocketImpl socketImpl;

Expand All @@ -195,7 +196,7 @@ public InstrumentedSocketImpl()
// create a SocketImpl instance
if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_13))
{
socketImpl = (SocketImpl) CONSTRUCTOR.newInstance(false);
socketImpl = (SocketImpl) FACTORY_METHOD.invoke(null, false);
}
else
{
Expand Down

0 comments on commit 08b32a6

Please sign in to comment.