mercredi 30 mars 2016

Interpreted language in Java and calls to Java methods

I have implemented simple interpreted language with dynamic typing in Java. Unfortunately I ran into the following problem. When testing the following code:

def main() {
    def ks = Map[[1, 2]].keySet();
    return ks.size();
}

I stumbled upon the following exception:

java.lang.IllegalAccessException: class is not public: java.util.HashMap$KeySet.size()int/invokeSpecial

Of course This is true and caused by the fact that HashMap$KeySet class has "package" visibility. This means that when I call it's "size()" method, I call method from class that is not visible to my code. Java avoids this problem easily - method keySet() returns value of type Set, so method size() used is declared in public and abstract class "Set".

My question is: does anyone has an idea, how this should be handled in generic way? By "general" case I mean not only this simple case, where I can walk through whole inheritance chain and find "first declaration" of this method, but also pathological cases like the following:

interface I1 {
    public void foo();
}
interface I2 {
    public void foo();
}
interface I3 {
    public void foo();
}
class C implements I1, I2, I3 {
    public void foo() { .... }
}

My current impression is that I could ignore those pathological cases and select any matching method on the grounds that if such object exists, then it's creation was successful, so it's compilation was successful, so all these methods have identical signatures and since in Java there is no way to specify different implementations of these methods depending on how object is viewed (as I1, I2 or I3), then result will be always the same.

Any help will be appreciated.





Aucun commentaire:

Enregistrer un commentaire