Skip to content

Commit

Permalink
try to catch exception when invoking a method in ObjCClass
Browse files Browse the repository at this point in the history
  • Loading branch information
umjammer committed Mar 26, 2024
1 parent 10670e9 commit d9add86
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ implementation of Objective-C interfaces in Java.
* separate same parts of jna-platform (like jna-platform-extended)
* deprecate rococoa-contrib
* ~~selector and java method binding for notification~~
* exception in callback method cannot be shown as reason, shown as "Exception calling method for selector foo:"
* `OCInvocationCallbacks.java:170`

----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static NSMutableArray toArray(NSObject...objects) {
@SuppressWarnings("unchecked")
public <T extends NSObject> List<T> toList() {
List<NSObject> result = new ArrayList<>();
for (int i = 0; i <this.count(); i++) {
for (int i = 0; i < this.count(); i++) {
result.add(this.objectAtIndex(i));
}
return (List<T>) result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.jna.Pointer;
Expand Down Expand Up @@ -170,7 +171,12 @@ public Object intercept(@This Object proxy, @Origin Method method, @AllArguments
if (!Modifier.isAbstract(method.getModifiers())) {
// method is not abstract, so a Java override has been provided, which we call
logging.finest(String.format("superMethod.invoke [%s %s].%s(%s)", javaClassName, ocInstance, method.getName(), new VarArgsUnpacker(args)));
return superMethod.invoke(proxy, args);
try {
return superMethod.invoke(proxy, args);
} catch (Throwable t) {
logging.log(Level.WARNING, String.format("superMethod.invoke [%s %s].%s(%s) failure", javaClassName, ocInstance, method.getName(), new VarArgsUnpacker(args)), t);
throw t;
}
}
// normal case
return invokeCocoa(method, args);
Expand Down
19 changes: 1 addition & 18 deletions rococoa-core/src/test/java/Test1.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.rococoa.cocoa.foundation.NSString;
Expand All @@ -20,29 +19,13 @@


/**
* Test1.
* Integration Test.
*
* @author <a href="mailto:[email protected]">Naohide Sano</a> (nsano)
* @version 0.00 2023-06-30 nsano initial version <br>
*/
public class Test1 {

@Test
@Disabled
void test1() throws Exception {
// MethodHandle methodHandleFieldDirect = lookup.unreflectGetter(fieldName);
// CallSite callSiteField = new ConstantCallSite(methodHandleFieldDirect);
// methodHandleFieldDirect = callSiteField.dynamicInvoker();
// name = (String) methodHandleFieldDirect.invokeExact(new Employee());
//
//
////Lookup invoke dynamic
// methodType = MethodType.methodType(String.class);
// methodHandle = lookup.findVirtual(Employee.class, "getName", methodType);
// CallSite callSiteMethod = new ConstantCallSite(methodHandleFieldDirect);
// methodHandle = callSiteMethod.dynamicInvoker();
}

@Test
@DisabledIfEnvironmentVariable(named = "GITHUB_WORKFLOW", matches = ".*")
void test2() throws Exception {
Expand Down
51 changes: 51 additions & 0 deletions rococoa-core/src/test/java/TestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2024 by Naohide Sano, All rights reserved.
*
* Programmed by Naohide Sano
*/

import java.util.List;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.rococoa.cocoa.foundation.NSArray;

import static org.junit.jupiter.api.Assertions.assertThrows;


/**
* TestCase.
*
* @author <a href="mailto:[email protected]">Naohide Sano</a> (nsano)
* @version 0.00 2024-03-26 nsano initial version <br>
*/
public class TestCase {

@Test
@Disabled
void test1() throws Exception {
// MethodHandle methodHandleFieldDirect = lookup.unreflectGetter(fieldName);
// CallSite callSiteField = new ConstantCallSite(methodHandleFieldDirect);
// methodHandleFieldDirect = callSiteField.dynamicInvoker();
// name = (String) methodHandleFieldDirect.invokeExact(new Employee());
//
//
////Lookup invoke dynamic
// methodType = MethodType.methodType(String.class);
// methodHandle = lookup.findVirtual(Employee.class, "getName", methodType);
// CallSite callSiteMethod = new ConstantCallSite(methodHandleFieldDirect);
// methodHandle = callSiteMethod.dynamicInvoker();
}

NSArray array() {
return null;
}

@Test
void test2() throws Exception {
NSArray array = array();
assertThrows(NullPointerException.class, () -> {
List<?> list = array.toList();
});
}
}

0 comments on commit d9add86

Please sign in to comment.