我试图反映parse(CharSequence, DateTimeFormatter)
每个扩展TemporalAccessor
该类的类中的方法。
private static final Map<Class<?>, MethodHandle> PARSE_HANDLES = synchronizedMap(new HashMap<>());
static <T extends TemporalAccessor> MethodHandle parseMethodHandle(final Class<T> clazz) {
if (clazz == null) {
throw new NullPointerException("clazz is null");
}
return PARSE_HANDLES.computeIfAbsent(clazz, k -> {
try {
final Method method = clazz.getMethod("parse", CharSequence.class, DateTimeFormatter.class);
log.debug("method: {}, {}", method, method.isAccessible());
// i don't understand; public static method is not accessible? yet it isn't.
assert method.isAccessible(); // NOT GOOD with UTs
return MethodHandles.lookup().unreflect(method);
} catch (final ReflectiveOperationException roe) {
throw new RuntimeException(roe);
}
});
}
在YearMonth
课堂上,我明白了这一点。
method: public static java.time.YearMonth java.time.YearMonth.parse(java.lang.CharSequence,java.time.format.DateTimeFormatter), false
为什么public static
无法访问方法?
请参阅文档isAccessible
:
不建议使用此方法,因为它的名称暗示它实际上指示是否禁止对Java语言访问控制的检查时,检查反射对象是否可访问。该方法可以
false
在调用者可访问的反射对象上返回。要测试此反射对象是否可访问,应使用canAccess(Object)
。
(我的重点。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句