Skip to content

Commit

Permalink
auto-strip profiler prefix set in jna.profiler.prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
twall committed May 29, 2013
1 parent c596f9f commit 3704b23
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Features
* Added memory dump for debugging (see `com.sun.jna.Memory`) - [@twall](https://github.com/twall).
* Improved caching of Structure alignment, type mapping, and encoding information - [@twall](https://github.com/twall).
* [#225](https://github.com/twall/jna/pull/225): Added `platform.win32.Kernel32.GetLogicalProcessorInformation` and `platform.win32.Kernel32Util.getLogicalProcessorInformation` - [@trejkaz](https://github.com/trejkaz).
* [#236](https://github.com/twall/jna/issues/236): Auto-strip profiler native method prefix specified by `jna.profiler.prefix`, which defaults to $$YJP$$ - [@twall](https://github.com/twall).

Bug Fixes
---------
Expand Down
9 changes: 3 additions & 6 deletions src/com/sun/jna/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -1454,12 +1454,7 @@ else if (cvt[t] == CVT_TYPE_MAPPER) {
}
}

String name = method.getName();
FunctionMapper fmapper = (FunctionMapper)lib.getOptions().get(Library.OPTION_FUNCTION_MAPPER);
if (fmapper != null) {
name = fmapper.getFunctionName(lib, method);
}
Function f = lib.getFunction(name, method);
Function f = lib.getFunction(method.getName(), method);
try {
handles[i] = registerMethod(cls, method.getName(),
sig, cvt,
Expand Down Expand Up @@ -1842,6 +1837,8 @@ static String getString(long addr, String encoding) {
<em>Warning</em>: avoid calling {@link #detach detach(true)} on threads
spawned by the JVM; the resulting behavior is not defined.
*/
// TODO: keep references to Java non-detached threads, and clear them when
// native side sets a flag saying they're detached (cleanup)
public static native void detach(boolean detach);

private static class Buffers {
Expand Down
5 changes: 5 additions & 0 deletions src/com/sun/jna/NativeLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ Function getFunction(String name, Method method) {
if (mapper != null) {
name = mapper.getFunctionName(this, method);
}
// If there's native method profiler prefix, strip it
String prefix = System.getProperty("jna.profiler.prefix", "$$YJP$$");
if (name.startsWith(prefix)) {
name = name.substring(prefix.length());
}
int flags = this.callFlags;
Class[] etypes = method.getExceptionTypes();
for (int i=0;i < etypes.length;i++) {
Expand Down
8 changes: 7 additions & 1 deletion test/com/sun/jna/DirectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ public void testGetOptionsForDirectMappingWithStaticInitializer() {
}

static class RemappedCLibrary {
public static native int $$YJP$$strlen(String s);
public static native int _prefixed_strlen(String s);
}

Expand All @@ -283,7 +284,12 @@ public String getFunctionName(NativeLibrary lib, Method method) {
Native.register(RemappedCLibrary.class,
NativeLibrary.getInstance(Platform.C_LIBRARY_NAME, options));
final String VALUE = getName();
int len = RemappedCLibrary._prefixed_strlen(VALUE);
int len;

len = RemappedCLibrary.$$YJP$$strlen(VALUE);
assertEquals(VALUE.length(), len);

len = RemappedCLibrary._prefixed_strlen(VALUE);
assertEquals(VALUE.length(), len);
}
catch(Exception e) {
Expand Down
3 changes: 3 additions & 0 deletions www/DirectMapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ Direct mapping supports the same type mappings as interface mapping, except for

You can easily convert from interface mapping to direct mapping by creating a direct mapping class which implements your library interface, with all methods defined as native methods. Then your library instance variable can be assigned an instance of this new class instead of the object returned by `Native.loadLibrary()`.

If you are using a profile which rewrites native methods, you may need to
set the system property `jna.profiler.prefix` to the prefix used by the
profiler to avoid linkage errors when binding to native methods.

0 comments on commit 3704b23

Please sign in to comment.