3

我正在尝试将一个类导入到 MVEL 的实现中,但出现以下错误:

[错误:com.MyApp.Main 类型的预期接收者,但得到了 java.lang.Class]

这是设置 MVEL 的方法:

public void runCode() {
 final String theCode = "Main.sendText();";
        System.setProperty("java.version", "1.6");

        new Thread(new Runnable() {

        public void run() {

            ParserContext context = new ParserContext();
            context.addImport("Main", Main.class);                      
            Serializable compiled = MVEL.compileExpression(theCode, context); // compile the expresion

                HashMap vars = new HashMap();
                vars.put("x", new Integer(10));
                vars.put("y", new Integer(10));

                Integer result = (Integer) MVEL.executeExpression(compiled, vars);          
        }
    }).start();
}

主类:

public void sendText(){
System.out.println("Success!");
}

日志猫:

08-05 20:23:31.900: E/AndroidRuntime(19968): FATAL EXCEPTION: Thread-30751
08-05 20:23:31.900: E/AndroidRuntime(19968): [Error: expected receiver of type com.MyApp.Main, but got java.lang.Class<com.MyApp.Main>]
08-05 20:23:31.900: E/AndroidRuntime(19968): [Near : {... Main.sendText(); ....}]
08-05 20:23:31.900: E/AndroidRuntime(19968):              ^
08-05 20:23:31.900: E/AndroidRuntime(19968): [Line: 1, Column: 1]
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:435)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:143)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.optimizers.dynamic.DynamicOptimizer.optimizeAccessor(DynamicOptimizer.java:67)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.ast.ASTNode.optimize(ASTNode.java:159)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:115)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.MVELRuntime.execute(MVELRuntime.java:85)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:113)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.MVEL.executeExpression(MVEL.java:969)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at com.MyApp.KessilLightScenes$1.run(KessilLightScenes.java:136)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at java.lang.Thread.run(Thread.java:856)
08-05 20:23:31.900: E/AndroidRuntime(19968): Caused by: java.lang.IllegalArgumentException: expected receiver of type com.MyApp.Main, but got java.lang.Class<com.MyApp.Main>
08-05 20:23:31.900: E/AndroidRuntime(19968):    at java.lang.reflect.Method.invokeNative(Native Method)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at java.lang.reflect.Method.invoke(Method.java:511)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:1104)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:987)
08-05 20:23:31.900: E/AndroidRuntime(19968):    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:344)
08-05 20:23:31.900: E/AndroidRuntime(19968):    ... 11 more

知道如何解决此错误吗?谢谢

4

1 回答 1

6

听起来您尝试访问的方法可能需要是静态的。

于 2013-10-28T01:52:05.907 回答