#3126: Use suppliers instead of reflection for native impl generation.

This commit is contained in:
Janmm14 2021-06-26 00:01:30 +00:00 committed by GitHub
parent a8b2f5268d
commit cb738188de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 16 deletions

View File

@ -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<T>
{
private final String name;
private final Class<? extends T> javaImpl;
private final Class<? extends T> nativeImpl;
private final Supplier<? extends T> javaImpl;
private final Supplier<? extends T> nativeImpl;
//
private boolean loaded;
public NativeCode(String name, Class<? extends T> javaImpl, Class<? extends T> nativeImpl)
public NativeCode(String name, Supplier<? extends T> javaImpl, Supplier<? extends T> nativeImpl)
{
this.name = name;
this.javaImpl = javaImpl;
@ -26,13 +27,7 @@ public final class NativeCode<T>
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()

View File

@ -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

View File

@ -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<BungeeCipher> factory = new NativeCode<>( "native-cipher", JavaCipher.class, NativeCipher.class );
private static final NativeCode<BungeeCipher> factory = new NativeCode<>( "native-cipher", JavaCipher::new, NativeCipher::new );
@Test
public void testNative() throws Exception

View File

@ -15,7 +15,7 @@ import org.junit.Test;
public class NativeZlibTest
{
private final NativeCode<BungeeZlib> factory = new NativeCode<>( "native-compress", JavaZlib.class, NativeZlib.class );
private final NativeCode<BungeeZlib> factory = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new );
@Test
public void doTest() throws DataFormatException

View File

@ -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<BungeeCipher> nativeFactory = new NativeCode<>( "native-cipher", JavaCipher.class, NativeCipher.class );
public static final NativeCode<BungeeCipher> nativeFactory = new NativeCode<>( "native-cipher", JavaCipher::new, NativeCipher::new );
static
{

View File

@ -8,5 +8,5 @@ import net.md_5.bungee.jni.zlib.NativeZlib;
public class CompressFactory
{
public static final NativeCode<BungeeZlib> zlib = new NativeCode<>( "native-compress", JavaZlib.class, NativeZlib.class );
public static final NativeCode<BungeeZlib> zlib = new NativeCode<>( "native-compress", JavaZlib::new, NativeZlib::new );
}