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 | 44138 | jjdelcerro | package org.gvsig.expressionevaluator.impl.function.programming; |
---|---|---|---|
2 | 43512 | jjdelcerro | |
3 | 43939 | jjdelcerro | import java.lang.reflect.Method; |
4 | 44750 | jjdelcerro | import java.util.List; |
5 | 43939 | jjdelcerro | import org.apache.commons.lang3.Range; |
6 | 44750 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
7 | import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
||
8 | 43521 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
9 | import org.gvsig.expressionevaluator.Interpreter; |
||
10 | 43939 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
11 | 43521 | jjdelcerro | |
12 | 44750 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
13 | 44138 | jjdelcerro | public class CallStaticMethodFunction extends AbstractFunction { |
14 | 43512 | jjdelcerro | |
15 | 44138 | jjdelcerro | public static final String NAME = "CALL_STATIC"; |
16 | 43939 | jjdelcerro | |
17 | 44138 | jjdelcerro | public CallStaticMethodFunction() {
|
18 | 43939 | jjdelcerro | super(
|
19 | 44138 | jjdelcerro | Function.GROUP_PROGRAMMING, |
20 | NAME, |
||
21 | 43939 | jjdelcerro | Range.between(2, Integer.MAX_VALUE), |
22 | 44138 | jjdelcerro | "The "+NAME+"() function call a static mehod of a class.", |
23 | NAME+"( className, methodName, arguments... )",
|
||
24 | 44098 | jjdelcerro | null,
|
25 | "Object",
|
||
26 | false
|
||
27 | 43939 | jjdelcerro | ); |
28 | 43512 | jjdelcerro | } |
29 | 43939 | jjdelcerro | |
30 | 43512 | jjdelcerro | @Override
|
31 | 43939 | jjdelcerro | 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 | 44038 | jjdelcerro | throw new NullPointerException("A class name name was expected to invoke and a null class name was received"); |
37 | 43512 | jjdelcerro | } |
38 | 43939 | jjdelcerro | if( theMethodName == null ) { |
39 | throw new NullPointerException("A method name was expected to invoke and a null methd name was received"); |
||
40 | 43512 | jjdelcerro | } |
41 | 44750 | jjdelcerro | 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 | 43939 | jjdelcerro | 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 | 43512 | jjdelcerro | } |
68 | 43939 | jjdelcerro | Method method = theClass.getMethod(theMethodName, parameterTypes);
|
69 | Object value = method.invoke(null, parameters); |
||
70 | return value;
|
||
71 | 43512 | jjdelcerro | } |
72 | |||
73 | } |