原始问题描述了如何使用反射在私有字段上运行方法?
班级是
public class SecureResource {
private HttpServletRequest request;
public SecureResource() {}
@Inject
public SecureResource(@Nonnull final HttpServletRequest request) {
this.request = request;
}
// more things
}
基于@Jon 的回答
(其中 resource 是对 SecureResource 相关实例的引用。)
我做了以下
Class cls = response.getResourceClass();
Object obj = cls.newInstance();
Field f = cls.getDeclaredField("request");
f.setAccessible(true);
HttpServletRequest request = (HttpServletRequest) f.get(obj);
String auth = request.getHeader("X-AUTH");
我得到request
了null
并且Field f
不为空
private javax.servlet.http.HttpServletRequest com.sunrunhome.blackbird.service.SecureResource.request
请让我知道我在哪里犯错误?