mardi 28 janvier 2020

Why a public static method is not accessible?

I'm trying to reflect the parse(CharSequence, DateTimeFormatter) methods from classes which each extends the TemporalAccessor class.

    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);
            }
        });
    }

With the YearMonth class, I got this.

method: public static java.time.YearMonth java.time.YearMonth.parse(java.lang.CharSequence,java.time.format.DateTimeFormatter), false

Why a public static method is not accessible?





Aucun commentaire:

Enregistrer un commentaire