svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / programming / CallStaticMethodFunction.java @ 44750
History | View | Annotate | Download (2.64 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|
2 |
|
3 |
import java.lang.reflect.Method; |
4 |
import java.util.List; |
5 |
import org.apache.commons.lang3.Range; |
6 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
7 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
8 |
import org.gvsig.expressionevaluator.Function; |
9 |
import org.gvsig.expressionevaluator.Interpreter; |
10 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
11 |
|
12 |
@SuppressWarnings("UseSpecificCatch") |
13 |
public class CallStaticMethodFunction extends AbstractFunction { |
14 |
|
15 |
public static final String NAME = "CALL_STATIC"; |
16 |
|
17 |
public CallStaticMethodFunction() {
|
18 |
super(
|
19 |
Function.GROUP_PROGRAMMING, |
20 |
NAME, |
21 |
Range.between(2, Integer.MAX_VALUE), |
22 |
"The "+NAME+"() function call a static mehod of a class.", |
23 |
NAME+"( className, methodName, arguments... )",
|
24 |
null,
|
25 |
"Object",
|
26 |
false
|
27 |
); |
28 |
} |
29 |
|
30 |
@Override
|
31 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
32 |
String theClassName = getStr(args,0); |
33 |
String theMethodName = getStr(args,1); |
34 |
int firstArg = 1; |
35 |
if( theClassName == null ) { |
36 |
throw new NullPointerException("A class name name was expected to invoke and a null class name was received"); |
37 |
} |
38 |
if( theMethodName == null ) { |
39 |
throw new NullPointerException("A method name was expected to invoke and a null methd name was received"); |
40 |
} |
41 |
List<ClassLoader> loaders = ExpressionEvaluatorLocator.getManager().getClassLoaders(); |
42 |
Class<?> theClass = null; |
43 |
for (ClassLoader loader : loaders) { |
44 |
try {
|
45 |
theClass = loader.loadClass(theClassName); |
46 |
if( theClass!=null ) { |
47 |
break;
|
48 |
} |
49 |
} catch(Throwable th) { |
50 |
// Do nothing skip
|
51 |
} |
52 |
} |
53 |
if( theClass == null ) { |
54 |
throw new ExpressionRuntimeException("Can't localte the class '"+theClassName+"'."); |
55 |
} |
56 |
|
57 |
Class[] parameterTypes = new Class[args.length-firstArg]; |
58 |
Object[] parameters = new Object[args.length-firstArg]; |
59 |
for (int i = 0; i < parameters.length; i++) { |
60 |
Object parameter = args[i+firstArg];
|
61 |
parameters[i] = parameter; |
62 |
if( parameter==null ) { |
63 |
parameterTypes[i] = null;
|
64 |
} else {
|
65 |
parameterTypes[i] = parameter.getClass(); |
66 |
} |
67 |
} |
68 |
Method method = theClass.getMethod(theMethodName, parameterTypes);
|
69 |
Object value = method.invoke(null, parameters); |
70 |
return value;
|
71 |
} |
72 |
|
73 |
} |