forked from kohsuke/com4j
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComMethod.java
More file actions
365 lines (338 loc) · 13.1 KB
/
Copy pathComMethod.java
File metadata and controls
365 lines (338 loc) · 13.1 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package com4j;
import java.lang.annotation.Annotation;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.nio.Buffer;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Internal abstraction that represents a COM method invocation.
*
* <p>
* Instances hide the details of how to invoke a COM method.
* (or a series of them.)
*
* @author Kohsuke Kawaguchi
* @author Michael Schnell (scm, (C) 2008, Michael-Schnell@gmx.de)
*/
abstract class ComMethod {
/** The Java method that corresponds to the native COM method */
final protected Method method;
/**
* The array of default parameters of the COM methods
* @see #defaultParameterIndex
*/
final protected Object[] defaultParameters;
/**
* The parameter indexes of default parameters. Each position in this array corresponds to a position in {@link #defaultParameters}
*
* <p>
* <h3>Example</h3>
* Let's assume the following values:
* <pre>
* defaultParameterIndex = {2, 4};
* defaultParameters = {new Integer(3), "Hello"};
* </pre>
* In this example the second parameter of the {@link ComMethod} has a default value of <code>new Integer(3)</code> and the
* forth parameter has a default value of "Hello".
* </p>
* @see #defaultParameters
*/
final protected int[] defaultParameterIndex;
/** Array of NativeTypes that describe the parameters of the method */
final NativeType[] params;
/** The parameter conversion codes passed to the native part of the invoke method */
final int[] paramConvs;
/** The index in the parameters of the return value ([retval, out] value) */
final int returnIndex;
/** True, if the [retval] parameters is [in, out] */
final boolean returnIsInOut;
/** The value conversion code for the return value */
final NativeType returnConv;
/** The Class objects of the parameters */
final Class<?>[] paramTypes;
/** The Type objects of the generic parameters */
final Type[] genericParamTypes;
/**
* Constructs a new ComMethod for the given {@link Method}
* @param method The Method designed to be a ComMethod (annotations)
*/
public ComMethod(Method method){
this.method = method;
//generate default values
UseDefaultValues defValues = method.getAnnotation(UseDefaultValues.class);
if(defValues != null){
defaultParameters = new Object[defValues.optParamIndex().length];
defaultParameterIndex = defValues.optParamIndex();
generateDefaultParameters(defValues);
}else{
defaultParameters = new Object[0];
defaultParameterIndex = new int[0];
}
MethodIntrospector mi = new MethodIntrospector(method);
Annotation[][] pa = method.getParameterAnnotations();
int paramLen = pa.length;
// retrieve information about the return value
ReturnValue rt = method.getAnnotation(ReturnValue.class);
if(rt!=null) {
if(rt.index()==-1) returnIndex=pa.length;
else returnIndex=rt.index();
returnIsInOut = rt.inout();
if(rt.type() == NativeType.Default){
returnConv = getDefaultConversion(method.getReturnType());
} else {
returnConv = rt.type();
}
} else {
// guess the default
if( method.getReturnType()==Void.TYPE ) {
// no return type
returnIndex = -1;
returnIsInOut = false;
returnConv = NativeType.Default; // unused
} else {
returnIndex = paramLen;
returnIsInOut = false;
returnConv = getDefaultConversion(method.getReturnType());
}
}
// retrieve information about the parameters
Class<?>[] paramTypesLocal = method.getParameterTypes();
Type[] genericParamTypesLocal = method.getGenericParameterTypes();
// Optional parameter
int defValsCount = 0;
if(defValues != null){
defValsCount = defValues.optParamIndex().length;
}
params = new NativeType[paramLen + defValsCount];
paramConvs = new int[paramLen + defValsCount];
paramTypes = new Class<?>[paramLen + defValsCount];
genericParamTypes = new Type[paramLen + defValsCount];
// the "user visalbe" parameters of the Java interface
for( int i=0; i<paramLen; i++ ) {
int destPos = i;
if(defValues != null){
// remap the parameter.
destPos = defValues.paramIndexMapping()[i];
}
NativeType n = mi.getParamConversation(i);
params[destPos] = n;
paramConvs[destPos] = n.code;
paramTypes[destPos] = paramTypesLocal[i];
genericParamTypes[destPos] = genericParamTypesLocal[i];
}
// add the default/optional parameters
for(int i = 0; i < defValsCount; i++){
int ind = defValues.optParamIndex()[i];
params[ind] = defValues.nativeType()[i];
paramConvs[ind] = params[ind].code;
paramTypes[ind] = defValues.javaType()[i];
genericParamTypes[ind] = defValues.javaType()[i];
}
}
/**
* Invokes a method and returns a value.
*
* @param ptr The interface pointer. {@link ComMethod} has apriori knowledge
* of what interface it points to.
*
* @param args The invocation arguments.
*
* @return The return value of the method invocation
*/
abstract Object invoke( long ptr, Object[] args );
/**
* Converts the parameters to be more native friendly.
* @param args the array of objects to be converted.
*/
@SuppressWarnings("unchecked")
protected void messageParameters(Object[] args){
for( int i=0; i<args.length; i++ ) {
if(args[i] instanceof Holder && params[i].getNoByRef()!=null) {
// massage the value of Holder, not the Holder itself
Holder h = (Holder)args[i];
h.value = params[i].getNoByRef().toNative(h.value);
} else {
args[i] = params[i].toNative(args[i]);
}
}
}
private static final Map<Class<?>,NativeType> defaultConversions = new HashMap<Class<?>, NativeType>();
static {
defaultConversions.put( Iterator.class, NativeType.ComObject );
defaultConversions.put( GUID.class, NativeType.GUID );
defaultConversions.put( double.class, NativeType.Double );
defaultConversions.put( float.class, NativeType.Float );
defaultConversions.put( long.class, NativeType.Int64 );
defaultConversions.put( int.class, NativeType.Int32 );
defaultConversions.put( short.class, NativeType.Int16 );
defaultConversions.put( byte.class, NativeType.Int8 );
defaultConversions.put( boolean.class, NativeType.VariantBool );
defaultConversions.put( String.class, NativeType.BSTR );
defaultConversions.put( Object.class, NativeType.VARIANT_ByRef );
defaultConversions.put( Variant.class, NativeType.VARIANT_ByRef );
defaultConversions.put( Date.class, NativeType.Date );
}
/**
* Computes the default conversion for the given type.
*/
static NativeType getDefaultConversion(Type t) {
if( t instanceof Class ) {
Class<?> c = (Class<?>)t;
NativeType r = defaultConversions.get(c);
if(r!=null) return r;
if(Com4jObject.class.isAssignableFrom(c))
return NativeType.ComObject;
if(Enum.class.isAssignableFrom(c))
return NativeType.Int32;
if(Buffer.class.isAssignableFrom(c))
return NativeType.PVOID;
if(Calendar.class.isAssignableFrom(c))
return NativeType.Date;
if(BigDecimal.class.isAssignableFrom(c))
return NativeType.Currency;
if(c.isArray())
return NativeType.SafeArray;
}
if( t instanceof ParameterizedType ) {
ParameterizedType p = (ParameterizedType) t;
if( p.getRawType()==Holder.class ) {
// let p=Holder<V>
Type v = p.getActualTypeArguments()[0];
Class<?> c = (v instanceof Class) ? (Class<?>)v : null;
if(c!=null) {
if(Com4jObject.class.isAssignableFrom(c))
return NativeType.ComObject_ByRef;
if(String.class==c)
return NativeType.BSTR_ByRef;
if(Integer.class==c || Enum.class.isAssignableFrom(c))
return NativeType.Int32_ByRef;
if(Boolean.class==c)
return NativeType.VariantBool_ByRef;
if(Buffer.class.isAssignableFrom(c))
return NativeType.PVOID_ByRef;
if(Double.class.isAssignableFrom(c))
return NativeType.Double_ByRef;
if(c.isArray())
return NativeType.SafeArray_ByRef;
}
if(v instanceof GenericArrayType){
return NativeType.SafeArray_ByRef;
}
}
if( p.getRawType()==Iterator.class ) {
return NativeType.ComObject;
}
}
throw new IllegalAnnotationException("no default conversion available for "+t);
}
/**
* Generates the default values for this ComMethod using the given {@link UseDefaultValues} annotation
* @param defValues the annotation containing the information to generate the default values.
*/
protected void generateDefaultParameters(UseDefaultValues defValues){
int count = defValues.optParamIndex().length;
NativeType[] nt = defValues.nativeType();
Variant.Type[] vt = defValues.variantType();
String[] literal = defValues.literal();
for (int i = 0; i < count; i++) {
switch(nt[i]){
case Bool:
case VariantBool:
case VariantBool_ByRef:
defaultParameters[i] = Boolean.parseBoolean(literal[i]);
break;
case BSTR:
case BSTR_ByRef:
case CSTR:
case Unicode:
defaultParameters[i] = literal[i];
break;
case Double:
case Double_ByRef:
defaultParameters[i] = Double.parseDouble(literal[i]);
break;
case Float:
case Float_ByRef:
defaultParameters[i] = Float.parseFloat(literal[i]);
break;
case Int8:
case Int8_ByRef:
defaultParameters[i] = Byte.parseByte(literal[i]);
break;
case Int16:
case Int16_ByRef:
defaultParameters[i] = Short.parseShort(literal[i]);
break;
case Int32:
case Int32_ByRef:
defaultParameters[i] = Integer.parseInt(literal[i]);
break;
case Int64:
case Int64_ByRef:
defaultParameters[i] = Long.parseLong(literal[i]);
break;
case GUID:
defaultParameters[i] = new GUID(literal[i]);
break;
case Currency:
case Currency_ByRef:
defaultParameters[i] = new BigDecimal(literal[i]);
break;
case VARIANT:
Variant v = new Variant();
switch(vt[i]){
case VT_I1:
case VT_UI1:
v.set(Byte.parseByte(literal[i]));
break;
case VT_I2:
case VT_UI2:
v.set(Short.parseShort(literal[i]));
break;
case VT_I4:
case VT_UI4:
case VT_INT:
case VT_UINT:
v.set(Integer.parseInt(literal[i]));
break;
case VT_I8:
v.set(Long.parseLong(literal[i]));
break;
case VT_R4:
v.set(Float.parseFloat(literal[i]));
break;
case VT_R8:
v.set(Double.parseDouble(literal[i]));
break;
case VT_BOOL:
v.set(Boolean.parseBoolean(literal[i]));
break;
case VT_BSTR:
v.set(literal[i]);
break;
case VT_EMPTY:
v= new Variant();
break;
case VT_ERROR:
v.makeError((int)Long.parseLong(literal[i], 16));
break;
// case VT_CY: ...
default:
throw new UnsupportedOperationException("Don't know how to parse literal " + literal[i] + " to an Java Object corresponding to Variant.Type." + vt[i].name());
}
v.setType(vt[i]);
defaultParameters[i] = v;
break;
default:
throw new UnsupportedOperationException("Don't know how to parse literal " + literal[i] + " to an Java Object corresponding to NativeType." + nt[i].name());
}
}
}
}