ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

How to create RPGLE prototype of constructor that uses java generics?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to create RPGLE prototype of constructor that uses java generics?

    Hello,

    Im trying to create an RPGLE prototype of a java constructor that uses generic's.

    Im not able to figure out how to do this. Here is the constructor:

    Transaction transaction = new Transaction();
    transaction.setAmount(amount);
    transaction.setDescription("This is the payment transaction description.");

    List<Transaction> transactions = new ArrayList<Transaction>();
    transactions.add(transaction);

  • #2
    Re: How to create RPGLE prototype of constructor that uses java generics?

    Generics are really just a syntactic feature of Java that lets you avoid having to do so much casting. The actual class works with the java.lang.Object type where it appears to be using the generic type.

    If you do this QSH command for the class that uses generics
    Code:
     javap -s java.util.Arraylist
    then you can see the actual signature of the methods. You'll see that it has java.lang.Object where the class is using a generic type.

    For example, for this method
    Code:
    public boolean add(E)
    java -s shows this:
    Code:
    Signature: (Ljava/lang/Object;)Z
    For constructors, it shows V (void) as the return type, for example ()V for this constructor:
    Code:
    public java.util.ArrayList()
    java -s shows this for that constructor:
    Code:
    Signature: ()V
    But a constructor always returns an object of the type of the class, so you would code the RPG prototype to return java.util.ArrayList.

    Here's an example of using an ArrayList with String objects.

    Code:
     * ArrayList prototypes
    D newArrayList    pr              o   extproc(*java                    
    D                                           : 'java.util.ArrayList'    
    D                                           : *constructor)            
    D add             pr              n   extproc(*java                    
    D                                           : 'java.util.ArrayList'    
    D                                           : 'add')                   
    D    obj                          o   class(*java : 'java.lang.Object')
    D                                     const                            
    D get             pr              o   class(*java : 'java.lang.Object')
    D                                     extproc(*java                    
    D                                           : 'java.util.ArrayList'    
    D                                           : 'get')                   
    D    index                      10i 0 value                            
    
     * String prototypes
    D newString       pr              o   extproc(*java                    
    D                                           : 'java.lang.String'       
    D                                           : *constructor)            
    D   value                       25a   const varying              
    D getBytes        pr            25a   varying                    
    D                                     extproc(*java              
    D                                           : 'java.lang.String' 
    D                                           : 'getBytes')        
    
     * Variables
    D al              s                   like(newArrayList)         
    D s               s                   like(newString)            
    D msg             s             52                               
     /free                                                           
          al = newArrayList();                                       
          add(al : newString('hello'));                              
          add(al : newString('world'));                              
          s = get(al : 1);  // get second element                    
          msg = getBytes(s);                                         
          dsply msg;        // displays 'world'
          // shhh, don't tell anyone I didn't free my objects                                         
          *inlr = '1';

    Comment


    • #3
      Re: How to create RPGLE prototype of constructor that uses java generics?

      I see what your saying.

      Since we're using the object class as a parm we can use basically what ever we want.

      Thank You very much Barbara.

      Your help has been invaluable.

      Comment

      Working...
      X