Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix com4j event handling #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
*.tli
vc90.idb
vc90.pdb
**/.project
**/.classpath
**/.settings/
**/target/
29 changes: 29 additions & 0 deletions runtime/src/main/java/com4j/DISPNAME.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>
* 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 ([email protected])
*/
@Retention(RUNTIME)
@Target(METHOD)
public @interface DISPNAME {
/**
* DISPNAME used by {@code EventProxy}.
*/
String value();
}
19 changes: 16 additions & 3 deletions runtime/src/main/java/com4j/EventProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ private static class EventInterfaceDescriptor<T> {

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);
}
}
Expand All @@ -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();
}
Expand All @@ -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<args.length; i++ )
oargs[i] = args[i].convertTo(params[i]);
for( int i=0; i<args.length; i++ ) {
try {
oargs[i] = args[i].convertTo(params[i]);
} catch (Exception e) {
logger.log(Level.WARNING, "Unable to convert param["+ i + "] " +params[i] + " of method "+method,e);
oargs[i] = null;
}
}
return method.invoke(o,oargs);
} catch (InvocationTargetException e) {
logger.log(Level.WARNING, method+" on "+o+" reported an exception",e.getTargetException());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ protected void annotate(IndentingWriter o) {
super.annotate(o);
o.printf("@DISPID(%1d)",method.getDispId());
o.println();
o.printf("@DISPNAME(\"%s\")", method.getName());
o.println();
}

@Override
Expand Down