From 3763bd69c345e706d33393aa5ac89a8e8619e430 Mon Sep 17 00:00:00 2001 From: burningtnt Date: Sat, 25 Nov 2023 12:53:30 +0800 Subject: [PATCH] Upgrade Method to MethodHandle --- .../burningtnt/webp/jfx/WEBPImageLoader.java | 45 +++++++++++-------- .../webp/jfx/WEBPImageLoaderFactory.java | 42 ++++++++++++----- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/jfx/src/main/java/net/burningtnt/webp/jfx/WEBPImageLoader.java b/jfx/src/main/java/net/burningtnt/webp/jfx/WEBPImageLoader.java index 9083a97..0705fcb 100644 --- a/jfx/src/main/java/net/burningtnt/webp/jfx/WEBPImageLoader.java +++ b/jfx/src/main/java/net/burningtnt/webp/jfx/WEBPImageLoader.java @@ -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 { @@ -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; diff --git a/jfx/src/main/java/net/burningtnt/webp/jfx/WEBPImageLoaderFactory.java b/jfx/src/main/java/net/burningtnt/webp/jfx/WEBPImageLoaderFactory.java index 34e6378..6145d13 100644 --- a/jfx/src/main/java/net/burningtnt/webp/jfx/WEBPImageLoaderFactory.java +++ b/jfx/src/main/java/net/burningtnt/webp/jfx/WEBPImageLoaderFactory.java @@ -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(); @@ -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); + } } } }