Error: expected receiver of type com.MyApp.Main, but got java.lang.Class<com.MyApp.Main>

6.8k Views Asked by At

I'm trying to import a class into an implementation of MVEL, and I get the following error:

[Error: expected receiver of type com.MyApp.Main, but got java.lang.Class]

Here's the method that sets up 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();
}

Main.class:

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

Logcat:

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

Any idea how to fix this error? Thanks

1

There are 1 best solutions below

0
On

It sounds like the method(s) you're attempting to access may need to be static.