Tuesday, October 14, 2014

Java Reflection in PeopleCode

Here is a simple example of how we can overcome the "more than one overload matches" error which occurs when calling an overloaded Java Class Method in PeopleCode.

Note: My understanding and learning is primarily based on Jim Marion's blog. This example is specific to my requirement and it can be easily extended as required.

Let us take an example of FileOutputStream class which has a write method which is overloaded.

Let us assume that we want to call the write method with the following parameters: public void write(byte[] b)

/* Use Reflection to call the appropriate method of the Java Class (which is an overloaded method) */

/* Create an instance of the Class */
Local JavaObject &fos = CreateJavaObject("java.io.FileOutputStream", "/tmp/" | &filename);

/******* REFLECTION ********/

/* Get a reference to a Java class instance of the method parameter(s): In this case the primitive byte array */
Local JavaObject &arrayClass = GetJavaClass("java.lang.reflect.Array");
Local JavaObject &bytArrClass = &arrayClass.newInstance(GetJavaClass("java.lang.Byte").TYPE, 0);

/* Get a reference to the method we want to call using Reflection */
Local JavaObject &methodArgTypes = CreateJavaObject("java.lang.Class[]", &bytArrClass.getClass());
Local JavaObject &methodReference = &fos.getClass().getMethod("write", &methodArgTypes);

/* Call the method */
Local any &result = &methodReference.invoke(&fos, CreateJavaObject("java.lang.Object[]", &byteArray));
/******* REFLECTION ********/

/* Close File Object */
&fos.close();

6 comments:

  1. Sasank I have similar issue but with fileinputstream. The issue is that fileinputstream has 3 mehtods and the one I specifically need is the one with input type file ANy help?
    Thank you

    Local JavaObject &jFile = CreateJavaObject("java.io.File", &1094C_FullPath);
    Local JavaObject &jFileReader = CreateJavaObject("java.io.FileInputStream", &jFile);
    Local JavaObject &messageDigest = GetJavaClass("java.security.MessageDigest").getInstance("MD5");
    Local JavaObject &jByteArray = CreateJavaArray("byte[]", 1024);
    /* Read Input File Byte by Byte */
    Local number &jByteCount = &jFileReader.read(&jByteArray);
    While &jByteCount > 0
    &messageDigest.update(&jByteArray, 0, &jByteCount);
    &jByteCount = &jFileReader.read(&jByteArray);
    End-While;
    /* Perform MD5 Digest */
    Local JavaObject &jMD5Array = &messageDigest.digest();
    /* Convert to Hexstring */
    Local JavaObject &DatatypeConverter = GetJavaClass("javax.xml.bind.DatatypeConverter");
    Local string &encodehex = &DatatypeConverter.printHexBinary(&jMD5Array);
    Local number &filesize = &jFile.length();

    ReplyDelete
    Replies
    1. Sorry for the delay in responding. What is your error message exactly?

      When I tried your code on a PT 8.54.08 environment, I did not receive any overloaded method errors.

      Delete
    2. Also, you could refer Jim Marion's approach to using server side scripting to workaround the java overloaded method errors. It is a much neater solution if it works for you instead of dealing with the reflection.

      http://jjmpsj.blogspot.com/2015/09/javascript-on-app-server-scripting.html

      Delete
  2. Hi Sasank, after reading a lot of blogs, I finaly decide to ask your help !
    I have to load a file (pdf) from a NAS, put it in a HashMap and pass the HashMap to a java method to send it via xmlrpc protocol to a service.
    The problem is that the file has to be load in byte in the map, that I'm trying to do, but the service doesn't recognize the file format.
    Could you please tell me how should i modify my code to do that ?

    Local array of JavaObject &a_docs = CreateArrayRept(CreateJavaObject("java.util.HashMap"), 0);

    Local JavaObject &jDocArray = CreateJavaArray("java.lang.Object[]", &rsDoc.ActiveRowCount);

    &jDoc = CreateJavaObject("java.util.HashMap");

    &SysFileName = &rs(1).PV_ATTACHMENTS.ATTACHSYSFILENAME.Value;
    &UsrFileName = &rs(1).PV_ATTACHMENTS.ATTACHUSERFILE.Value;

    rem here read file into buffer byte array;
    Local JavaObject &buf = CreateJavaArray("byte[]", 1024);
    Local JavaObject &bytes = CreateJavaObject("java.io.ByteArrayOutputStream");
    Local number &byteCount;
    Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", &pathFTP | &SysFileName);

    &byteCount = &in.read(&buf);

    While &byteCount > 0
    &bytes.write(&buf, 0, &byteCount);
    &byteCount = &in.read(&buf);
    End-While;
    &in.close();

    &jDoc.put("content", &bytes.toByteArray());
    &a_docs.Push(&jDoc);

    Thanks in advance.
    Regards

    ReplyDelete
    Replies
    1. Hi Franck - Sorry for the delay. To be honest I have not worked much with Hash Maps let alone documents with Hash Maps. That said, I have a couple of questions:
      - Not sure why you are adding the hash map into an array?
      - Also, does the service provider have any sample java code that they/you can share? It would be a lot easier to try to replicate the code in PeopleCode if we have a sample to work with!

      Delete
  3. We are experiencing a problem with reflection during response.
    &jgetResponseCode.invoke(&jconn, CreateJavaObject("java.lang.Object[]"));

    Error log shows the following:
    Java Exception: java.lang.reflect.InvocationTargetException: during call of java.lang.reflect.Method.invoke. (2,763) AHPY_I037_AE.DOWNLOAD.GBL.default.1900-01-01.HTTPSv2.OnExecute PCPC:2835 Statement:21 (0,0)
    GetNextStateRecord [68] Exception logged: RC=3.
    AePcdExecutePeopleCode [235] Exception logged: RC=100.

    Any help is appreciated

    ReplyDelete