mardi 21 août 2018

Java initialising classes with populators in their constructor

I have a repository of classes in a package, in their constructor, they populate a component of the server.

For example:

The master class:

public abstract class MessageDecoder<T> {

    public MessageDecoder(boolean logicType, Integer...opcodes) {
        for (int opcode : opcodes) {
            GameConstants.RELEASE.register(logicType, opcode, this);
        }
    }

    /**
     * Get the encoded message to send to the client.
     * 
     * @param the {@link GamePacket} ready to be read.
     * @return a constructed message ready to be handled.
     */
    public abstract T decode(GamePacket packet);

}

And the repoistory classes look like:

public final class PlayerWalkPathMessageDecoder extends MessageDecoder<PlayerWalkPathMessage> {

    public PlayerWalkPathMessageDecoder() {
        super(true, 8, 58);
    }

    @Override 
    public PlayerWalkPathMessage decode(GamePacket packet) {

        return null;
    }

}

In the master class you can see how the method

register(logicType, opcode, this); 

is being used to register the class to a

map < Integer, MessageDecoder< ? > >

But during runtime, the map will not be populated because the repository classes have not been constructed yet.

This warrents me using an empty iterator to get the classes to be constructed:

Iterate.classes("bin/com/web/net/decoders", clazz -> {


});

Is there a better way to construct the classes located in a repository package?





Aucun commentaire:

Enregistrer un commentaire