dimanche 16 février 2020

What could cause Class#newInstance to block a thread until killed?

I'm having an issue in Java where a generic class cannot be instantiated using reflection and blocks the CompletableFuture asynchronous thread it is running on. I'm not sure what could cause this without any exceptions being thrown.

The class that I'm attempting to instantiate has a single, public, blank constructor (where normally it doesn't specify any):

    public UserInfo() {
        System.out.println("New instance"); // TODO
    }

The reflection code:

    public static <T extends InfoCore> CompletableFuture<T> get(Class<T> infoClass, PlayerInfo playerInfo) {
        System.out.println("Getting an Info Core"); // TODO
        return CompletableFuture.supplyAsync(() -> {
            System.out.println("Supplying the info core async thread"); // TODO
            try {
                System.out.println("Trying"); // TODO
                T instance = infoClass.newInstance();
                System.out.println("Got new instance"); // TODO
                instance.setPlayerInfo(playerInfo);
                System.out.println("Set player info in the instance"); // TODO
                instance.load();
                System.out.println("Loaded the instance"); // TODO
                return instance;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("Error occurred instantiating an InfoCore");
            }
        });
    }

I receive all System.out messages up until "Trying". The "Got new instance" message is never printed and the entire async thread freezes indefinitely (I've had to kill the process every time). I have also attempted to use Class#getConstructor(new Class[0]).newInstance() with the same result.

Does anyone know why this might be happening?





Aucun commentaire:

Enregistrer un commentaire