2010-12-03 4 views
1

MVEL의 설명서에 따르면 스크립트에서 정적 Java 메서드 인 http://mvel.codehaus.org/Programmatic+Imports+for+2.0을 가져올 수 있습니다. 다음 예제는 해당 페이지에서 가져온 것이지만 작동하지 않습니다 (오류 : 속성 (NULL 상위)에 액세스 할 수 없습니다 : 시간). 무엇이 잘못 될 수 있습니까?MVEL을 사용하여 정적 메서드를 가져올 수 없음

import java.io.Serializable; 
import org.mvel2.MVEL; 
import org.mvel2.ParserContext; 

public class Test { 

    public static void main(String[] args) { 

     ParserContext ctx = new ParserContext(); 
     try { 
      ctx.addImport("time", System.class.getMethod("currentTimeMillis", long.class)); 
     } 
     catch (NoSuchMethodException e) { 
      // handle exception here. 
     } 

     Serializable s = MVEL.compileExpression("time();", ctx); 
     Object ans = MVEL.executeExpression(s); 
     System.out.println(ans.toString()); 

    } 

} 

답변

2

getMethod의 두 번째 인수는 매개 변수 유형에 사용되며 메소드의 반환 유형에는 사용되지 않습니다.

변경이 줄이와

System.class.getMethod("currentTimeMillis", long.class) 

:

System.class.getMethod("currentTimeMillis") 
관련 문제