Created
April 15, 2015 13:31
-
-
Save bastman/e3334af833262c328be6 to your computer and use it in GitHub Desktop.
ClientFactory - Generics Example. Create Instances using Reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ClientFactory { | |
public static <T extends Client> T createClient(Class<T> clientClass, String host, Integer port) | |
{ | |
try { | |
Constructor<T> constructor = clientClass.getConstructor( | |
String.class, Integer.class | |
); | |
T client = constructor.newInstance( | |
host, port | |
); | |
return client; | |
} catch (Exception e) { | |
throw new RuntimeException("Unable to create client: " + e.getClass().getName()+" "+e.getMessage(), e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment