vendredi 27 janvier 2017

Enum's values() method access level

JLS 8 states that each enum class has implicitly declared method:

public static E[] values();

So, it is public by specification.

At the same time Class.getEnumConstantsShared() method forcibly makes it accessible:

            final Method values = getMethod("values");
            java.security.AccessController.doPrivileged(
                new java.security.PrivilegedAction<Void>() {
                    public Void run() {
                            values.setAccessible(true);
                            return null;
                        }
                    });
            @SuppressWarnings("unchecked")
            T[] temporaryConstants = (T[])values.invoke(null);

I'm wondering: what's the sense?





Aucun commentaire:

Enregistrer un commentaire