@@ -36,29 +36,39 @@ static boolean isAvailable() {
3636 theUnsafe .setAccessible (true );
3737 Object instance = theUnsafe .get (null );
3838 if (instance .getClass () == clazz ) {
39- // Validate that this Unsafe instance exposes all methods we need
40- clazz . getDeclaredMethod ( "getByte " , Object .class , long .class );
41- clazz . getDeclaredMethod ( "putByte " , Object .class , long . class , byte .class );
42- clazz . getDeclaredMethod ( "getShort " , Object .class , long .class );
43- clazz . getDeclaredMethod ( "putShort " , Object .class , long . class , short .class );
44- clazz . getDeclaredMethod ( "getInt " , Object .class , long .class );
45- clazz . getDeclaredMethod ( "putInt " , Object .class , long . class , int .class );
46- clazz . getDeclaredMethod ( "getLong " , Object .class , long .class );
47- clazz . getDeclaredMethod ( "putLong " , Object . class , long .class , long .class );
48- clazz . getDeclaredMethod ( "getFloat " , Object .class , long .class );
49- clazz . getDeclaredMethod ( "putFloat " , Object .class , long . class , float .class );
50- clazz . getDeclaredMethod ( "getDouble " , Object .class , long .class );
51- clazz . getDeclaredMethod ( "putDouble " , Object .class , long . class , double .class );
52- clazz . getDeclaredMethod ( "getBoolean " , Object .class , long .class );
53- clazz . getDeclaredMethod ( "putBoolean " , Object .class , long .class , boolean .class );
54- clazz . getDeclaredMethod ( "copyMemory " , Object . class , long . class , Object . class , long . class , long .class );
55- clazz . getDeclaredMethod ( "arrayBaseOffset " , Class .class );
56- clazz . getDeclaredMethod ( "arrayIndexScale" , Class . class );
39+ checkMethod ( clazz , "getByte" , Object . class , long . class );
40+ checkMethod ( clazz , "putByte " , Object .class , long . class , byte .class );
41+ checkMethod ( clazz , "getShort " , Object .class , long .class );
42+ checkMethod ( clazz , "putShort " , Object .class , long . class , short .class );
43+ checkMethod ( clazz , "getInt " , Object .class , long .class );
44+ checkMethod ( clazz , "putInt " , Object .class , long . class , int .class );
45+ checkMethod ( clazz , "getLong " , Object .class , long .class );
46+ checkMethod ( clazz , "putLong " , Object . class , long .class , long .class );
47+ checkMethod ( clazz , "getFloat " , Object .class , long .class );
48+ checkMethod ( clazz , "putFloat " , Object .class , long . class , float .class );
49+ checkMethod ( clazz , "getDouble " , Object .class , long .class );
50+ checkMethod ( clazz , "putDouble " , Object .class , long . class , double .class );
51+ checkMethod ( clazz , "getBoolean " , Object .class , long .class );
52+ checkMethod ( clazz , "putBoolean " , Object .class , long . class , boolean .class );
53+ checkMethod ( clazz , "copyMemory " , Object .class , long .class , Object . class , long . class , long .class );
54+ checkMethod ( clazz , "arrayBaseOffset " , Class .class );
55+ checkMethod ( clazz , "arrayIndexScale " , Class .class );
56+
5757 unsafe = (Unsafe ) instance ;
5858 }
5959 } catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | SecurityException | IllegalAccessException | ClassCastException ex ) {
6060 // Do nothing, keep unsafe as null
6161 }
6262 UNSAFE = unsafe ;
6363 }
64+
65+ /**
66+ * Validate that this Unsafe instance exposes this method
67+ *
68+ * ErrorProne does not like that we do nothing with the returned method... but there is nothing to do with it, so disable the check
69+ */
70+ @ SuppressWarnings ("ReturnValueIgnored" )
71+ private static void checkMethod (Class <?> unsafeClass , String methodName , Class <?>... parameterTypes ) throws NoSuchMethodException {
72+ unsafeClass .getDeclaredMethod (methodName , parameterTypes );
73+ }
6474}
0 commit comments