-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed direct-mapping type-mapped pointer result types #319
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,12 @@ | |
|
||
package com.sun.jna; | ||
|
||
import java.awt.Point; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.lang.reflect.Field; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import junit.framework.TestCase; | ||
|
@@ -156,6 +158,48 @@ public void testIntegerToBooleanResultConversion() throws Exception { | |
assertFalse("Failed to convert integer return to boolean FALSE", | ||
lib.returnInt32Argument(false)); | ||
} | ||
public static class DirectTypeMappedResultTypeTestLibrary { | ||
public native Point returnPoint(int x, int y); | ||
static { | ||
Map options = new HashMap(); | ||
DefaultTypeMapper mapper = new DefaultTypeMapper(); | ||
mapper.addTypeConverter(Point.class, new TypeConverter() { | ||
public Object fromNative(Object value, FromNativeContext context) { | ||
Pointer p = (Pointer) value; | ||
int x = p.getInt(0), y = p.getInt(4); | ||
Native.free(Pointer.nativeValue(p)); | ||
return new Point(x, y); | ||
} | ||
public Object toNative(Object value, ToNativeContext context) { | ||
return Pointer.NULL; // dummy implementation (not called) | ||
} | ||
public Class nativeType() { | ||
return Pointer.class; | ||
} | ||
}); | ||
options.put(Library.OPTION_TYPE_MAPPER, mapper); | ||
|
||
// Can't extend java.awt.Point; can't add: | ||
// public final static TypeMapper TYPE_MAPPER = mapper; | ||
// -> Extend Native.options via reflection: | ||
try { | ||
Field f = Native.class.getDeclaredField("options"); | ||
f.setAccessible(true); | ||
((Map) f.get(null)).put(Point.class, options); | ||
} | ||
catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
Native.register(NativeLibrary.getInstance("testlib", options)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
} | ||
} | ||
public void testTypeMapperResultTypeConversion() throws Exception { | ||
DirectTypeMappedResultTypeTestLibrary lib = new DirectTypeMappedResultTypeTestLibrary(); | ||
Point p = lib.returnPoint(1234, 5678); | ||
assertEquals("Failed to convert int* return to java.awt.Point", 1234, p.x); | ||
assertEquals("Failed to convert int* return to java.awt.Point", 5678, p.y); | ||
} | ||
|
||
public static void main(String[] args) { | ||
junit.textui.TestRunner.run(DirectTypeMapperTest.class); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Were these other issues you ran into? Could you please elaborate?