lundi 28 octobre 2019

At the bytecode level, how does Java's Class.getEnumConstants() know which classes are enum classes?

The Java reflection API contains a method Class.getEnumConstants() that makes it possible to determine whether or not a class is an enum class (it returns null if it doesn't think the class is an enum), and what its constants are.

I'm working on a program that generates JVM bytecode directly, and am trying to generate an enum class. As such, I need to know how Java recognises enum classes from their bytecode, so that getEnumConstants will work correctly. Obviously, the class needs to extend Enum, but that clearly isn't enough on its own (e.g. the bytecode corresponding to public class Example extends Enum<Example> {} will not be recognised as an enum); what other features does the JVM bytecode for a class need to have so that Java's reflection API will recognise it as a Java enum, and be able to determine its enum constants?





Aucun commentaire:

Enregistrer un commentaire