lundi 13 juin 2016

Java: Is a reflection called the same every time on every machine?

This is an odd question. I have a method that populates a list with a collection of classes using java reflection.

    import org.reflections.Reflections;
    ...
    Reflections reflections = new Reflections("network.packet");
    Class<?>[] packets = new Class<?>[256];
    for (Class<?> c : reflections.getSubTypesOf(Packet.class))
    {
        try {
            byte opcode = ((Packet)c.newInstance()).getOpcode();
            packets[opcode] = c;
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

GetOpcode returns a byte registering a byte to a packet. I'm wondering if i can make my code more API friendly, by offering a automatic solution to opcode mapping. Currently I have in the Classes that extend Packet force a call the super constructor with a byte registering an opcode.

setOpcode(byte num)

that sets an opcode for this class.

My problem lyes in whether or not I can trust org.reflection to run the same way every-time on every machine to calculate the codes rather then having them hard coded, and making users of the API not step on already existing codes.

So, does anyone know?





Aucun commentaire:

Enregistrer un commentaire