I am newbie to MVEL. Following steps I followed
1) Downloaded latest from http://mvel.codehaus.org/Downloading+MVEL i.e. Download mvel2-2.2.0.jar (JDK 1.5+ required)
2) Created a simple list of custom obj
class TestMVEL {
public static void main(String[] p){
String expr0 = "fruits[1]";
String expr1 = "System.out.println(fruits[1]);";
String expr2 = "foreach( f : fruits) { System.out.println(f.getName()); }";
System.out.println(MVEL.eval(expr0, new Basket()));//prints Fruit{name=Banana}
MVEL.eval(expr1, new Basket());// prints Fruit{name=Banana}
MVEL.eval(expr2, new Basket());//error
}
}
class Basket {
List<Fruit> fruits = new ArrayList<Fruit>();
public List<Fruit> getFruits(){
fruits.clear();
fruits.add(new Fruit("Apple"));
fruits.add(new Fruit("Banana"));
fruits.add(new Fruit("Orange"));
return fruits;
}
}
class Fruit {
private String name;
public Fruit(String n){
name = n;
}
public String getName(){
return name;
}
public String toString() {
return "Fruit{" + "name=" + name + '}';
}
}
3) Getting below error
Exception in thread "main" [Error: f.getName(): Class org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer can not access a member of class test.Fruit with modifiers "public"] [Near : {... foreach( f : fruits) { System.out.println(f.getName()); } ....}]
class Fruit needs to be public.
public class Fruit...