From 7c3db24f579f84cce413bae8eb3fa8382f2b144c Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Tue, 3 Sep 2019 19:13:32 -0700 Subject: [PATCH] Fix Boolean, CFIndex, and Pointer typing --- .../sun/jna/platform/mac/CoreFoundation.java | 35 ++++++++----------- .../jna/platform/mac/CoreFoundationUtil.java | 2 +- .../jna/platform/mac/CoreFoundationTest.java | 16 ++++----- .../com/sun/jna/platform/mac/IOKitTest.java | 10 +++--- 4 files changed, 29 insertions(+), 34 deletions(-) diff --git a/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java b/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java index bf29f779e6..d9f9e96f1d 100644 --- a/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java +++ b/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java @@ -54,14 +54,8 @@ public interface CoreFoundation extends Library { int kCFNotFound = -1; - int kCFStringEncodingMacRoman = 0; - int kCFStringEncodingWindowsLatin1 = 0x0500; - int kCFStringEncodingISOLatin1 = 0x0201; - int kCFStringEncodingNextStepLatin = 0x0B01; int kCFStringEncodingASCII = 0x0600; - int kCFStringEncodingUnicode = 0x0100; int kCFStringEncodingUTF8 = 0x08000100; - int kCFStringEncodingNonLossyASCII = 0x0BFF; /** * The {@code CFTypeRef} type is the base type defined in Core Foundation. It is @@ -267,7 +261,7 @@ public static CFStringRef toCFString(String s) { * A pointer to the value for the returned number object. * @return A new number with the value specified by {@code valuePtr}. */ - CFNumberRef CFNumberCreate(CFAllocatorRef alloc, int theType, PointerType valuePtr); + CFNumberRef CFNumberCreate(CFAllocatorRef alloc, long theType, PointerType valuePtr); /** * Creates a new immutable array with the given values. @@ -475,9 +469,9 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac * value from the dictionary is not returned (but the return value of * this function still indicates whether or not the key-value pair * was present). - * @return {@code true} if a matching key was found, otherwise {@code false}. + * @return 1 if a matching key was found, otherwise 0. */ - boolean CFDictionaryGetValueIfPresent(CFDictionaryRef theDict, PointerType key, PointerByReference value); + byte CFDictionaryGetValueIfPresent(CFDictionaryRef theDict, PointerType key, PointerByReference value); /** * Sets the value corresponding to a given key. @@ -525,19 +519,19 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac * The string encoding to which the character contents of * {@code theString} should be converted. The encoding must specify * an 8-bit encoding. - * @return {@code true} upon success or {@code false} if the conversion fails or - * the provided buffer is too small. + * @return 1 upon success or 0 if the conversion fails or the provided buffer is + * too small. */ - boolean CFStringGetCString(CFTypeRef theString, Pointer bufferToFill, long bufferSize, int encoding); + byte CFStringGetCString(CFTypeRef theString, Pointer bufferToFill, long bufferSize, int encoding); /** * Returns the value of a {@code CFBoolean} object. * * @param bool * The boolean to examine. - * @return The value of {@code bool}. + * @return 1 if the value of {@code bool} is {@code true}, 0 otherwise. */ - boolean CFBooleanGetValue(CFTypeRef bool); + byte CFBooleanGetValue(CFTypeRef bool); /** * Retrieves a value at a given index. @@ -560,7 +554,7 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac * @return A constant that indicates the data type of the value contained in * number. See {@link CFNumberType} for a list of possible values. */ - int CFNumberGetType(CFNumberRef number); + long CFNumberGetType(CFNumberRef number); /** * Obtains the value of a {@code CFNumber} object cast to a specified type. @@ -572,10 +566,9 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac * {@link CFNumberType} for a list of possible values. * @param On * return, contains the value of {@code number}. - * @return {@code true} if the operation was successful, otherwise - * {@code false}. + * @return 1 if the operation was successful, otherwise 0. */ - boolean CFNumberGetValue(CFNumberRef number, int theType, ByReference valuePtr); + byte CFNumberGetValue(CFNumberRef number, long theType, ByReference valuePtr); /** * Returns the number (in terms of UTF-16 code pairs) of Unicode characters in a @@ -597,7 +590,9 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac * @param encoding * The string encoding for the number of characters specified by * length. - * @return a long. + * @return The maximum number of bytes that could be needed to represent length + * number of Unicode characters with the string encoding encoding, or + * {@link #kCFNotFound} if the number exceeds {@link Long#MAX_VALUE}. */ long CFStringGetMaximumSizeForEncoding(long length, int encoding); @@ -628,5 +623,5 @@ CFMutableDictionaryRef CFDictionaryCreateMutable(CFAllocatorRef alloc, int capac * The {@code CFData} object to examine. * @return A read-only pointer to the bytes associated with {@code theData}. */ - PointerByReference CFDataGetBytePtr(CFDataRef theData); + Pointer CFDataGetBytePtr(CFDataRef theData); } diff --git a/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundationUtil.java b/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundationUtil.java index 3d4f997cd0..bc259462e8 100644 --- a/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundationUtil.java +++ b/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundationUtil.java @@ -91,7 +91,7 @@ public static int cfPointerToInt(CFNumberRef theInt) { * @return The corresponding {@code boolean} */ public static boolean cfPointerToBoolean(CFBooleanRef theBoolean) { - return CF.CFBooleanGetValue(theBoolean); + return 0 != CF.CFBooleanGetValue(theBoolean); } /** diff --git a/contrib/platform/test/com/sun/jna/platform/mac/CoreFoundationTest.java b/contrib/platform/test/com/sun/jna/platform/mac/CoreFoundationTest.java index cc2732d269..cf1dc83310 100644 --- a/contrib/platform/test/com/sun/jna/platform/mac/CoreFoundationTest.java +++ b/contrib/platform/test/com/sun/jna/platform/mac/CoreFoundationTest.java @@ -27,7 +27,7 @@ import static com.sun.jna.platform.mac.CoreFoundationUtil.release; import static com.sun.jna.platform.mac.CoreFoundationUtil.releaseAll; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -65,7 +65,7 @@ public void testCFStringRef() { Memory mem = new Memory(awesome.getBytes().length + 1); mem.clear(); - assertTrue(CF.CFStringGetCString(cfAwesome, mem, mem.size(), CoreFoundation.kCFStringEncodingUTF8)); + assertNotEquals(0, CF.CFStringGetCString(cfAwesome, mem, mem.size(), CoreFoundation.kCFStringEncodingUTF8)); byte[] awesomeBytes = mem.getByteArray(0, (int) mem.size() - 1); byte[] awesomeArr = awesome.getBytes(); for (int i = 0; i < awesomeArr.length; i++) { @@ -160,8 +160,8 @@ public void testCFData() { CFDataRef cfBug = CF.CFDataCreate(null, bugBytes, bugBytes.size()); assertEquals(bugBytes.size(), CF.CFDataGetLength(cfBug)); - PointerByReference bytes = CF.CFDataGetBytePtr(cfBug); - assertEquals(deadBug, bytes.getPointer().getString(0)); + Pointer bytes = CF.CFDataGetBytePtr(cfBug); + assertEquals(deadBug, bytes.getString(0)); release(cfBug); } @@ -173,13 +173,13 @@ public void testCFDictionary() { CFStringRef oneStr = CFStringRef.toCFString("one"); // Key does not exist, returns null - assertFalse(CF.CFDictionaryGetValueIfPresent(dict, oneStr, null)); + assertEquals(0, CF.CFDictionaryGetValueIfPresent(dict, oneStr, null)); Pointer cfNull = CF.CFDictionaryGetValue(dict, oneStr); assertNull(cfNull); // Store and retrieve null value CF.CFDictionarySetValue(dict, oneStr, null); - assertTrue(CF.CFDictionaryGetValueIfPresent(dict, oneStr, null)); + assertNotEquals(0, CF.CFDictionaryGetValueIfPresent(dict, oneStr, null)); Pointer cfNullValue = CF.CFDictionaryGetValue(dict, oneStr); assertNull(cfNullValue); @@ -188,13 +188,13 @@ public void testCFDictionary() { CFNumberRef cfOne = CF.CFNumberCreate(null, CFNumberType.kCFNumberIntType.ordinal(), one); CF.CFDictionarySetValue(dict, oneStr, cfOne); - assertTrue(CF.CFDictionaryGetValueIfPresent(dict, oneStr, null)); + assertNotEquals(0, CF.CFDictionaryGetValueIfPresent(dict, oneStr, null)); Pointer result = CF.CFDictionaryGetValue(dict, oneStr); CFNumberRef numRef = new CFNumberRef(result); assertEquals(1, CoreFoundationUtil.cfPointerToInt(numRef)); PointerByReference resultPtr = new PointerByReference(); - assertTrue(CF.CFDictionaryGetValueIfPresent(dict, oneStr, resultPtr)); + assertNotEquals(0, CF.CFDictionaryGetValueIfPresent(dict, oneStr, resultPtr)); numRef = new CFNumberRef(resultPtr.getValue()); assertEquals(1, CoreFoundationUtil.cfPointerToInt(numRef)); diff --git a/contrib/platform/test/com/sun/jna/platform/mac/IOKitTest.java b/contrib/platform/test/com/sun/jna/platform/mac/IOKitTest.java index 2449d7440b..91cd73be04 100644 --- a/contrib/platform/test/com/sun/jna/platform/mac/IOKitTest.java +++ b/contrib/platform/test/com/sun/jna/platform/mac/IOKitTest.java @@ -105,7 +105,7 @@ public void testMatching() { IO.IORegistryEntryCreateCFProperties(platformExpert, properties, CF.CFAllocatorGetDefault(), 0)); dict = new CFMutableDictionaryRef(); dict.setPointer(properties.getValue()); - assertTrue(CF.CFDictionaryGetValueIfPresent(dict, serialKey, null)); + assertNotEquals(0, CF.CFDictionaryGetValueIfPresent(dict, serialKey, null)); result = CF.CFDictionaryGetValue(dict, serialKey); cfSerial = new CFStringRef(result); assertEquals(serialNumber, CoreFoundationUtil.cfPointerToString(cfSerial)); @@ -250,16 +250,16 @@ public void testPowerSources() { // Get values from dictionary (See IOPSKeys.h) // Skip if not present PointerByReference result = new PointerByReference(); - if (CF.CFDictionaryGetValueIfPresent(dictionary, isPresentKey, result)) { + if (0 != CF.CFDictionaryGetValueIfPresent(dictionary, isPresentKey, result)) { CFBooleanRef isPresentRef = new CFBooleanRef(result.getValue()); - if (CF.CFBooleanGetValue(isPresentRef)) { + if (CoreFoundationUtil.cfPointerToBoolean(isPresentRef)) { int currentCapacity = 0; - if (CF.CFDictionaryGetValueIfPresent(dictionary, currentCapacityKey, result)) { + if (0 != CF.CFDictionaryGetValueIfPresent(dictionary, currentCapacityKey, result)) { CFNumberRef cap = new CFNumberRef(result.getValue()); currentCapacity = CoreFoundationUtil.cfPointerToInt(cap); } int maxCapacity = 100; - if (CF.CFDictionaryGetValueIfPresent(dictionary, maxCapacityKey, result)) { + if (0 != CF.CFDictionaryGetValueIfPresent(dictionary, maxCapacityKey, result)) { CFNumberRef cap = new CFNumberRef(result.getValue()); maxCapacity = CoreFoundationUtil.cfPointerToInt(cap); }