Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (29 sloc) 1.55 KB

Native methods in Java

See for example workflow of developing new native methods

If you check java sources (original java.* classes) you will see many methods declared as native and which should be implemented outside of java (for example in c for oracle jvm). PyJVM is implemented in python so there is no surprise natives are implemented in python as well.

For example, open source code for java.lang.System and find method currentTimeMillis(). It is declared as:

public static native long currentTimeMillis();

PyJVM supports these methods in predefined fashion.

  • Static and instance native methods are handled in the same way
  • Class name, method name and method signature are concatenated with "_"
  • Characters / ( ) [ ; . are replaced with "_"
  • Result is interpreted as python function to call with two parameters: current frame and arguments (see for details)
  • Python function is allowed to change stack to push result to (according to Oracle documentation)

For example: on call of System.currentTimeMillis() python function to be executed is

def java_lang_System_currentTimeMillis___J(frame, args):

Python functions should be added under pyjvm/platform - all implementations must follow this convention, e.g. folder structure same as package name and lowercased py source code file.

When specific native call is not yet implemented, application will shutdown with exception:

Exception: Op (SOME_NAME) is not yet supported in natives

SOME_NAME is the name of python function to be implemented.