Question
· Aug 23, 2018

Problem with generic classes using Java Gateway

Hello,

Currently, I'm working with a Java Gateway, and I have the following problem. When importing a class into IRIS, which uses methods from a generic class, I can not find the general methods I need. I'm talking about the test2 method from the class test. How can I import the java method that returns a generic?

public class Generic<T1, T2> {

        T1 object1;
        T2 object2;

   public Generic(T1 one, T2 two) {
            object1 = one;
            object2 = two;
        }

        public T1 getFirst() {
            return object1;
        }

        public T2 getSecond() {
            return object2;
        }
}
public class Test {

    public Test(){

    }
    public static Generic<String,Integer> test2(){

        return new Generic<String, Integer>("hello ", 5);
    }

    public static String main() {
        Generic<Integer, String> pair = new Generic(6, " Apr");
        return pair.getFirst() + pair.getSecond();
    }
}

Generated class:

Class User.Test Extends java.lang.Object [ ProcedureBlock ]
{

 Parameter IMPORTTIMESTAMP As STRING = "2018-08-23 12:42:53.397";

 Method %OnNew(ByRef p0 As %ObjectHandle) As %Status
 {
     Quit:'$D(p0) $$$OK
     Quit ..%Constructor(p0,"Test",0)
 }

 ClassMethod main(ByRef p0 As %ObjectHandle) As %ObjectHandle
 {
     Quit ..%SR(p0,"main","Test")
 }

}

Regards,
Gevorg Arutunyan

Discussion (2)1
Log in or sign up to continue

While it is true that Generics are not supported by the Java Gateway I would like to provide a little more guidance on why that is the case and how to deal with this not uncommon situation.

The Java Gateway (and the dotNet Gateway by the way) allow you to interface between ObjectScript and another language environment and obviously there are concepts in one language foreign to the other. Our approach to the Gateways is to support basic structures and concepts which are similar in both languages or easily translated between them. A "complete" approach would not only take very long to implement, would be hard to verify for correctness and because of the rules you had to apply not easy to understand. It would defeat the basic rule "power through simplicity".

Generics is a case which is not easily mapped and therefore not supported. Of course you can come up with ideas how to support such a concept with what the server-side environment has to offer, but keep in mind what I wrote above.

 Every time you encounter such a situation you find yourself in a situation where the Java API you would like to call from our side of the fence is too complex and properly includes functionality you don't want to interact with in its entirety.

The Facade pattern allows you to easily work around this challenge. Build a basic API around the functionality you want to interact with on the Java side and import your handwritten entry point. This makes the interface much more manageable for the gateway and all the participating parties as the contract between Java and ObjectScript is very clear with this pattern.

Hope that helps,

Stefan