Skip to content

Commit 40d4daa

Browse files
committed
Fix warnings
1 parent 34c067b commit 40d4daa

File tree

9 files changed

+100
-170
lines changed

9 files changed

+100
-170
lines changed

src/main/java/ca/weblite/nativeutils/NativeUtils.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ public static void loadLibraryFromJar(String path) throws IOException {
4040
* Method uses String as filename because the pathname is "abstract", not system-dependent.
4141
*
4242
* @throws java.lang.IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters (restriction of @see File#createTempFile(java.lang.String, java.lang.String)).
43-
* @throws java.lang.IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters (restriction of @see File#createTempFile(java.lang.String, java.lang.String)).
44-
* @throws java.lang.IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters (restriction of @see File#createTempFile(java.lang.String, java.lang.String)).
4543
* @param path a {@link java.lang.String} object.
4644
* @param source a {@link java.lang.Class} object.
4745
* @throws java.io.IOException if any.
4846
*/
49-
public static void loadLibraryFromJar(String path, Class source) throws IOException {
47+
public static void loadLibraryFromJar(String path, Class<?> source) throws IOException {
5048

5149

5250
// Finally, load the library
@@ -61,7 +59,7 @@ public static void loadLibraryFromJar(String path, Class source) throws IOExcept
6159
* @return a {@link java.io.File} object.
6260
* @throws java.io.IOException if any.
6361
*/
64-
public static File loadFileFromJar(String path, Class source) throws IOException {
62+
public static File loadFileFromJar(String path, Class<?> source) throws IOException {
6563
if (!path.startsWith("/")) {
6664
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
6765
}

src/main/java/ca/weblite/objc/Client.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,6 @@ public Message[] buildMessageChain(Object... parameters){
516516

517517
}
518518
messages.add(buffer);
519-
520-
521-
522-
523-
524519
}
525520

526521
return messages.toArray(new Message[messages.size()]);

src/main/java/ca/weblite/objc/NSObject.java

Lines changed: 77 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import java.util.HashMap;
1212
import java.util.List;
1313
import java.util.Map;
14-
import java.util.logging.Level;
15-
import java.util.logging.Logger;
1614

1715
import com.sun.jna.Function;
1816
import com.sun.jna.Pointer;
@@ -53,9 +51,6 @@
5351
* @since 1.1
5452
*/
5553
public class NSObject extends Proxy implements PeerableRecipient {
56-
private static final Logger LOG = Logger.getLogger("NSObject");
57-
58-
5954
/**
6055
* Pointer to the parent objective-c object of this object.
6156
*/
@@ -69,7 +64,7 @@ public class NSObject extends Proxy implements PeerableRecipient {
6964
/**
7065
* Maps string selectors to java methods for this class.
7166
*/
72-
private static Map<Class,Map<String,Method>> methodMap = new HashMap<Class,Map<String,Method>>();
67+
private static final Map<Class<?>, Map<String,Method>> methodMap = new HashMap<>();
7368

7469
/**
7570
* Returns the method map for a particular class. The Map that is returned maps string selectors
@@ -78,7 +73,7 @@ public class NSObject extends Proxy implements PeerableRecipient {
7873
* @param cls The class whose map we wish to obtain
7974
* @return The map that maps string selectors
8075
*/
81-
protected static Map<String,Method> getMethodMap(Class cls){
76+
protected static Map<String,Method> getMethodMap(Class<?> cls){
8277
Map<String,Method> mm = methodMap.get(cls);
8378

8479
if ( mm == null ){
@@ -94,20 +89,20 @@ protected static Map<String,Method> getMethodMap(Class cls){
9489
}
9590

9691
for (Class<?> c : classes) {
97-
Method[] methods = c.getDeclaredMethods();
92+
Method[] methods = c.getDeclaredMethods();
9893

99-
for ( int i=0; i<methods.length; i++){
100-
Method method = methods[i];
101-
102-
// ignore methods of superclasses if override
103-
if (!mm.containsKey(method.getName())) {
104-
Msg message = (Msg)method.getAnnotation(Msg.class);
105-
106-
if ( message != null){
107-
mm.put(message.selector(), method);
108-
}
109-
}
110-
}
94+
for ( int i=0; i<methods.length; i++){
95+
Method method = methods[i];
96+
97+
// ignore methods of superclasses if override
98+
if (!mm.containsKey(method.getName())) {
99+
Msg message = method.getAnnotation(Msg.class);
100+
101+
if ( message != null){
102+
mm.put(message.selector(), method);
103+
}
104+
}
105+
}
111106
}
112107

113108
methodMap.put(cls, mm);
@@ -268,7 +263,7 @@ public long methodSignatureForSelector(long lselector) {
268263
Pointer selector = new Pointer(lselector);
269264
Method method = methodForSelector(selName(selector));
270265
if ( method != null){
271-
Msg message = (Msg)method.getAnnotation(Msg.class);
266+
Msg message = method.getAnnotation(Msg.class);
272267
if ( !"".equals(message.signature()) ){
273268
long res = PointerTool.getPeer(
274269
msgPointer(cls("NSMethodSignature"), "signatureWithObjCTypes:", message.signature())
@@ -338,19 +333,17 @@ public void forwardInvocationToParent(long linvocation){
338333
strReturnType = strReturnType.substring(offset);
339334
}
340335

341-
Object[] args = new Object[new Long(numArgs).intValue()];
336+
Object[] args = new Object[(int) numArgs];
342337
args[0] = peer;
343338
args[1] = parent;
344339
for ( int i=2; i<numArgs; i++){
345-
long argumentSigAddr = (Long)pSig.send("getArgumentTypeAtIndex:", i);
346-
String argumentSignature = new Pointer(argumentSigAddr).getString(0);
347340
LongByReference ptrRef = new LongByReference();
348341
msg(invocation, "getArgument:atIndex:", ptrRef.getPointer(), i);
349342
args[i] = ptrRef.getValue();
350343
}
351344
char retTypeChar = strReturnType.charAt(0);
352345

353-
Class retType = null;
346+
Class<?> retType = null;
354347
switch ( retTypeChar){
355348
case 'v':
356349
retType = void.class; break;
@@ -392,11 +385,7 @@ public void forwardInvocationToParent(long linvocation){
392385
// the normal way
393386
//System.out.println("We give up... passing "+sel(selector)+" to parent");
394387
msg(invocation, "invokeWithTarget:", parent);
395-
return;
396-
397-
398-
399-
388+
return;
400389
}
401390

402391
Object retVal = func.invoke(retType, args);
@@ -466,80 +455,70 @@ public void forwardInvocation(long linvocation) {
466455
String selName = selName(selector);
467456
Method method = methodForSelector(selName);
468457
if ( method != null){
469-
470-
Msg message = (Msg)method.getAnnotation(Msg.class);
471-
if ( true || !"".equals(message.signature()) ){
472-
// Perform the method and provide the correct output for the invocation
473-
474-
475-
Object[] args = new Object[new Long(numArgs).intValue()-2];
476-
for ( int i=2; i<numArgs; i++){
477-
478-
long argumentSigAddr = (Long)pSig.send("getArgumentTypeAtIndex:", i);
479-
String argumentSignature = new Pointer(argumentSigAddr).getString(0);
480-
481-
if ( "fd".indexOf(argumentSignature.substring(0,1)) != -1 ){
482-
DoubleByReference ptrRef = new DoubleByReference();
483-
484-
msg(invocation, "getArgument:atIndex:", ptrRef.getPointer(), i);
485-
486-
args[i-2] = TypeMapper
487-
.getInstance()
488-
.cToJ(
489-
ptrRef.getValue(),
490-
//argPtr.toNative(),
491-
argumentSignature,
492-
TypeMapper.getInstance()
493-
);
494-
} else {
495-
LongByReference ptrRef = new LongByReference();
496-
497-
msg(invocation, "getArgument:atIndex:", ptrRef.getPointer(), i);
498-
499-
args[i-2] = TypeMapper
500-
.getInstance()
501-
.cToJ(
502-
ptrRef.getValue(),
503-
//argPtr.toNative(),
504-
argumentSignature,
505-
TypeMapper.getInstance()
506-
);
507-
}
508-
509-
510-
511-
}
458+
// Perform the method and provide the correct output for the invocation
459+
Object[] args = new Object[(int) numArgs - 2];
460+
for ( int i=2; i<numArgs; i++){
461+
462+
long argumentSigAddr = (Long)pSig.send("getArgumentTypeAtIndex:", i);
463+
String argumentSignature = new Pointer(argumentSigAddr).getString(0);
512464

513-
try {
514-
method.setAccessible(true);
515-
Object res = method.invoke(this, args);
465+
if ( "fd".indexOf(argumentSignature.charAt(0)) != -1 ){
466+
DoubleByReference ptrRef = new DoubleByReference();
516467

517-
// We should release the arguments now since we retained them before
518-
// to prevent memory leaks.
519-
for ( int i=0; i<args.length; i++){
520-
Proxy.release(args[i]);
521-
}
522-
523-
long returnType = (Long)pSig.send("methodReturnType");
468+
msg(invocation, "getArgument:atIndex:", ptrRef.getPointer(), i);
524469

525-
String strReturnType = new Pointer(returnType).getString(0);
470+
args[i-2] = TypeMapper
471+
.getInstance()
472+
.cToJ(
473+
ptrRef.getValue(),
474+
//argPtr.toNative(),
475+
argumentSignature,
476+
TypeMapper.getInstance()
477+
);
478+
} else {
479+
LongByReference ptrRef = new LongByReference();
526480

481+
msg(invocation, "getArgument:atIndex:", ptrRef.getPointer(), i);
527482

528-
res = TypeMapper
529-
.getInstance()
530-
.jToC(res, strReturnType, TypeMapper.getInstance());
531-
532-
if ( !"v".equals(strReturnType)){
533-
534-
Object retVal = res == null ? new PointerByReference(Pointer.NULL).getPointer() : RuntimeUtils.getAsReference(res, strReturnType);
535-
msg(invocation, "setReturnValue:", retVal);
536-
}
537-
538-
return;
539-
} catch (Exception ex){
540-
throw new NSMessageInvocationException(selName, method, ex);
483+
args[i-2] = TypeMapper
484+
.getInstance()
485+
.cToJ(
486+
ptrRef.getValue(),
487+
//argPtr.toNative(),
488+
argumentSignature,
489+
TypeMapper.getInstance()
490+
);
491+
}
492+
}
493+
494+
try {
495+
method.setAccessible(true);
496+
Object res = method.invoke(this, args);
497+
498+
// We should release the arguments now since we retained them before
499+
// to prevent memory leaks.
500+
for ( int i=0; i<args.length; i++){
501+
Proxy.release(args[i]);
541502
}
542-
503+
504+
long returnType = (Long) pSig.send("methodReturnType");
505+
506+
String strReturnType = new Pointer(returnType).getString(0);
507+
508+
509+
res = TypeMapper
510+
.getInstance()
511+
.jToC(res, strReturnType, TypeMapper.getInstance());
512+
513+
if ( !"v".equals(strReturnType)){
514+
515+
Object retVal = res == null ? new PointerByReference(Pointer.NULL).getPointer() : RuntimeUtils.getAsReference(res, strReturnType);
516+
msg(invocation, "setReturnValue:", retVal);
517+
}
518+
519+
return;
520+
} catch (Exception ex){
521+
throw new NSMessageInvocationException(selName, method, ex);
543522
}
544523
}
545524
// If we send using invokeWithTarget, then we will use the method of the parent
@@ -628,6 +607,4 @@ public NSObject dealloc(){
628607
}
629608

630609

631-
632-
633610
}

src/main/java/ca/weblite/objc/RuntimeUtils.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import com.sun.jna.ptr.PointerByReference;
1313
import com.sun.jna.ptr.ShortByReference;
1414
import java.io.IOException;
15-
import java.util.Arrays;
16-
1715

1816
/**
1917
* A Java class with static methods that interact with the Objective-C runtime.
@@ -311,7 +309,9 @@ public static Pointer msgPointer(String receiver, String selector, Object... arg
311309
* Wrapper around msg() that returns an int. This should only be used for
312310
* sending messages that return int-compatible numeric values. E.g.
313311
* byte, bool, long, int, short. Do not use this if the message will return
314-
* something else (like a float, double, string, or pointer).
312+
* something else (like a float, double, string, or pointer). Narrowing
313+
* primitive conversion will be applied; this will cause information loss
314+
* if the {@code long} result does not fit in the {@code int} value range.
315315
*
316316
* @param receiver The target of the message.
317317
* @param selector The selector for the message.
@@ -320,7 +320,7 @@ public static Pointer msgPointer(String receiver, String selector, Object... arg
320320
*/
321321
public static int msgInt(Pointer receiver, Pointer selector, Object... args){
322322
long res = msg(receiver, selector, args);
323-
return new Long(res).intValue();
323+
return (int) res;
324324
}
325325

326326
/**
@@ -576,8 +576,6 @@ public static double msgDouble(Pointer receiver, String selector, Object... args
576576
* on the return type of the message.
577577
*/
578578
public static Object msg(boolean coerceReturn, boolean coerceArgs, Pointer receiver, Pointer selector, Object... args){
579-
Object[] originalArgs = args;
580-
581579
Pointer methodSignature = msgPointer(receiver, "methodSignatureForSelector:", selector);
582580

583581
int numArgs = (int)msg(methodSignature, "numberOfArguments");
@@ -594,15 +592,10 @@ public static Object msg(boolean coerceReturn, boolean coerceArgs, Pointer recei
594592

595593

596594
if ( coerceArgs && args.length > 0 ){
597-
originalArgs = Arrays.copyOf(args, args.length);
598-
599595
for ( int i=0; i<args.length; i++ ){
600-
ByteByReference out = new ByteByReference();
601-
602-
long out2 = (long)msg(methodSignature, "getArgumentTypeAtIndex:", i+2);
596+
long out2 = msg(methodSignature, "getArgumentTypeAtIndex:", i+2);
603597
String argumentTypeSignature = new Pointer(out2).getString(0);
604598
args[i] = TypeMapper.getInstance().jToC(args[i], argumentTypeSignature, TypeMapper.getInstance());
605-
606599
}
607600
}
608601

@@ -619,7 +612,7 @@ public static Object msg(boolean coerceReturn, boolean coerceArgs, Pointer recei
619612
returnTypeSignature = returnTypeSignature.substring(offset);
620613
}
621614

622-
String returnTypeFirstChar = returnTypeSignature.substring(0,1);
615+
char returnTypeFirstChar = returnTypeSignature.charAt(0);
623616
if ( "[{(".indexOf(returnTypeFirstChar) ==-1 ){
624617
// We are not returning a structure so we'll just
625618
// do the message.
@@ -655,10 +648,6 @@ public static Object msg(boolean coerceReturn, boolean coerceArgs, Pointer recei
655648
Proxy.release(args[i]);
656649
}
657650
return output;
658-
659-
660-
661-
662651
}
663652

664653

@@ -727,7 +716,7 @@ public static Object msg(Message... messages){
727716

728717
try {
729718

730-
m.result = msg(coerceOutput, coerceInput, m.receiver, m.selector, m.args.toArray(new Object[m.args.size()]));
719+
m.result = msg(coerceOutput, coerceInput, m.receiver, m.selector, m.args.toArray());
731720
} catch (Exception ex){
732721
m.error = ex;
733722
}
@@ -804,13 +793,6 @@ public static ByReference getAsReferenceWrapper(Object val, String signature){
804793
signature = signature.substring(offset);
805794
}
806795

807-
String firstChar = signature.substring(0,1);
808-
String numeric = "iIsSlLqQfd";
809-
810-
811-
812-
813-
//String firstChar = signature.substring(0,1);
814796
switch ( signature.charAt(0)){
815797
case 'i':
816798
case 'I':

0 commit comments

Comments
 (0)