Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 45 additions & 22 deletions src/main/java/com/jme3/bullet/NpoTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -89,30 +91,16 @@ void freeTrackedObject() {
* Remove this tracker from the map BEFORE freeing the native object.
*/
NativePhysicsObject.removeTracker(id);

Class<?> c;
for (c = referentClass; c != Object.class; c = c.getSuperclass()) {
Method method;
Method[] methods = FreeingMethods.of(referentClass);
// Avoid re-boxing if there is more than one method to call.
Object[] boxedId = new Object[]{ Long.valueOf(id) };
for (int i = 0; i < methods.length; ++i) {
Method method = methods[i];
try {
method = c.getDeclaredMethod("freeNativeObject", long.class);

} catch (IllegalArgumentException
| NoClassDefFoundError exception) {
System.out.println("c = " + c.getName());
throw new RuntimeException(exception);

} catch (NoSuchMethodException exception) {
continue;
}

try {
method.setAccessible(true);
method.invoke(null, id);

method.invoke(null, boxedId);
++invocationCount;
} catch (IllegalAccessException | IllegalArgumentException
| SecurityException | InvocationTargetException exception) {
throw new RuntimeException(exception);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

Expand All @@ -132,5 +120,40 @@ public String toString() {
result += "_" + Long.toHexString(id);

return result;
}
// *************************************************************************
// Freeing methods cache

/**
* Simple cache so we don't have to traverse the class hierarchy and reflect
* each time.
*/
private static final class FreeingMethods {
private static final ConcurrentHashMap<Class<?>, Method[]> methodsByClass = new ConcurrentHashMap<>();

private static final Method[] generate(Class<?> clazz) {
ArrayList<Method> methods = new ArrayList<>();
for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) {
try {
Method method = c.getDeclaredMethod("freeNativeObject", long.class);
method.setAccessible(true);
methods.add(method);
} catch (IllegalArgumentException | NoClassDefFoundError | SecurityException e) {
System.out.println("c = " + c.getName());
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
continue;
}
}
return methods.toArray(new Method[methods.size()]);
}

public static final Method[] of(Class<?> clazz) {
Method[] methods = methodsByClass.get(clazz);
if (methods == null) {
methodsByClass.put(clazz, methods = generate(clazz));
}
return methods;
}
}
}