Advanced question about generics

Discussion in 'Java' started by Vincent Cantin, Nov 25, 2004.

  1. Hello,

    I have a class Server<C extends Connection> that accepts some connections
    are for each of them creates an instance of C.

    Problem : the implementation of the class need to have C.class in order to
    create its instance but it doesn't know it unless I ask the user of my class
    to give it in parameter.

    So the user of the class write something like that :

    Server server = new Server<MyConnection>(MyConnection.class);

    but I would like him to only write something like that :

    Server server = new Server<MyConnection>();

    Is there something in the language of Java 1.5 that can do implicitly what I
    want ?

    Thank you,
    Vincent
    Vincent Cantin, Nov 25, 2004
    #1
    1. Advertising

  2. Vincent Cantin

    xarax Guest

    "Vincent Cantin" <> wrote in message
    news:...
    > Hello,
    >
    > I have a class Server<C extends Connection> that accepts some connections
    > are for each of them creates an instance of C.
    >
    > Problem : the implementation of the class need to have C.class in order to
    > create its instance but it doesn't know it unless I ask the user of my class
    > to give it in parameter.
    >
    > So the user of the class write something like that :
    >
    > Server server = new Server<MyConnection>(MyConnection.class);
    >
    > but I would like him to only write something like that :
    >
    > Server server = new Server<MyConnection>();
    >
    > Is there something in the language of Java 1.5 that can do implicitly what I
    > want ?


    I believe there is nothing to do exactly what you
    want as described.

    However, you may want to consider passing a factory
    object to the constructor. The factory object will
    instantiate an object of the correct type. The factory
    object should be declared generically (via an interface
    type) so that the compiler can verify that it is delivering
    the correct type.
    xarax, Nov 25, 2004
    #2
    1. Advertising

  3. Vincent Cantin wrote:
    > So the user of the class write something like that :
    >
    > Server server = new Server<MyConnection>(MyConnection.class);
    >
    > but I would like him to only write something like that :
    >
    > Server server = new Server<MyConnection>();
    >
    > Is there something in the language of Java 1.5 that can do implicitly what I
    > want ?


    That's IMO not possible, because generics don't exist on the bytecode level,
    they're purely for the sake of the compiler. That means what gets *executed*
    in the end is:

    Server server = new Server();

    With to MyConnection added whenever a method of server is called that
    requires them.
    Michael Borgwardt, Nov 25, 2004
    #3
  4. "Vincent Cantin" <> wrote in message news:<>...
    > Hello,
    >
    > I have a class Server<C extends Connection> that accepts some connections
    > are for each of them creates an instance of C.
    >
    > Problem : the implementation of the class need to have C.class in order to
    > create its instance but it doesn't know it unless I ask the user of my class
    > to give it in parameter.
    >
    > So the user of the class write something like that :
    >
    > Server server = new Server<MyConnection>(MyConnection.class);
    >
    > but I would like him to only write something like that :
    >
    > Server server = new Server<MyConnection>();
    >
    > Is there something in the language of Java 1.5 that can do implicitly what I
    > want ?


    No, not possible because of type erasure. A flexible solution is to
    pass a factory object that create the instances, for example:

    Server server = new Server<MyConnection>(new Factory<MyConnection>() {
    public MyConnection create() {
    return new MyConnection();
    }
    });

    public interface Factory<T> {
    T create();
    }

    This has several advantages over directly calling a constructor using
    reflection.

    /Jesper Nordenberg
    Jesper Nordenberg, Nov 26, 2004
    #4
  5. Jesper Nordenberg wrote:
    > "Vincent Cantin" <> wrote in message news:<>...
    >
    >>Hello,
    >>
    >>I have a class Server<C extends Connection> that accepts some connections
    >>are for each of them creates an instance of C.
    >>
    >>Problem : the implementation of the class need to have C.class in order to
    >>create its instance but it doesn't know it unless I ask the user of my class
    >>to give it in parameter.
    >>
    >>So the user of the class write something like that :
    >>
    >> Server server = new Server<MyConnection>(MyConnection.class);
    >>
    >>but I would like him to only write something like that :
    >>
    >> Server server = new Server<MyConnection>();
    >>
    >>Is there something in the language of Java 1.5 that can do implicitly what I
    >>want ?

    >
    >
    > No, not possible because of type erasure. A flexible solution is to
    > pass a factory object that create the instances, for example:
    >
    > Server server = new Server<MyConnection>(new Factory<MyConnection>() {
    > public MyConnection create() {
    > return new MyConnection();
    > }
    > });
    >
    > public interface Factory<T> {
    > T create();
    > }
    >
    > This has several advantages over directly calling a constructor using
    > reflection.
    >
    > /Jesper Nordenberg

    Hi,
    actually it is the same as the original solution - but more verbose.
    To avoid wiriting "MyConnection" twice you should use a static generic
    method:

    public class Server<T extends Connection> {

    public static <S> Server<? extends S> create(Class<S> clazz) {
    return new Server<S>(clazz);
    }

    public Server(Class<T> c) {
    //...
    }

    }

    Client code:

    Server<MyConnection> s = Server.create(MyConnection.class);

    Cheers,
    Michal
    Michal Kleczek, Nov 26, 2004
    #5
  6. Thank you for all the answers, it was usefull for me.

    Vincent
    Vincent Cantin, Nov 26, 2004
    #6
  7. Vincent Cantin

    Adam Jenkins Guest

    Michal Kleczek <> wrote in message news:<co6vqp$7dr$>...
    > Hi,
    > actually it is the same as the original solution - but more verbose.
    > To avoid wiriting "MyConnection" twice you should use a static generic
    > method:

    [snip]
    > Client code:
    >
    > Server<MyConnection> s = Server.create(MyConnection.class);


    I still count two "MyConnection"s in that statement, so I'm not sure
    what you've gained.
    Adam Jenkins, Nov 26, 2004
    #7
  8. Michal Kleczek <> wrote in message news:<co6vqp$7dr$>...
    > Jesper Nordenberg wrote:
    > > "Vincent Cantin" <> wrote in message news:<>...
    > >
    > >>Hello,
    > >>
    > >>I have a class Server<C extends Connection> that accepts some connections
    > >>are for each of them creates an instance of C.
    > >>
    > >>Problem : the implementation of the class need to have C.class in order to
    > >>create its instance but it doesn't know it unless I ask the user of my class
    > >>to give it in parameter.
    > >>
    > >>So the user of the class write something like that :
    > >>
    > >> Server server = new Server<MyConnection>(MyConnection.class);
    > >>
    > >>but I would like him to only write something like that :
    > >>
    > >> Server server = new Server<MyConnection>();
    > >>
    > >>Is there something in the language of Java 1.5 that can do implicitly what I
    > >>want ?

    > >
    > >
    > > No, not possible because of type erasure. A flexible solution is to
    > > pass a factory object that create the instances, for example:
    > >
    > > Server server = new Server<MyConnection>(new Factory<MyConnection>() {
    > > public MyConnection create() {
    > > return new MyConnection();
    > > }
    > > });
    > >
    > > public interface Factory<T> {
    > > T create();
    > > }
    > >
    > > This has several advantages over directly calling a constructor using
    > > reflection.
    > >
    > > /Jesper Nordenberg

    > Hi,
    > actually it is the same as the original solution - but more verbose.


    No, it's not the same. The Factory solution is more flexible because
    it allows you to use any constructor of the MyConnection class, or
    even not to create a new object at all. For example:

    final MyConnection connection = getConnection();

    Server server = new Server<MyConnection>(new Factory<MyConnection>() {
    public MyConnection create() {
    return connection;
    }
    });


    Server server2 = new Server<MyConnection>(new Factory<MyConnection>()
    {
    public MyConnection create() {
    return new MyConnection("This is an argument!");
    }
    });

    This is something you can't do by just passing the MyConnection.class
    object.

    /Jesper Nordenberg
    Jesper Nordenberg, Nov 26, 2004
    #8
  9. Jesper Nordenberg wrote:
    > Michal Kleczek <> wrote in message news:<co6vqp$7dr$>...
    >
    >>Jesper Nordenberg wrote:
    >>
    >>>"Vincent Cantin" <> wrote in message news:<>...
    >>>
    >>>
    >>>>Hello,
    >>>>
    >>>>I have a class Server<C extends Connection> that accepts some connections
    >>>>are for each of them creates an instance of C.
    >>>>
    >>>>Problem : the implementation of the class need to have C.class in order to
    >>>>create its instance but it doesn't know it unless I ask the user of my class
    >>>>to give it in parameter.
    >>>>
    >>>>So the user of the class write something like that :
    >>>>
    >>>> Server server = new Server<MyConnection>(MyConnection.class);
    >>>>
    >>>>but I would like him to only write something like that :
    >>>>
    >>>> Server server = new Server<MyConnection>();
    >>>>
    >>>>Is there something in the language of Java 1.5 that can do implicitly what I
    >>>>want ?
    >>>
    >>>
    >>>No, not possible because of type erasure. A flexible solution is to
    >>>pass a factory object that create the instances, for example:
    >>>
    >>>Server server = new Server<MyConnection>(new Factory<MyConnection>() {
    >>> public MyConnection create() {
    >>> return new MyConnection();
    >>> }
    >>>});
    >>>
    >>>public interface Factory<T> {
    >>> T create();
    >>>}
    >>>
    >>>This has several advantages over directly calling a constructor using
    >>>reflection.
    >>>
    >>>/Jesper Nordenberg

    >>
    >>Hi,
    >>actually it is the same as the original solution - but more verbose.

    >
    >
    > No, it's not the same. The Factory solution is more flexible because
    > it allows you to use any constructor of the MyConnection class, or
    > even not to create a new object at all. For example:
    >
    > final MyConnection connection = getConnection();
    >
    > Server server = new Server<MyConnection>(new Factory<MyConnection>() {
    > public MyConnection create() {
    > return connection;
    > }
    > });
    >
    >
    > Server server2 = new Server<MyConnection>(new Factory<MyConnection>()
    > {
    > public MyConnection create() {
    > return new MyConnection("This is an argument!");
    > }
    > });
    >
    > This is something you can't do by just passing the MyConnection.class
    > object.
    >
    > /Jesper Nordenberg

    Hi,
    I agree with you it's more flexible, but has nothing to do with generics.
    Michal
    Michal Kleczek, Nov 29, 2004
    #9
  10. Adam Jenkins wrote:
    > Michal Kleczek <> wrote in message news:<co6vqp$7dr$>...
    >
    >>Hi,
    >>actually it is the same as the original solution - but more verbose.
    >>To avoid wiriting "MyConnection" twice you should use a static generic
    >>method:

    >
    > [snip]
    >
    >>Client code:
    >>
    >>Server<MyConnection> s = Server.create(MyConnection.class);

    >
    >
    > I still count two "MyConnection"s in that statement, so I'm not sure
    > what you've gained.

    Hi,
    It's still better than three in this statement:

    Server<MyConnection> s = new Server<MyConnection>(MyConnection.class>;

    Michal
    Michal Kleczek, Nov 29, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Natty Gur

    Re: Advanced Caching Question

    Natty Gur, Jul 13, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    354
    Natty Gur
    Jul 13, 2003
  2. Juergen Berchtel
    Replies:
    1
    Views:
    5,956
    John C. Bollinger
    May 20, 2005
  3. Stephen Waits

    best book on advanced generics?

    Stephen Waits, Apr 30, 2004, in forum: C++
    Replies:
    3
    Views:
    377
    Alexei Polkhanov
    May 1, 2004
  4. Soul
    Replies:
    0
    Views:
    497
  5. Michele Simionato
    Replies:
    1
    Views:
    579
    Lacrima
    Mar 27, 2010
Loading...

Share This Page