Skip to content
Permalink
Browse files
Consolidation of all the Reflection boilerplate into one class.
  • Loading branch information
leerho committed May 11, 2021
1 parent 9ac70eb commit c26a0a4fde81c4598732551eb4c4c4ea5e8003db
Show file tree
Hide file tree
Showing 8 changed files with 354 additions and 377 deletions.
@@ -25,8 +25,6 @@
import static org.testng.Assert.fail;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteOrder;

import org.apache.datasketches.memory.MemoryRequestServer;
import org.apache.datasketches.memory.Util;
@@ -39,34 +37,6 @@
@SuppressWarnings("javadoc")
public class AllocateDirectMemoryTest {

static final Method CHECK_VALID;
static final Method WRAP_DIRECT;
static final Method GET_CURRENT_DIRECT_MEMORY_ALLOCATIONS;

static {
CHECK_VALID =
ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "checkValid", (Class<?>[])null); //not static
WRAP_DIRECT =
ReflectUtil.getMethod(ReflectUtil.BASE_WRITABLE_MEMORY_IMPL,
"wrapDirect", long.class, ByteOrder.class, MemoryRequestServer.class); //static method
GET_CURRENT_DIRECT_MEMORY_ALLOCATIONS =
ReflectUtil.getMethod(ReflectUtil.BASE_STATE,
"getCurrentDirectMemoryAllocations", (Class<?>[])null); //static method
}

private static long getCurrentDirectMemoryAllocations() {
try {
return (long) GET_CURRENT_DIRECT_MEMORY_ALLOCATIONS.invoke(null);
} catch (Exception e) { throw new RuntimeException(e); }
}

private static WritableDirectHandle wrapDirect(final long capacityBytes,
final ByteOrder byteOrder, final MemoryRequestServer memReqSvr) {
try {
return (WritableDirectHandle) WRAP_DIRECT.invoke(null, capacityBytes, byteOrder, memReqSvr);
} catch (Exception e) { throw new RuntimeException(e); }
}

