Skip to content

Commit

Permalink
Use invoke for SslContextBuilder#endpointIdentificationAlgorithm(Stri…
Browse files Browse the repository at this point in the history
…ng) (#3072)

Motivation:

Users reported that they can see:
```
io.servicetalk.transport.netty.internal.SslContextFactory - SslContextBuilder#endpointIdentificationAlgorithm(String) is available only starting from Netty 4.2.0.Final. Detected Netty version: 4.1.113.Final
java.lang.invoke.WrongMethodTypeException: expected (SslContextBuilder,String)SslContextBuilder but found (SslContextBuilder,Object)SslContextBuilder
        at java.base/java.lang.invoke.Invokers.newWrongMethodTypeException(Invokers.java:523) ~[?:?]
        at java.base/java.lang.invoke.Invokers.checkExactType(Invokers.java:532) ~[?:?]
        at io.servicetalk.transport.netty.internal.SslContextFactory.setEndpointIdentificationAlgorithm(SslContextFactory.java:126) ~[servicetalk-transport-netty-internal-0.42.48.jar:0.42.48]
        at io.servicetalk.transport.netty.internal.SslContextFactory.<clinit>(SslContextFactory.java:92) ~[servicetalk-transport-netty-internal-0.42.48.jar:0.42.48]
```

However, this method was backported to Netty 4.1.112. See release notes:
https://netty.io/news/2024/07/19/4-1-112-Final.html

For some reason, `invokeExact` does not work as expected.

Modifications:

- Replaced `invokeExact` with `invoke`;

Result:

Method `SslContextBuilder#endpointIdentificationAlgorithm(String)` is
detected for Netty 4.1.112.
  • Loading branch information
idelpivnitskiy authored Oct 2, 2024
1 parent 892e613 commit a3a4212
Showing 1 changed file with 7 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ public final class SslContextFactory {

MethodHandle endpointIdentificationAlgorithm;
try {
// Find a new method that exists only in Netty starting from 4.2.0.Final:
// Find a new method that exists only in Netty starting from 4.1.112.Final:
endpointIdentificationAlgorithm = MethodHandles.publicLookup().findVirtual(SslContextBuilder.class,
"endpointIdentificationAlgorithm", methodType(SslContextBuilder.class, String.class));
// Verify the method is working as expected:
setEndpointIdentificationAlgorithm(endpointIdentificationAlgorithm,
SslContextBuilder.forClient(), "HTTPS");
} catch (Throwable cause) {
LOGGER.debug("SslContextBuilder#endpointIdentificationAlgorithm(String) is available only " +
"starting from Netty 4.2.0.Final. Detected Netty version: {}",
"starting from Netty 4.1.112.Final. Detected Netty version: {}",
SslContextBuilder.class.getPackage().getImplementationVersion(), cause);
endpointIdentificationAlgorithm = null;
}
Expand All @@ -115,18 +115,16 @@ private static boolean isOptionSupported(final MethodHandle isOptionSupportedMet
}
}

private static SslContextBuilder setEndpointIdentificationAlgorithm(@Nullable final MethodHandle methodHandle,
final SslContextBuilder builderInstance,
@Nullable final String algorithm) {
private static void setEndpointIdentificationAlgorithm(@Nullable final MethodHandle methodHandle,
final SslContextBuilder builderInstance,
@Nullable final String algorithm) {
if (methodHandle == null) {
return builderInstance;
return;
}
try {
// invokeExact requires return type cast to match the type signature
return (SslContextBuilder) methodHandle.invokeExact(builderInstance, algorithm == null ? "" : algorithm);
methodHandle.invoke(builderInstance, algorithm == null ? "" : algorithm);
} catch (Throwable t) {
throwException(t);
return builderInstance;
}
}

Expand Down

0 comments on commit a3a4212

Please sign in to comment.