tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > Expression Language > Static Methods

Static Methods 

The Spring Expression Language (SpEL) is a simple and powerful expression language which helps to query and manipulate objects at runtime. The following example shows accessing static methods using T operator.

File Name  :  
com/bethecoder/tutorials/spring3/tests/spel/StaticMethodsTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.spring3.tests.spel;

import java.util.Arrays;

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

public class StaticMethodsTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    ExpressionParser parser = new SpelExpressionParser();
    
    //Accessing Static Methods
    double random = parser.parseExpression(
        "T(Math).random() * 1729").getValue(Double.class);
    System.out.println(random);
    
    random = parser.parseExpression(
        "T(Math).ceil(  T(Math).random() * 1729 )").getValue(Double.class);
    System.out.println(random);
    
    String binary = parser.parseExpression(
        "T(Integer).toBinaryString(1729)").getValue(String.class);
    System.out.println(binary);
    
    int [] data = (int [])parser.parseExpression(
    "T(com.bethecoder.tutorials.spring3.tests.spel.SortUtility).sort({ 2, 4, 1, 3, 6, 5 })"
    ).getValue();
    System.out.println(Arrays.toString(data));

  }
}

class SortUtility {
  public static int [] sort(int [] data) {  
    Arrays.sort(data);
    return data;
  }
}
   

It gives the following output,
440.7308292727332
606.0
11011000001
[1, 2, 3, 4, 5, 6]



 
  


  
bl  br