Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use LambdaMetafactory to generate Supplier<Checksum> for java.util.zip.CRC32C instances #595

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/main/java/org/xerial/snappy/SnappyFramed.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package org.xerial.snappy;

import java.io.IOException;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
Expand Down Expand Up @@ -37,21 +38,27 @@ final class SnappyFramed
Supplier<Checksum> supplier = null;
try
{
final Class crc32cClazz = Class.forName("java.util.zip.CRC32C");
final MethodHandles.Lookup lookup = MethodHandles.publicLookup();

final MethodHandle conHandle = lookup.findConstructor(crc32cClazz, MethodType.methodType(void.class))
.asType(MethodType.methodType(Checksum.class));
supplier = () -> {
try
{
return (Checksum) conHandle.invokeExact();
}
catch (Throwable e)
{
throw new IllegalStateException(e);
}
};
final Class<?> crc32cClazz = Class.forName("java.util.zip.CRC32C");
// using LambdaMetafactory requires a caller sensitive lookup
final MethodHandles.Lookup lookup = MethodHandles.lookup();
final MethodHandle conHandle = lookup.findConstructor(crc32cClazz, MethodType.methodType(void.class));

// use LambdaMetafactory to generate an implementation of Supplier<Checksum> which invokes
// the java.util.zip.CRC32C default constructor
supplier = (Supplier<Checksum>) LambdaMetafactory.metafactory(lookup,
// method name on Supplier
"get",
// functional interface to be created by factory
MethodType.methodType(Supplier.class),
// type of the functional interface
// uses a generic, so erasure to Object
MethodType.methodType(Object.class),
// the method handle to call
conHandle,
// type as used at call site
MethodType.methodType(Checksum.class))
.getTarget()
.invoke();
}
catch(Throwable t)
{
Expand Down
Loading