@Test
public void simpleAllocateDirect() {
int longs = 32;
@@ -79,17 +49,15 @@ public void simpleAllocateDirect() {
}
//inside the TWR block the memory should be valid
try {
CHECK_VALID.invoke(wMem);
//wMem.checkValid();
ReflectUtil.checkValid(wMem);
//OK
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
//The TWR block has exited, so the memory should be invalid
try {
CHECK_VALID.invoke(wMem);
//wMem.checkValid();
ReflectUtil.checkValid(wMem);
fail();
} catch (final Exception e) {
if (e instanceof IllegalStateException || e instanceof InvocationTargetException) { } //OK
@@ -136,7 +104,7 @@ public void checkNullMemoryRequestServer() {

@Test
public void checkNonNativeDirect() { //not allowed in public API
try (WritableDirectHandle h = wrapDirect(8, Util.nonNativeByteOrder, null)) {
try (WritableDirectHandle h = ReflectUtil.wrapDirect(8, Util.nonNativeByteOrder, null)) {
//BaseWritableMemoryImpl.wrapDirect(8, Util.nonNativeByteOrder, null)) {
WritableMemory wmem = h.get();
wmem.putChar(0, (char) 1);
@@ -154,7 +122,7 @@ public void checkExplicitClose() {

@AfterClass
public void checkDirectCounter() {
long count = getCurrentDirectMemoryAllocations();
long count = ReflectUtil.getCurrentDirectMemoryAllocations();
//final long count = BaseState.getCurrentDirectMemoryAllocations();
if (count != 0) {
println(""+count);
@@ -24,7 +24,6 @@
package org.apache.datasketches.memory.test;

import static java.nio.charset.StandardCharsets.UTF_8;
//import static org.apache.datasketches.memory.AllocateDirectMap.isFileReadOnly;
import static org.apache.datasketches.memory.Util.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
@@ -25,7 +25,6 @@
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.lang.reflect.Method;
import java.nio.ByteOrder;

import org.apache.datasketches.memory.Buffer;
@@ -39,19 +38,6 @@

@SuppressWarnings("javadoc")
public class BaseStateTest {

static final Method GET_NATIVE_BASE_OFFSET;

static {
GET_NATIVE_BASE_OFFSET =
ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "getNativeBaseOffset", (Class<?>[])null);
}

private static long getNativeBaseOffset(final Object owner) {
try {
return (long) GET_NATIVE_BASE_OFFSET.invoke(owner);
} catch (Exception e) { throw new RuntimeException(e); }
}

@Test
public void checkPrimOffset() {
@@ -105,7 +91,7 @@ public void checkPrim() {
@Test
public void checkGetNativeBaseOffset_Heap() throws Exception {
WritableMemory wmem = WritableMemory.allocate(8); //heap
final long offset = getNativeBaseOffset(wmem);
final long offset = ReflectUtil.getNativeBaseOffset(wmem);
assertEquals(offset, 0L);
}

@@ -27,7 +27,6 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@@ -45,13 +44,6 @@
public class LeafImplTest {
static final ByteOrder LE = ByteOrder.LITTLE_ENDIAN;
static final ByteOrder BE = ByteOrder.BIG_ENDIAN;

static final Method GET_UNSAFE_OBJECT;

static {
GET_UNSAFE_OBJECT = ReflectUtil.getMethod(ReflectUtil.BASE_STATE, "getUnsafeObject", (Class<?>[])null); //not static
}


@Test
public void checkDirectLeafs() {
@@ -63,12 +55,6 @@ public void checkDirectLeafs() {
checkDirectImpl(memLE, off, cap);
}
}

private static Object getUnsafeObject(Object target) {
try {
return GET_UNSAFE_OBJECT.invoke(target);
} catch (Exception e) { throw new RuntimeException(e); }
}

private static void checkDirectImpl(WritableMemory mem, long off, long cap) {
assertEquals(mem.writableRegion(off, cap, LE).getShort(0), 1);
@@ -80,7 +66,7 @@ private static void checkDirectImpl(WritableMemory mem, long off, long cap) {
assertTrue(mem.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(mem.getMemoryRequestServer() != null);
assertTrue(mem.isDirect());
assertTrue(getUnsafeObject(mem) == null);
assertTrue(ReflectUtil.getUnsafeObject(mem) == null);
//assertTrue(mem.getUnsafeObject() == null);
assertTrue(mem.isValid() == true);

@@ -95,7 +81,7 @@ private static void checkDirectImpl(WritableMemory mem, long off, long cap) {
assertTrue(buf.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(buf.getMemoryRequestServer() != null);
assertTrue(buf.isDirect());
assertTrue(getUnsafeObject(buf) == null);
assertTrue(ReflectUtil.getUnsafeObject(buf) == null);
//assertTrue(buf.getUnsafeObject() == null);
assertTrue(buf.isValid() == true);

@@ -110,7 +96,7 @@ private static void checkDirectImpl(WritableMemory mem, long off, long cap) {
assertTrue(nnMem.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnMem.getMemoryRequestServer() != null);
assertTrue(nnMem.isDirect());
assertTrue(getUnsafeObject(nnMem) == null);
assertTrue(ReflectUtil.getUnsafeObject(nnMem) == null);
//assertTrue(nnMem.getUnsafeObject() == null);
assertTrue(nnMem.isValid() == true);

@@ -125,7 +111,7 @@ private static void checkDirectImpl(WritableMemory mem, long off, long cap) {
assertTrue(nnBuf.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnBuf.getMemoryRequestServer() != null);
assertTrue(nnBuf.isDirect());
assertTrue(getUnsafeObject(nnBuf) == null);
assertTrue(ReflectUtil.getUnsafeObject(nnBuf) == null);
//assertTrue(nnBuf.getUnsafeObject() == null);
assertTrue(nnBuf.isValid() == true);
}
@@ -165,7 +151,7 @@ private static void checkMapImpl(WritableMemory mem, long off, long cap) {
assertTrue(mem.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(mem.getMemoryRequestServer() == null);
assertTrue(mem.isDirect());
assertTrue(getUnsafeObject(mem) == null);
assertTrue(ReflectUtil.getUnsafeObject(mem) == null);
//assertTrue(mem.getUnsafeObject() == null);
assertTrue(mem.isValid() == true);

@@ -180,7 +166,7 @@ private static void checkMapImpl(WritableMemory mem, long off, long cap) {
assertTrue(buf.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(buf.getMemoryRequestServer() == null);
assertTrue(buf.isDirect());
assertTrue(getUnsafeObject(buf) == null);
assertTrue(ReflectUtil.getUnsafeObject(buf) == null);
//assertTrue(buf.getUnsafeObject() == null);
assertTrue(buf.isValid() == true);

@@ -195,7 +181,7 @@ private static void checkMapImpl(WritableMemory mem, long off, long cap) {
assertTrue(nnMem.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnMem.getMemoryRequestServer() == null);
assertTrue(nnMem.isDirect());
assertTrue(getUnsafeObject(nnMem) == null);
assertTrue(ReflectUtil.getUnsafeObject(nnMem) == null);
//assertTrue(nnMem.getUnsafeObject() == null);
assertTrue(nnMem.isValid() == true);

@@ -210,7 +196,7 @@ private static void checkMapImpl(WritableMemory mem, long off, long cap) {
assertTrue(nnBuf.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnBuf.getMemoryRequestServer() == null);
assertTrue(nnBuf.isDirect());
assertTrue(getUnsafeObject(nnBuf) == null);
assertTrue(ReflectUtil.getUnsafeObject(nnBuf) == null);
//assertTrue(nnBuf.getUnsafeObject() == null);
assertTrue(nnBuf.isValid() == true);
}
@@ -241,7 +227,7 @@ private static void checkByteBufferImpl(WritableMemory mem, long off, long cap,
assertTrue(mem.getByteBuffer() != null);
assertTrue(mem.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(mem.getMemoryRequestServer() == null);
Object obj = getUnsafeObject(mem);
Object obj = ReflectUtil.getUnsafeObject(mem);
//Object obj = mem.getUnsafeObject();
if (direct) {
assertTrue(mem.isDirect());
@@ -262,7 +248,7 @@ private static void checkByteBufferImpl(WritableMemory mem, long off, long cap,
assertTrue(buf.getByteBuffer() != null);
assertTrue(buf.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(buf.getMemoryRequestServer() == null);
obj = getUnsafeObject(buf);
obj = ReflectUtil.getUnsafeObject(buf);
//obj = buf.getUnsafeObject();
if (direct) {
assertTrue(buf.isDirect());
@@ -283,7 +269,7 @@ private static void checkByteBufferImpl(WritableMemory mem, long off, long cap,
assertTrue(nnMem.getByteBuffer() != null);
assertTrue(nnMem.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnMem.getMemoryRequestServer() == null);
obj = getUnsafeObject(nnMem);
obj = ReflectUtil.getUnsafeObject(nnMem);
//obj = nnMem.getUnsafeObject();
if (direct) {
assertTrue(nnMem.isDirect());
@@ -304,7 +290,7 @@ private static void checkByteBufferImpl(WritableMemory mem, long off, long cap,
assertTrue(nnBuf.getByteBuffer() != null);
assertTrue(nnBuf.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnBuf.getMemoryRequestServer() == null);
obj = getUnsafeObject(nnBuf);
obj = ReflectUtil.getUnsafeObject(nnBuf);
//obj = nnBuf.getUnsafeObject();
if (direct) {
assertTrue(nnBuf.isDirect());
@@ -335,7 +321,7 @@ private static void checkHeapImpl(WritableMemory mem, long off, long cap) {
assertTrue(mem.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(mem.getMemoryRequestServer() == null);
assertFalse(mem.isDirect());
assertTrue(getUnsafeObject(mem) != null);
assertTrue(ReflectUtil.getUnsafeObject(mem) != null);
//assertTrue(mem.getUnsafeObject() != null);
assertTrue(mem.isValid() == true);

@@ -350,7 +336,7 @@ private static void checkHeapImpl(WritableMemory mem, long off, long cap) {
assertTrue(buf.getTypeByteOrder() == Util.nativeByteOrder);
assertTrue(buf.getMemoryRequestServer() == null);
assertFalse(buf.isDirect());
assertTrue(getUnsafeObject(buf) != null);
assertTrue(ReflectUtil.getUnsafeObject(buf) != null);
//assertTrue(buf.getUnsafeObject() != null);
assertTrue(buf.isValid() == true);

@@ -365,7 +351,7 @@ private static void checkHeapImpl(WritableMemory mem, long off, long cap) {
assertTrue(nnMem.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnMem.getMemoryRequestServer() == null);
assertFalse(nnMem.isDirect());
assertTrue(getUnsafeObject(nnMem) != null);
assertTrue(ReflectUtil.getUnsafeObject(nnMem) != null);
//assertTrue(nnMem.getUnsafeObject() != null);
assertTrue(nnMem.isValid() == true);

@@ -380,7 +366,7 @@ private static void checkHeapImpl(WritableMemory mem, long off, long cap) {
assertTrue(nnBuf.getTypeByteOrder() == Util.nonNativeByteOrder);
assertTrue(nnBuf.getMemoryRequestServer() == null);
assertFalse(nnBuf.isDirect());
assertTrue(getUnsafeObject(nnBuf) != null);
assertTrue(ReflectUtil.getUnsafeObject(nnBuf) != null);
//assertTrue(nnBuf.getUnsafeObject() != null);
assertTrue(nnBuf.isValid() == true);
}

0 comments on commit c26a0a4

Please sign in to comment.