diff --git a/README.md b/README.md index a3ac54b..5462408 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Add the following dependency to your pom.xml: ## JavaDocs & Documentation -* [Documentation](https://solutions.weblite.ca/maven/java-objc-bridge/apidocs/index.html) +* [Documentation](https://solutions.weblite.ca/java-objective-c-bridge/docs/) * Read a [blog post](https://sjhannah.com/blog/2012/10/29/speaking-cocoa-from-java/) about the motivation for this project. ## Contact diff --git a/src/main/java/ca/weblite/objc/RuntimeUtils.java b/src/main/java/ca/weblite/objc/RuntimeUtils.java index 1362599..cee78bf 100644 --- a/src/main/java/ca/weblite/objc/RuntimeUtils.java +++ b/src/main/java/ca/weblite/objc/RuntimeUtils.java @@ -1093,6 +1093,12 @@ public static ByReference getAsReferenceWrapper(Object val, String signature){ */ public static native Recipient getJavaPeer(long nsObject); - + /** + * Configures debug logging. Even if debug logging is disabled, exceptions + * will still be printed when they occur. Debug mode is disabled by default. + * + * @param debugMode whether debug mode should be enabled + */ + public static native void setDebugMode(boolean debugMode); } diff --git a/src/main/objectivec/implementation/WLJavaProxy.h b/src/main/objectivec/implementation/WLJavaProxy.h index e404c03..03d1fad 100644 --- a/src/main/objectivec/implementation/WLJavaProxy.h +++ b/src/main/objectivec/implementation/WLJavaProxy.h @@ -18,6 +18,7 @@ } +(void)setJVM:(JavaVM*)theJvm; ++(void)setDebugMode:(bool)debugMode; -(WLJavaProxy*)init:(jobject)peer; -(NSMethodSignature*)methodSignatureForSelector:(SEL)sel; -(void)forwardInvocation:(NSInvocation *)invocation; diff --git a/src/main/objectivec/implementation/WLJavaProxy.m b/src/main/objectivec/implementation/WLJavaProxy.m index c4bb1c6..3b5107d 100644 --- a/src/main/objectivec/implementation/WLJavaProxy.m +++ b/src/main/objectivec/implementation/WLJavaProxy.m @@ -10,6 +10,7 @@ #include "JavaUtil.h" static JavaVM *jvm = NULL; +static bool debugMode = false; unsigned long acceptNSRange(NSRange range) { return range.length + range.location; @@ -123,9 +124,13 @@ -(NSMethodSignature*)methodSignatureForSelector:(SEL)sel // return [NSMethodSignature methodSignatureForSelector:sel]; } +#ifndef logIfDebug +#define logIfDebug(args...) if(debugMode) { NSLog(args); } +#endif + -(void)forwardInvocation:(NSInvocation *)invocation { - NSLog(@"forwardInvocation: %@", invocation); + logIfDebug(@"forwardInvocation: %@", invocation); JNIEnv *env=0; SEL aSelector = [invocation selector]; @@ -133,13 +138,13 @@ -(void)forwardInvocation:(NSInvocation *)invocation @try { if ( attach == 0 ) { - NSLog(@"Before CallBooleanMethod"); + logIfDebug(@"Before CallBooleanMethod"); //JNF_COCOA_ENTER(env); (*env)->CallVoidMethod(env, peer, jForwardInvocation, invocation); //JNF_COCOA_EXIT(env); - NSLog(@"After CallBooleanMethod"); + logIfDebug(@"After CallBooleanMethod"); } //(*jvm)->DetachCurrentThread(jvm); } @catch (NSException *e) { @@ -267,4 +272,8 @@ - (id)forwardInvocationForSelector: (SEL)aSelector withTarget: (id _Nullable)aTa return nil; } ++(void)setDebugMode:(bool)_debugMode { + debugMode = _debugMode; +} + @end diff --git a/src/main/objectivec/implementation/jnaexample_NativeClient_Setup.h b/src/main/objectivec/implementation/jnaexample_NativeClient_Setup.h index b25fde1..45a48de 100644 --- a/src/main/objectivec/implementation/jnaexample_NativeClient_Setup.h +++ b/src/main/objectivec/implementation/jnaexample_NativeClient_Setup.h @@ -30,7 +30,10 @@ JNIEXPORT jlong JNICALL Java_ca_weblite_objc_RuntimeUtils_createProxy */ JNIEXPORT jobject JNICALL Java_ca_weblite_objc_RuntimeUtils_getJavaPeer (JNIEnv *, jclass, jlong); - + +JNIEXPORT void JNICALL Java_ca_weblite_objc_RuntimeUtils_setDebugMode +(JNIEnv *, jclass, jboolean); + /* JNIEXPORT jobject JNICALL Java_ca_weblite_objc_RuntimeUtils_invokeWithSelfAndTarget (JNIEnv *, jclass, jlong, jlong, jlong); diff --git a/src/main/objectivec/implementation/jnaexample_NativeClient_Setup.m b/src/main/objectivec/implementation/jnaexample_NativeClient_Setup.m index 6f51997..3bba22d 100644 --- a/src/main/objectivec/implementation/jnaexample_NativeClient_Setup.m +++ b/src/main/objectivec/implementation/jnaexample_NativeClient_Setup.m @@ -71,6 +71,12 @@ void exceptionHandler(NSException *exception) } } +JNIEXPORT void JNICALL Java_ca_weblite_objc_RuntimeUtils_setDebugMode +(JNIEnv *env, jclass cls, jboolean debugMode) +{ + [WLJavaProxy setDebugMode:debugMode]; +} + /* JNIEXPORT jobject JNICALL Java_ca_weblite_objc_RuntimeUtils_invokeWithSelfAndTarget (JNIEnv *env, jclass jcls, jlong selfPtr, jlong target, jlong invocation) diff --git a/src/test/java/ca/weblite/objc/NSObjectTest.java b/src/test/java/ca/weblite/objc/NSObjectTest.java index ca958e5..96b4a4b 100644 --- a/src/test/java/ca/weblite/objc/NSObjectTest.java +++ b/src/test/java/ca/weblite/objc/NSObjectTest.java @@ -119,6 +119,17 @@ public void testCustomClass(){ myObj = cls.sendProxy("getMyObj"); assertEquals(array, myObj); } + + @Test + public void debugModeTest() { + setDebugMode(true); + TestCustomClass cls = new TestCustomClass(); + cls.init("NSObject"); + cls.send("myCustomString"); + setDebugMode(false); + cls.send("myCustomString"); + System.out.println("Exactly one set of debug logs should be printed above"); + } public static class TestCustomClass extends NSObject { private String str = "My custom string";