Kiểu này mà không phải là dễ dàng. Dưới đây là một phương pháp mà các cuộc gọi một phương pháp tĩnh:
public static Object callStaticMethod(
// class that contains the static method
final Class<?> clazz,
// method name
final String methodName,
// optional method parameters
final Object... parameters) throws Exception{
for(final Method method : clazz.getMethods()){
if(method.getName().equals(methodName)){
final Class<?>[] paramTypes = method.getParameterTypes();
if(parameters.length != paramTypes.length){
continue;
}
boolean compatible = true;
for(int i = 0; i < paramTypes.length; i++){
final Class<?> paramType = paramTypes[i];
final Object param = parameters[i];
if(param != null && !paramType.isInstance(param)){
compatible = false;
break;
}
}
if(compatible){
return method.invoke(/* static invocation */null,
parameters);
}
}
}
throw new NoSuchMethodException(methodName);
}
Cập nhật: Chờ, tôi chỉ thấy thẻ gwt về câu hỏi này. Bạn không thể sử dụng phản ánh trong GWT
Nguồn
2011-02-02 10:09:47
và làm thế nào để bắn constructor mặc định hoặc một số phương pháp trong lớp đó? –
thông tin cập nhật ... –
xin lỗi tôi quên thêm về bộ công cụ web google và các công cụ, im sử dụng bộ công cụ web google và nó không hỗ trợ phản ánh. –