Skip to content

Commit 59c90b7

Browse files
committed
Return primitive types from convenience methods
1 parent a9db43b commit 59c90b7

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ public CFArrayRef(Pointer p) {
322322
*
323323
* @return The number of values in this array.
324324
*/
325-
public CFIndex getCount() {
326-
return INSTANCE.CFArrayGetCount(this);
325+
public int getCount() {
326+
return INSTANCE.CFArrayGetCount(this).intValue();
327327
}
328328

329329
/**
@@ -333,8 +333,8 @@ public CFIndex getCount() {
333333
* The index of the value to retrieve.
334334
* @return The value at the {@code idx} index.
335335
*/
336-
public Pointer getValueAtIndex(CFIndex idx) {
337-
return INSTANCE.CFArrayGetValueAtIndex(this, idx);
336+
public Pointer getValueAtIndex(int idx) {
337+
return INSTANCE.CFArrayGetValueAtIndex(this, new CFIndex(idx));
338338
}
339339
}
340340

@@ -359,8 +359,8 @@ public CFDataRef(Pointer p) {
359359
* @return An index that specifies the number of bytes associated with this
360360
* object.
361361
*/
362-
public CFIndex getLength() {
363-
return INSTANCE.CFDataGetLength(this);
362+
public int getLength() {
363+
return INSTANCE.CFDataGetLength(this).intValue();
364364
}
365365

366366
/**
@@ -402,18 +402,19 @@ public Pointer getValue(PointerType key) {
402402
}
403403

404404
/**
405-
* Convenience method for {@link CoreFoundation#CFDictionaryGetValueIfPresent}
406-
* on this object.
405+
* Convenience method for
406+
* {@link CoreFoundation#CFDictionaryGetValueIfPresent} on this object.
407407
*
408408
* @param key
409409
* The key for which to find a match.
410410
* @param value
411411
* A pointer to memory which, on return, is filled with the
412412
* pointer-sized value if a matching key is found.
413-
* @return 1 if a matching key was found, otherwise 0
413+
* @return {@code true} if a matching key was found, otherwise
414+
* {@code false}
414415
*/
415-
public byte getValueIfPresent(PointerType key, PointerByReference value) {
416-
return INSTANCE.CFDictionaryGetValueIfPresent(this, key, value);
416+
public boolean getValueIfPresent(PointerType key, PointerByReference value) {
417+
return INSTANCE.CFDictionaryGetValueIfPresent(this, key, value) > 0;
417418
}
418419
}
419420

contrib/platform/src/com/sun/jna/platform/mac/IOKit.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ public byte[] getByteArrayProperty(String key) {
398398
keyAsCFString.release();
399399
if (valueAsCFType != null) {
400400
CFDataRef valueAsCFData = new CFDataRef(valueAsCFType.getPointer());
401-
int length = valueAsCFData.getLength().intValue();
401+
int length = valueAsCFData.getLength();
402402
Pointer p = valueAsCFData.getBytePtr();
403403
value = p.getByteArray(0, length);
404404
valueAsCFType.release();

contrib/platform/test/com/sun/jna/platform/mac/CoreFoundationTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ public void testCFArray() {
141141
CFArrayRef cfPtrArray = CF.CFArrayCreate(null, contiguousArray, new CFIndex(refArray.length), null);
142142
assertEquals(CoreFoundation.ARRAY_TYPE_ID, cfPtrArray.getTypeID());
143143

144-
assertEquals(refArray.length, cfPtrArray.getCount().intValue());
144+
assertEquals(refArray.length, cfPtrArray.getCount());
145145
for (int i = 0; i < refArray.length; i++) {
146-
Pointer result = cfPtrArray.getValueAtIndex(new CFIndex(i));
146+
Pointer result = cfPtrArray.getValueAtIndex(i);
147147
try {
148148
new CFStringRef(result);
149149
fail("Should have thrown a ClassCastExcpetion.");
@@ -173,7 +173,7 @@ public void testCFData() {
173173
CFDataRef cfData = CF.CFDataCreate(null, nativeBytes, new CFIndex(size));
174174
assertEquals(CoreFoundation.DATA_TYPE_ID, cfData.getTypeID());
175175

176-
int dataSize = cfData.getLength().intValue();
176+
int dataSize = cfData.getLength();
177177
assertEquals(size, dataSize);
178178
// Read it back out and convert to an array
179179
Pointer bytes = cfData.getBytePtr();

contrib/platform/test/com/sun/jna/platform/mac/IOKitTest.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import com.sun.jna.platform.mac.CoreFoundation.CFArrayRef;
4242
import com.sun.jna.platform.mac.CoreFoundation.CFBooleanRef;
4343
import com.sun.jna.platform.mac.CoreFoundation.CFDictionaryRef;
44-
import com.sun.jna.platform.mac.CoreFoundation.CFIndex;
4544
import com.sun.jna.platform.mac.CoreFoundation.CFMutableDictionaryRef;
4645
import com.sun.jna.platform.mac.CoreFoundation.CFNumberRef;
4746
import com.sun.jna.platform.mac.CoreFoundation.CFStringRef;
@@ -108,8 +107,7 @@ public void testMatching() {
108107
// Get all the keys
109108
PointerByReference properties = new PointerByReference();
110109
assertEquals(0, platformExpert.createCFProperties(properties));
111-
dict = new CFMutableDictionaryRef();
112-
dict.setPointer(properties.getValue());
110+
dict = new CFMutableDictionaryRef(properties.getValue());
113111
assertNotEquals(0, dict.getValueIfPresent(serialKey, null));
114112
result = dict.getValue(serialKey);
115113
cfSerial = new CFStringRef(result);
@@ -243,27 +241,26 @@ public void testPowerSources() {
243241
CFStringRef isPresentKey = CFStringRef.createCFString("Is Present");
244242
CFStringRef currentCapacityKey = CFStringRef.createCFString("Current Capacity");
245243
CFStringRef maxCapacityKey = CFStringRef.createCFString("Max Capacity");
246-
int powerSourcesCount = powerSourcesList.getCount().intValue();
244+
int powerSourcesCount = powerSourcesList.getCount();
247245
for (int ps = 0; ps < powerSourcesCount; ps++) {
248246
// Get the dictionary for that Power Source
249-
Pointer pwrSrcPtr = powerSourcesList.getValueAtIndex(new CFIndex(ps));
250-
CFTypeRef powerSource = new CFTypeRef();
251-
powerSource.setPointer(pwrSrcPtr);
247+
Pointer pwrSrcPtr = powerSourcesList.getValueAtIndex(ps);
248+
CFTypeRef powerSource = new CFTypeRef(pwrSrcPtr);
252249
CFDictionaryRef dictionary = IOKit.INSTANCE.IOPSGetPowerSourceDescription(powerSourcesInfo, powerSource);
253250

254251
// Get values from dictionary (See IOPSKeys.h)
255252
// Skip if not present
256253
PointerByReference result = new PointerByReference();
257-
if (0 != dictionary.getValueIfPresent(isPresentKey, result)) {
254+
if (dictionary.getValueIfPresent(isPresentKey, result)) {
258255
CFBooleanRef isPresentRef = new CFBooleanRef(result.getValue());
259256
if (isPresentRef.booleanValue()) {
260257
int currentCapacity = 0;
261-
if (0 != dictionary.getValueIfPresent(currentCapacityKey, result)) {
258+
if (dictionary.getValueIfPresent(currentCapacityKey, result)) {
262259
CFNumberRef cap = new CFNumberRef(result.getValue());
263260
currentCapacity = cap.intValue();
264261
}
265262
int maxCapacity = 100;
266-
if (0 != dictionary.getValueIfPresent(maxCapacityKey, result)) {
263+
if (dictionary.getValueIfPresent(maxCapacityKey, result)) {
267264
CFNumberRef cap = new CFNumberRef(result.getValue());
268265
maxCapacity = cap.intValue();
269266
}

0 commit comments

Comments
 (0)