MrDuk
MrDuk

Reputation: 18242

How do you pass a reference to type 'class'?

I have a method that registers classes for serialization. I want to call this from a control class, something like:

    public static void main(String[] args) {
        Registrar.RegisterClass(Control.SomeRequest);
        Registrar.RegisterClass(Control.SomeResponse);

        Sender testServer = new Sender();
        Receiver testClient = new Receiver();

        testServer.StartServer();
        testClient.StartClient();
    }

    public static class SomeRequest {
        public String text;
    }
    public static class SomeResponse {
        public String text;
    }
---------------------------------------------
public class Registrar {
    static Kryo kryo = client.getKryo();

    public static void RegisterClass(??? cls){
        kryo.register(cls.class);
    }
}

Alternatively, I could pass in Control.SomeRequest.class just as easy, though I'm not sure how to achieve either of these.

Upvotes: 0

Views: 38

Answers (1)

gefei
gefei

Reputation: 19766

The class name of "classes" is Class

public static void RegisterClass(Class cls){
        kryo.register(cls);
}

Upvotes: 3

Related Questions