Skip to content

Commit

Permalink
Upgrade Method to MethodHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Nov 25, 2023
1 parent d38729b commit 3763bd6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
45 changes: 27 additions & 18 deletions jfx/src/main/java/net/burningtnt/webp/jfx/WEBPImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.ByteBuffer;

public class WEBPImageLoader extends ImageLoaderImpl {
Expand All @@ -39,27 +40,35 @@ public static ImageDescriptor getImageDescriptor() {
private static ImageDescriptor initImageDescriptor() {
Throwable throwable1;
try {
return ImageDescriptor.class.getConstructor(String.class, String[].class, ImageFormatDescription.Signature[].class)
.newInstance(
"WEBP",
new String[]{"webp"},
new ImageFormatDescription.Signature[]{new ImageFormatDescription.Signature((byte) 'R', (byte) 'I', (byte) 'F', (byte) 'F')}
);
} catch (InvocationTargetException | InstantiationException | IllegalAccessException |
NoSuchMethodException e) {
return (ImageDescriptor) MethodHandles.lookup().findConstructor(
ImageDescriptor.class,
MethodType.methodType(
void.class,
String.class, String[].class, ImageFormatDescription.Signature[].class
)
).invoke(
"WEBP",
new String[]{"webp"},
new ImageFormatDescription.Signature[]{new ImageFormatDescription.Signature((byte) 'R', (byte) 'I', (byte) 'F', (byte) 'F')}
);
} catch (Throwable e) {
throwable1 = e;
}

try {
return ImageDescriptor.class.getConstructor(String.class, String[].class, ImageFormatDescription.Signature[].class, String[].class)
.newInstance(
"WEBP",
new String[]{"webp"},
new ImageFormatDescription.Signature[]{new ImageFormatDescription.Signature((byte) 'R', (byte) 'I', (byte) 'F', (byte) 'F')},
new String[]{"webp"}
);
} catch (InvocationTargetException | InstantiationException | IllegalAccessException |
NoSuchMethodException e) {
return (ImageDescriptor) MethodHandles.lookup().findConstructor(
ImageDescriptor.class,
MethodType.methodType(
void.class,
String.class, String[].class, ImageFormatDescription.Signature[].class, String[].class
)
).invoke(
"WEBP",
new String[]{"webp"},
new ImageFormatDescription.Signature[]{new ImageFormatDescription.Signature((byte) 'R', (byte) 'I', (byte) 'F', (byte) 'F')},
new String[]{"webp"}
);
} catch (Throwable e) {
IllegalStateException t = new IllegalStateException("Cannot construct a ImageDescriptor.", e);
t.addSuppressed(throwable1);
throw t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import com.sun.javafx.iio.ImageStorage;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public final class WEBPImageLoaderFactory implements ImageLoaderFactory {
private static final WEBPImageLoaderFactory instance = new WEBPImageLoaderFactory();
Expand All @@ -39,17 +40,36 @@ public ImageLoader createImageLoader(InputStream input) {
}

public static void setupListener() {
ImageStorage imageStorage; // Get the instance of ImageStorage if needed.
MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
try {
imageStorage = (ImageStorage) ImageStorage.class.getMethod("getInstance").invoke(null);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
imageStorage = null;
}

try {
ImageStorage.class.getMethod("addImageLoaderFactory", ImageLoaderFactory.class).invoke(imageStorage, instance);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new IllegalStateException("Cannot install WEBPImageLoader", e);
LOOKUP.findVirtual(
ImageStorage.class,
"addImageLoaderFactory",
MethodType.methodType(
void.class,
ImageLoaderFactory.class
)
).invoke((ImageStorage) LOOKUP.findStatic(
ImageStorage.class,
"getInstance",
MethodType.methodType(
ImageStorage.class
)
).invoke(), instance);
} catch (Throwable e) {
try {
LOOKUP.findStatic(
ImageStorage.class,
"addImageLoaderFactory",
MethodType.methodType(
void.class,
ImageLoaderFactory.class
)
).invoke(instance);
} catch (Throwable e2) {
e2.addSuppressed(e);
throw new IllegalStateException("Cannot install WEBPImageLoader", e);
}
}
}
}

0 comments on commit 3763bd6

Please sign in to comment.