Skip to content

Commit

Permalink
ARROW-16537: [Java] Patch dataset module testing failure with JSE11+
Browse files Browse the repository at this point in the history
Patch dataset module testing failure with JSE11+

Closes #13200 from davisusanibar/ARROW-16537

Lead-authored-by: david dali susanibar arce <davi.sarces@gmail.com>
Co-authored-by: David Li <li.davidm96@gmail.com>
Signed-off-by: David Li <li.davidm96@gmail.com>
  • Loading branch information
davisusanibar and lidavidm committed May 23, 2022
1 parent d4a7638 commit 442b24b
Showing 1 changed file with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class DirectReservationListener implements ReservationListener {
private DirectReservationListener() {
try {
final Class<?> classBits = Class.forName("java.nio.Bits");
methodReserve = classBits.getDeclaredMethod("reserveMemory", long.class, int.class);
methodReserve = this.getDeclaredMethodBaseOnJDKVersion(classBits, "reserveMemory");
methodReserve.setAccessible(true);
methodUnreserve = classBits.getDeclaredMethod("unreserveMemory", long.class, int.class);
methodUnreserve = this.getDeclaredMethodBaseOnJDKVersion(classBits, "unreserveMemory");
methodUnreserve.setAccessible(true);
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -87,11 +87,38 @@ public void unreserve(long size) {
public long getCurrentDirectMemReservation() {
try {
final Class<?> classBits = Class.forName("java.nio.Bits");
final Field f = classBits.getDeclaredField("reservedMemory");
Field f;
try {
f = classBits.getDeclaredField("reservedMemory");
} catch (NoSuchFieldException e) {
try {
f = classBits.getDeclaredField("RESERVED_MEMORY");
} catch (NoSuchFieldException ex) {
throw new AssertionError(ex);
}
}
f.setAccessible(true);
return ((AtomicLong) f.get(null)).get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* Get the given method via reflection, searching for different signatures based on the Java version.
* @param classBits The java.nio.Bits class.
* @param name The method being requested.
* @return The method object.
*/
private Method getDeclaredMethodBaseOnJDKVersion(Class<?> classBits, String name) {
try {
return classBits.getDeclaredMethod(name, long.class, int.class);
} catch (NoSuchMethodException e) {
try {
return classBits.getDeclaredMethod(name, long.class, long.class);
} catch (NoSuchMethodException ex) {
throw new AssertionError(ex);
}
}
}
}

0 comments on commit 442b24b

Please sign in to comment.