Advanced native Java code interfacing

If you have read Interfacing with native Java code: Creating externs, and you have more advanced uses to interface with native Java code, you are in the right place!

Checked exceptions (throws ...)


Since Haxe doesn't have checked exceptions, these aren't first-class citizens in the Haxe world. This problem will exist until the bytecode target is added. When you encounter a checked exception, you can either:
  • "Transform" it into an unchecked exception.
    This is very simple:
    NativeClass.doCompilerCheckedException();
    

will have to be written as:

try
{
    NativeClass.doCompilerCheckedException();
}
catch(e:Dynamic)
{
    throw e;
}

which you can shorten by removing the braces:

try NativeClass.doCompilerCheckedException() catch(e:Dynamic) throw e;

By default, Haxe will turn all dynamically typed objects into unchecked exceptions.

  • Mark the function that calls checked exception as "throws TheCheckedException".
    You can do that with the @:throws metadata:
@:throws("path.of.CheckedException")
function haxeFunction()
{
    NativeClass.doCompilerCheckedException();
}

Member visibility


By default, Haxe makes all declared java fields public. If you need to extend a native protected or internal class, you can use the @:protected or the @:internal member metadatas

Extra modifiers


Also you can use @:final, @:volatile and @:transient metadatas, which refer all to the native java counterparts

Unsafe inline Java code


If you need to use inline Java code for some reason, you can use untyped __java__("//java code here"); magic construct.
If you need to replace the whole function body, you can use the @:functionCode metadata, and if you need to add new Java members, you can use the @:classCode metadata.
Please be aware that when the bytecode targets are released, your code won't be compatible with it if yo
version #19871, modified 2013-12-31 11:44:52 by AndyLi