From 5af2fd2e8ff70f7a22fc4d83ec7c73313d15d752 Mon Sep 17 00:00:00 2001 From: Maximilian Gerhard Date: Fri, 28 Jun 2024 16:45:58 +0200 Subject: [PATCH] Fix com4j event handling This commit fixes not always working com4j event handling. There are 2 issues fixed. First is caused by a possible camelize of method names on event interface code generation. The camelize action is compliant with Java method naming conventions but native callback into EventProxy is not aware of such renaming of methods and fails to find the DISPID by name. To solve that a new annotation DISPNAME was introduced for storing the original method name. The EventInterfaceGenerator adds this annotation now to event methods besides the existing DISPID annotation. For keeping compatibility with existing code at runtime the DISPNAME is not mandatory for EventMethods. The EventInterfaceDescriptor uses the DISPNAME value (if present) or the Java method name (previous behavior) as key to fill the methodByName map. The second is caused by a ComException on event method invoke and an arguments Variant.convertTo error. The exception is causing the JVM to crash with a access violation. To avoid that the event method args convertTo is wrapped by a try-catch now. --- .gitignore | 4 +++ runtime/src/main/java/com4j/DISPNAME.java | 29 +++++++++++++++++++ runtime/src/main/java/com4j/EventProxy.java | 19 ++++++++++-- .../com4j/tlbimp/EventInterfaceGenerator.java | 2 ++ 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 runtime/src/main/java/com4j/DISPNAME.java diff --git a/.gitignore b/.gitignore index b64eccb..832ff84 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,7 @@ *.tli vc90.idb vc90.pdb +**/.project +**/.classpath +**/.settings/ +**/target/ \ No newline at end of file diff --git a/runtime/src/main/java/com4j/DISPNAME.java b/runtime/src/main/java/com4j/DISPNAME.java new file mode 100644 index 0000000..180cded --- /dev/null +++ b/runtime/src/main/java/com4j/DISPNAME.java @@ -0,0 +1,29 @@ +package com4j; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Dispatch NAME of the method. + * + *

+ * Java method naming convention is to use mixed case with the first letter + * lowercase, with the first letter of each internal word capitalized. The com4j + * type library importer tool therefore camelize method names if necessary. The + * information of original method name is required for COM event handling to + * find correct Java side method. So we have to annotate that information + * explicitly. + * + * @author Maximilian Gerhard (mgerhard@gk-software.com) + */ +@Retention(RUNTIME) +@Target(METHOD) +public @interface DISPNAME { + /** + * DISPNAME used by {@code EventProxy}. + */ + String value(); +} diff --git a/runtime/src/main/java/com4j/EventProxy.java b/runtime/src/main/java/com4j/EventProxy.java index a4cb08e..1859f2b 100755 --- a/runtime/src/main/java/com4j/EventProxy.java +++ b/runtime/src/main/java/com4j/EventProxy.java @@ -122,7 +122,9 @@ private static class EventInterfaceDescriptor { for (Method m : eventInterface.getDeclaredMethods()) { EventMethod em = new EventMethod(m); - methodsByName.put(m.getName(),em); + + String name = em.dispname != null ? em.dispname : m.getName(); + methodsByName.put(name, em); methodsByID.put(em.dispid,em); } } @@ -142,14 +144,19 @@ public EventMethod get(int id) { private static class EventMethod { private final int dispid; + private final String dispname; private final Method method; private final Class[] params; public EventMethod(Method m) { DISPID a = m.getAnnotation(DISPID.class); + DISPNAME b = m.getAnnotation(DISPNAME.class); if(a==null) throw new IllegalAnnotationException(m+" needs to have @DISPID"); + if (b == null) + logger.log(Level.WARNING, m + " should have @DISPNAME annotation"); dispid = a.value(); + dispname = b != null ? b.value() : null; method = m; params = m.getParameterTypes(); } @@ -163,8 +170,14 @@ public Object invoke(Object o, int flag, Variant[] args) throws Throwable { throw new ComException("Argument length mismatch. Expected "+params.length+" but found "+args.length,DISP_E_BADPARAMCOUNT); Object[] oargs = new Object[args.length]; - for( int i=0; i