@@ -29,19 +29,28 @@ public static Unsafe getUnsafe() {
2929
3030 public static Unsafe unsafe = getUnsafe ();
3131
32- private static Constructor <?> findDirectByteBufferConstructor () {
32+ private static ByteBufferCreator findDirectByteBufferConstructor () {
3333 try {
34- return Class . forName ( "java.nio.DirectByteBuffer" ). getDeclaredConstructor ( Long . TYPE , Integer . TYPE , Object . class );
34+ return constructorWithAttStrategy ( );
3535 }
3636 catch (ClassNotFoundException e ) {
37- throw new IllegalStateException (String .format ("Failed to find java.nio.DirectByteBuffer: $s" , e .getMessage ()));
37+ throw new IllegalStateException (
38+ String .format ("Failed to find java.nio.DirectByteBuffer: $s" , e .getMessage ()));
3839 }
3940 catch (NoSuchMethodException e ) {
40- throw new IllegalStateException (String .format ("Failed to find constructor f java.nio.DirectByteBuffer: $s" , e .getMessage ()));
41+ try {
42+ return constructorWithoutAttStrategy ();
43+ } catch (NoSuchMethodException e2 ) {
44+ throw new IllegalStateException (
45+ String .format ("Failed to find constructor f java.nio.DirectByteBuffer: $s" , e2 .getMessage ()), e2 );
46+ } catch (ClassNotFoundException e2 ) {
47+ throw new IllegalStateException (
48+ String .format ("Failed to find constructor f java.nio.DirectByteBuffer: $s" , e2 .getMessage ()), e2 );
49+ }
4150 }
4251 }
4352
44- private static Constructor <?> dbbCC = findDirectByteBufferConstructor ();
53+ private static ByteBufferCreator byteBufferCreator = findDirectByteBufferConstructor ();
4554
4655 /**
4756 * Create a new DirectByteBuffer from a given address and size.
@@ -55,15 +64,49 @@ private static Constructor<?> findDirectByteBufferConstructor() {
5564 * @return
5665 */
5766 public static ByteBuffer newDirectByteBuffer (long addr , int size , Object att ) {
58- dbbCC .setAccessible (true );
59- Object b = null ;
60- try {
61- b = dbbCC .newInstance (new Long (addr ), new Integer (size ), att );
62- return ByteBuffer .class .cast (b );
63- }
64- catch (Exception e ) {
65- throw new IllegalStateException (String .format ("Failed to create DirectByteBuffer: %s" , e .getMessage ()));
66- }
67+ return byteBufferCreator .newDirectByteBuffer (addr , size , att );
68+ }
69+
70+ private static ByteBufferCreator constructorWithAttStrategy ()
71+ throws ClassNotFoundException , NoSuchMethodException {
72+ final Constructor <? extends ByteBuffer > dbbCC =
73+ (Constructor <? extends ByteBuffer >) Class .forName ("java.nio.DirectByteBuffer" )
74+ .getDeclaredConstructor (Long .TYPE , Integer .TYPE , Object .class );
75+ return new ByteBufferCreator () {
76+ @ Override
77+ public ByteBuffer newDirectByteBuffer (long addr , int size , Object att ) {
78+ dbbCC .setAccessible (true );
79+ try {
80+ return dbbCC .newInstance (Long .valueOf (addr ), Integer .valueOf (size ), att );
81+ } catch (Exception e ) {
82+ throw new IllegalStateException (
83+ String .format ("Failed to create DirectByteBuffer: %s" , e .getMessage ()), e );
84+ }
85+ }
86+ };
87+ }
88+
89+ private static ByteBufferCreator constructorWithoutAttStrategy ()
90+ throws ClassNotFoundException , NoSuchMethodException {
91+ final Constructor <? extends ByteBuffer > dbbCC =
92+ (Constructor <? extends ByteBuffer >) Class .forName ("java.nio.DirectByteBuffer" )
93+ .getDeclaredConstructor (Long .TYPE , Integer .TYPE );
94+ return new ByteBufferCreator () {
95+ @ Override
96+ public ByteBuffer newDirectByteBuffer (long addr , int size , Object att ) {
97+ dbbCC .setAccessible (true );
98+ try {
99+ return dbbCC .newInstance (Long .valueOf (addr ), Integer .valueOf (size ));
100+ } catch (Exception e ) {
101+ throw new IllegalStateException (
102+ String .format ("Failed to create DirectByteBuffer: %s" , e .getMessage ()), e );
103+ }
104+ }
105+ };
106+ }
107+
108+ private interface ByteBufferCreator {
109+ ByteBuffer newDirectByteBuffer (long addr , int size , Object att );
67110 }
68111
69112
0 commit comments