forked from kohsuke/com4j
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDispatchComMethod.java
More file actions
78 lines (62 loc) · 2.29 KB
/
DispatchComMethod.java
File metadata and controls
78 lines (62 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com4j;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* {@link ComMethod} that invokes through {@code IDispatch.Invoke}.
*
* @author Kohsuke Kawaguchi
*/
final class DispatchComMethod extends ComMethod {
final int dispId;
final int flag;
final Class<?> retType;
DispatchComMethod( Method m ) {
super(m);
DISPID id = m.getAnnotation(DISPID.class);
if(id ==null)
throw new IllegalAnnotationException("@DISPID is missing: "+m.toGenericString());
dispId = id.value();
flag = getFlag();
Class retType = m.getReturnType();
if (retType.isPrimitive() && boxTypeMap.containsKey(retType))
retType = boxTypeMap.get(retType);
this.retType = retType;
}
private int getFlag() {
PropGet get = method.getAnnotation(PropGet.class);
PropPut put = method.getAnnotation(PropPut.class);
if(get!=null && put!=null)
throw new IllegalAnnotationException("@PropPut and @PropGet are mutually exclusive: "+method.toGenericString());
if(get!=null)
return DISPATCH_PROPERTYGET;
if(put!=null)
return DISPATCH_PROPERTYPUT;
return DISPATCH_METHOD;
}
Object invoke(long ptr, Object[] args) {
messageParameters(args);
Variant v = Native.invokeDispatch(ptr,dispId,flag,args);
if(v==null)
return null;
if(retType==void.class)
return null;
return v.convertTo(retType);
}
private static final int DISPATCH_METHOD = 0x1;
private static final int DISPATCH_PROPERTYGET = 0x2;
private static final int DISPATCH_PROPERTYPUT = 0x4;
@SuppressWarnings("unused")
private static final int DISPATCH_PROPERTYPUTREF = 0x8;
private static final Map<Class,Class> boxTypeMap = new HashMap<Class,Class>();
static {
boxTypeMap.put(byte.class,Byte.class);
boxTypeMap.put(short.class,Short.class);
boxTypeMap.put(int.class,Integer.class);
boxTypeMap.put(long.class,Long.class);
boxTypeMap.put(float.class,Float.class);
boxTypeMap.put(double.class,Double.class);
boxTypeMap.put(boolean.class,Boolean.class);
boxTypeMap.put(char.class,Character.class);
}
}