Simn / genjvm

13 stars 1 forks source link

Method name clashes #50

Open Simn opened 5 years ago

Simn commented 5 years ago

Since all classes implicitly inherit from java.lang.Object at run-time, we have to deal with name clashes. The following methods are final and have to be escaped:

Note that the signature is relevant and we only have to escape if a method matches in both name and signature.

The following methods are not final and might be overridden:

The question here is what we want to do with those. We can treat toString as a separate case because it's defined in Haxe with the same semantics, so we always want to allow overriding it in classes. For the others, there are two options:

  1. Escape by default, allow to not-escape (and thus override) with metadata.
  2. Don't escape (and thus override) by default, allow to to escape with metadata.

Not sure which option is better. From a pragmatic point of view I'm leaning towards 2.


As for how to handle the escaping, I propose that we prefix with _hx_ and make sure reflection respects that. This should be a matter of stripping a leading _hx_ in some places, and adding it in some others. That's pretty annoying, but I don't think we can avoid it.

Simn commented 5 years ago

We also have to escape some enum parameter fields in addition to the java.lang.Object ones:

This should not require any reflection adjustments because there is no API to interact with enum parameters by-name.

Simn commented 4 years ago

I've learned that it's ok to have a field and method with the same name, so there's no problem with enums.

The original problem remains and is very easy to reproduce:

class Main {
    static function main() {
        new Main().wait();
    }

    function new() {}

    function wait() {}
}
Error: LinkageError occurred while loading main class haxe.root.Main
        java.lang.VerifyError: class haxe.root.Main overrides final method java.lang.Object.wait()V
Error: Command failed with error 1