1 Reply Latest reply on Sep 17, 2010 7:06 PM by jschellSomeoneStoleMyAlias

    DatagramChannel and ServerSocketChannel question

      For some reasons I need to switch between DatagramChannel and ServerSocketChannel. My program will use functions e.g. open(), socket(), etc. However, sometimes they do not use the same interface; for instance, DatagramChannel.socket() returns DatagramSocket whereas ServerSocketChannel.socket() ServerSocket. At the moment I make use of reflection to dynamic calling underlying functions.
      ServerSocketChannel ssc = ServerSocketChannel.open();
      obj.getClass().getMethod("open", null).invoke(null, null);
      if (obj instanceof DatagramChannel){
          ((DatagramChannel)obj).socket().getClass().getMethod("setReuseAddress", Boolean.TYPE).invoke(((DatagramChannel)Obj).socket(), new Boolean("true"));
      }else if (obj instanceof ServerSocketChannel){
          .... // similar action
      This looks ugly because there are some other method executions (e.g. ServerSocketChannel.socket().configureBlocking(...) ) staying on the other part of the class/ program. So my question - is there any pattern that may simplify such kind of usage?

      I appreciate any suggestion.