2 Replies Latest reply on Aug 5, 2003 9:23 AM by jsimone

    Generic DTO converter

      I have the following code that takes a collection of beans and returns an array of Data Transfer Objects, sorted for HTML display.

      public static RefDTO [] convertRefs(Collection coll) {
      Set sortedSet = new TreeSet();
      Iterator i = coll.iterator();
      while (i.hasNext()) {
      Reference ref = (Reference) i.next();
      sortedSet.add(convertToReferenceDTO(ref));
      }
      RefDTO [] dtoa = new RefDTO[sortedSet.size()];
      sortedSet.toArray(dtoa);
      return dtoa;
      }

      What I would like is some generic function that takes class names or some other argument type and creates XDTO array based on arguments passed to it.

      How would I do this? Or where can I go to learn how I could accomplish this?

      Many thanks,
      Joe

        • 1. Re: Generic DTO converter
          nraghuram

          Look up java.lang.reflect .
          Given an object o , you can get its class c and its methods m[] . your dto will have the same method signatures.
          then create an instance of your dto class d. if there is a relation between your entity name and dto name, then from then entity name you should be able to get the dto name.
          loop through the method array
          for each getter method in your bean you can call the corresponding setter method in your dto.


          • 2. Re: Generic DTO converter

            Thanks! I will investigate.

            Joe