public class AppTest{
public static void main(String[] args) throws NoSuchMethodException {
specialOperator();
}
private static void literalExpression(){
ExpressionParser parser = new SpelExpressionParser();
Boolean var = parser.parseExpression("true").getValue(Boolean.class);
System.out.println(var);
}
private static void fromImmutableObject() {
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("name.toUpperCase()");
Student stu = Student.builder().name("jonny").age(18).build();
StandardEvaluationContext context = new StandardEvaluationContext(stu);
System.out.println(expression.getValue(context));
}
private static void fromMutableObject() {
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("name.toUpperCase()");
Student stu = Student.builder().name("jonny").age(18).build();
System.out.println(expression.getValue(stu));
}
private static void autoTypeConversion(){
class Sample{
public final List<Integer> data = new ArrayList<>();
}
Sample sample = new Sample();
sample.data.add(1);
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext(sample);
parser.parseExpression("data[0]").setValue(context,"2");
System.out.println(sample.data);
}
private static void inlineList(){
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
Expression expression = parser.parseExpression("{'a','b','c'}");
System.out.println(expression.getValue(context));
}
private static void methods(){
class Functions{
public String hello(){
return "hello";
}
public String hello(String name){
return "hello "+name;
}
}
Functions functions = new Functions();
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext(functions);
System.out.println(parser.parseExpression("hello()").getValue(context, String.class));
System.out.println(parser.parseExpression("hello('jonny')").getValue(context, String.class));
}
private static void logicOperation(){
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
Expression expression = parser.parseExpression("1>2 or 1 < 2");
System.out.println(expression.getValue(Boolean.class));
}
private static void types(){
ExpressionParser parser = new SpelExpressionParser();
System.out.println(parser.parseExpression("T(java.util.UUID).randomUUID()").getValue());
}
private static void variables(){
List<Integer> numberList = Lists.list(1,2,4,5,6);
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("numberList", numberList);
System.out.println(parser.parseExpression("#numberList.?[#this % 2 == 0]").getValue(context));
}
private static void functions() throws NoSuchMethodException {
class Functions{
public String hello(){
return "hello";
}
public static String hello(String name){
return "hello "+name;
}
}
Method hello = Functions.class.getDeclaredMethod("hello", String.class);
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.registerFunction("hello",hello);
System.out.println(parser.parseExpression("#hello('jonny')").getValue(context,String.class));
}
private static void specialOperator(){
class Sample{
public String name;
}
Sample sample = new Sample();
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("sample",sample);
System.out.println(parser.parseExpression("#sample.name?:'nnull'").getValue(context,String.class));
System.out.println(parser.parseExpression("#sample?.name").getValue(context, String.class));
}
}