From cb738188de992d7691b618196a5c26fffed13bf0 Mon Sep 17 00:00:00 2001 From: Janmm14 Date: Sat, 26 Jun 2021 00:01:30 +0000 Subject: [PATCH] #3126: Use suppliers instead of reflection for native impl generation. --- .../main/java/net/md_5/bungee/jni/NativeCode.java | 15 +++++---------- .../net/md_5/bungee/jni/cipher/JavaCipher.java | 10 ++++++++-- .../java/net/md_5/bungee/NativeCipherTest.java | 2 +- .../test/java/net/md_5/bungee/NativeZlibTest.java | 2 +- .../main/java/net/md_5/bungee/EncryptionUtil.java | 2 +- .../net/md_5/bungee/compress/CompressFactory.java | 2 +- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/native/src/main/java/net/md_5/bungee/jni/NativeCode.java b/native/src/main/java/net/md_5/bungee/jni/NativeCode.java index 4e4130bc..62bdaa0e 100644 --- a/native/src/main/java/net/md_5/bungee/jni/NativeCode.java +++ b/native/src/main/java/net/md_5/bungee/jni/NativeCode.java @@ -6,18 +6,19 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.function.Supplier; import net.md_5.bungee.jni.cipher.BungeeCipher; public final class NativeCode { private final String name; - private final Class javaImpl; - private final Class nativeImpl; + private final Supplier javaImpl; + private final Supplier nativeImpl; // private boolean loaded; - public NativeCode(String name, Class javaImpl, Class nativeImpl) + public NativeCode(String name, Supplier javaImpl, Supplier nativeImpl) { this.name = name; this.javaImpl = javaImpl; @@ -26,13 +27,7 @@ public final class NativeCode public T newInstance() { - try - { - return ( loaded ) ? nativeImpl.getDeclaredConstructor().newInstance() : javaImpl.getDeclaredConstructor().newInstance(); - } catch ( ReflectiveOperationException ex ) - { - throw new RuntimeException( "Error getting instance", ex ); - } + return ( loaded ) ? nativeImpl.get() : javaImpl.get(); } public boolean load() diff --git a/native/src/main/java/net/md_5/bungee/jni/cipher/JavaCipher.java b/native/src/main/java/net/md_5/bungee/jni/cipher/JavaCipher.java index d211cef6..0e27c2d7 100644 --- a/native/src/main/java/net/md_5/bungee/jni/cipher/JavaCipher.java +++ b/native/src/main/java/net/md_5/bungee/jni/cipher/JavaCipher.java @@ -25,9 +25,15 @@ public class JavaCipher implements BungeeCipher } } - public JavaCipher() throws GeneralSecurityException + public JavaCipher() { - this.cipher = Cipher.getInstance( "AES/CFB8/NoPadding" ); + try + { + this.cipher = Cipher.getInstance( "AES/CFB8/NoPadding" ); + } catch ( GeneralSecurityException ex ) + { + throw new RuntimeException( ex ); + } } @Override diff --git a/native/src/test/java/net/md_5/bungee/NativeCipherTest.java b/native/src/test/java/net/md_5/bungee/NativeCipherTest.java index 72b81ab8..b67862ea 100644 --- a/native/src/test/java/net/md_5/bungee/NativeCipherTest.java +++ b/native/src/test/java/net/md_5/bungee/NativeCipherTest.java @@ -26,7 +26,7 @@ public class NativeCipherTest private final SecretKey secret = new SecretKeySpec( new byte[ 16 ], "AES" ); private static final int BENCHMARK_COUNT = 4096; // - private static final NativeCode factory = new NativeCode<>( "native-cipher", JavaCipher.class, NativeCipher.class ); + private static final NativeCode factory = new NativeCode<>( "native-cipher", JavaCipher::new, NativeCipher::new ); @Test public void testNative() throws Exception diff --git a/native/src/test/java/net/md_5/bungee/NativeZlibTest.java b/native/src/test/java/net/md_5/bungee/NativeZlibTest.java index fde626b1..60b71a13 100644 --- a/native/src/test/java/net/md_5/bungee/NativeZlibTest.java +++ b/native/src/test/java/net/md_5/bungee/NativeZlibTest.java @@ -15,7 +15,7 @@ import org.junit.Test; public class NativeZlibTest { - private final NativeCode factory = new NativeCode<>( "native-compress", JavaZlib.class, NativeZlib.class ); + private final NativeCode factory = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new ); @Test public void doTest() throws DataFormatException diff --git a/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java b/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java index ff6bb8c1..f76c395a 100644 --- a/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java +++ b/proxy/src/main/java/net/md_5/bungee/EncryptionUtil.java @@ -31,7 +31,7 @@ public class EncryptionUtil public static final KeyPair keys; @Getter private static final SecretKey secret = new SecretKeySpec( new byte[ 16 ], "AES" ); - public static final NativeCode nativeFactory = new NativeCode<>( "native-cipher", JavaCipher.class, NativeCipher.class ); + public static final NativeCode nativeFactory = new NativeCode<>( "native-cipher", JavaCipher::new, NativeCipher::new ); static { diff --git a/proxy/src/main/java/net/md_5/bungee/compress/CompressFactory.java b/proxy/src/main/java/net/md_5/bungee/compress/CompressFactory.java index 1f2d5a70..2e0c63d1 100644 --- a/proxy/src/main/java/net/md_5/bungee/compress/CompressFactory.java +++ b/proxy/src/main/java/net/md_5/bungee/compress/CompressFactory.java @@ -8,5 +8,5 @@ import net.md_5.bungee.jni.zlib.NativeZlib; public class CompressFactory { - public static final NativeCode zlib = new NativeCode<>( "native-compress", JavaZlib.class, NativeZlib.class ); + public static final NativeCode zlib = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new ); }