0

I have this big problem on my code that I can't create an instance of my generic class :( My goal was a server, that can handle different kinds of Client-Sockets.

public class Server<T extends AClientSocket> implements Runnable{
//... some other code
private void newClient() throws Exception
{
    System.out.println("[SERVER] Auf neuen Client warten...");
    Socket clientSocket = serverSocket.accept();
    clients.add(new T(clientSocket)); //Compile Error :(
    new Thread((clients.get(clients.size()-1))).start();
}
//... some more code
public static void main(String[] args) throws Exception
{
    Server<ClientSocketHTTP> server = new Server<ClientSocketHTTP>(8000);
    //... different code
}
}

"AClientSocket" is an abstract class with a defined constructor.

The Solution:

//... some code
private final Function<Socket, T> clientCreator;
public Server(int port, Function<Socket, T> clientCreator) throws Exception
{
    PORT = port;
    serverSocket = new ServerSocket(PORT);
    sockets = new ArrayList<T>();
    this.clientCreator = clientCreator;
}
//... some code
private void newClient() throws Exception
{
    System.out.println("[SERVER] Auf neuen Client warten...");
    Socket clientSocket = serverSocket.accept();
    System.out.println("[SERVER] Client gefunden...");
    sockets.add(clientCreator.apply(clientSocket));
    new Thread((sockets.get(sockets.size()-1))).start();
    System.out.println("[SERVER] Client hinzugefügt...");
}
//... some code
public static void main(String[] args) throws Exception
{
    Server<ClientSocketHTTP> server = new Server<ClientSocketHTTP>(8000, clientSocket -> {
        try {
            return new ClientSocketHTTP(clientSocket);
        } catch (Exception e1) {
            System.exit(0);
            return null;
        }
    });
//... code

1 Answer 1

6

You don't, and newing up instances like this is the source of all sorts of design headaches. Instead, pass a Function<Socket, T> to the class, and have newClient() call clientCreator.apply(clientSocket). If the implementation is as simple as creating a new instance, you can just pass ClientSocketHttp::new.

Sign up to request clarification or add additional context in comments.

11 Comments

Given the OP's code, it looks like they want a Function<Socket, T>, not a Supplier<T>.
Thanks for the help! What do you mean with "clientCreator.apply(clientSocket)"? Could you explain me more about that? Would appreciate! :)
@AlwinBrauns If the answer solved your problem, mark it as "accepted" by clicking the green checkmark on the left and optionally clicking the up arrow.
I will give it a try tomorrow and if it worked I will do that! Thanks
@chrylis-cautiouslyoptimistic- private class ClientSocketCreator implements Function<Socket, T> { @Override public T apply(Socket arg0) { return null; // How??? I want (new T(arg0)) } } private void newClient() throws Exception { System.out.println("[SERVER] Auf neuen Client warten..."); Socket client = serverSocket.accept(); clientSocket.add(clientCreator.apply(client)); new Thread((clientSocket.get(clientSocket.size()-1))).start(); } Sorry for the formatting im new here xD
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.