2012-09-20 2 views
1

아파치에서 속도 가이드 재산 조회 규칙은 다음과 같습니다 ADRESS 재산아파치 속도 사용자 정의 Uberspect

getaddress() 
getAddress() 
get("address") 
isAddress() 

내가 가변 인자 개체 매개 변수를 허용하는 규칙을 얻을 변경하려면 ($ object.address)에 대한

. 그래서 내가이 propably 사용자 정의 ubespector를 작성하여 수행 할 수 있습니다 알고

get(String name) 

하지만

get(String name, Object...params) 

를 호출하지 않습니다. 그러나 나는 어떻게 될지 전혀 모른다.

답변

2

마지막 2 일 동안 파기 한 후 맞춤형 Uberspector를 만들 수있었습니다. 해당 메서드가 발견되지 않으면 "run (String, Object [])"메서드를 호출합니다. 표준

import java.lang.reflect.Method; 
import org.apache.velocity.util.introspection.Info; 
import org.apache.velocity.util.introspection.UberspectImpl; 
import org.apache.velocity.util.introspection.VelMethod; 

public class CustomUberspector extends UberspectImpl { 

public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) 
     throws Exception { 

     if (obj == null) return null; 

     VelMethod vm = super.getMethod(obj, methodName, args, i); 
     if(vm != null) return vm; 

     Object[] iargs = {methodName, new Object[] {}} ; 
     Method m = introspector.getMethod(obj.getClass(), "run", iargs); 
     if (m != null) return new PortalVelMethodImpl(m, methodName); 

     return null; 
} 

public class PortalVelMethodImpl extends VelMethodImpl { 
    final Method method; 
    final String name; 

    public PortalVelMethodImpl(Method m, String methodName) { 
     super(m); 
     method = m; 
     name = methodName;    
    } 

    protected Object doInvoke(Object o, Object[] actual) throws Exception { 
     /* "run" method argumens are String and Object[] so we need to get plain Object[] array 
     * From [Arg0,[Arg1..ArgN] to [Arg0,Arg1..ArgN] 
     * */ 
     List<Object> args = new ArrayList<Object>(); 
     if(actual.length >= 1) { 
      args.add(actual[0]); 

      if(actual.length >= 2) { 
       Object[] nestedArgs = (Object[])actual[1]; 
       for(int i=1; i<nestedArgs.length; i++) args.add(nestedArgs[i]); 
      } 
     } 

     return method.invoke(o, name, args.toArray()); 
    } 

} 

}

CustomUbersector

run(name, new Object[]{}); 

"(String)를 얻을"호출

관련 문제