Using Java Reflection in PeopleCode - Part 1

When working with GetJavaClass today in PeopleCode, I got a strange Error. I was trying to invoke the "getworksheet" method in JExcel workbook class, and PeopleCode greeted me with this error;

Calling Java jxl.Workbook.getWorkbook: more than one overload matches.

Now the key is, "more than one overload matches". I'm a beginner in Java and felt that I would do some more R & D to understand why PeopleSoft is throwing this error. Information on this error is scattered across the web, but nothing helps a beginner to grasp it clearly. To make things worse, the fix requires some understanding of reflection concepts in Java which makes it bad. To make things simple for a common PeopleSoft user, let me try to explain this error and the procedure to fix this in PeopleSoft. Create a simple class file image.class as per the code below

import java.util.*;
public class image {
public static void main(String[] args){
}
public static int square( int x )
         {
         return x * x;
     }
   
     public static double square( double y )
         {
         return y * y;
     }

}

Note that we have two methods "square" with the same name but the input to those methods are different; an int and a double. Compile this file and place it in your application server class directory. Now, if you run the following code from PeopleSoft

&ars_out_l = GetJavaClass("image").square(3.5); => You will get a neat error as shown below

Calling Java image.square: more than one overload matches.
While calling the noted Java method, the PeopleCode interface found more than one method signature that could match the given parameter types.

What this means to us is PeopleSoft application layer has detected more than one overload (method) and does not know which one to "invoke". When this is in Java, Java intelligently decides which method to invoke by understanding the nature of the inputs passed to the method. Unfortunately, PeopleSoft lacks this intelligence.Now, providing this intelligence to PeopleSoft would mean, the developer will have to find a way to tell PeopleSoft to "choose" the right method to invoke depending on the programming requirements.

To help PeopleSoft choose the right one, we will use a concept called "Reflection". What reflection means in simple terms is to read the class file, its methods and help in choosing the right one to call. In the next part of this post, we will see how to select the right method for the same example and get the right output.

No comments:

Post a Comment