62

After delving into the source code for the JRE library, I noticed a strangely common code structure, like this:

public int foo(double bar) {
    return foo0(bar);
}

private native int foo0(double bar);

What is the purpose of this code pattern, and why is it used instead of simply exposing the underlying native method as the public one?

3
  • 10
    Good question. You'd have to ask the language designers. Maybe when some of the early libraries were designed, they hadn't yet decided whether or not it would be possible to override a native method. Just guessing. Aug 15, 2016 at 1:16
  • 2
    The defination of "many" is subjective.
    – Raedwald
    Aug 15, 2016 at 6:53
  • 14
    As someone who wrote lots of JNI code, I have to say that I'm using the same pattern consequently - last but not least, it's nice to quickly add some System.out.println("The method is called, so why the heck doesn't it work!?!?!"); in the Java part, without having to recompile any native code.
    – Marco13
    Aug 15, 2016 at 23:46

2 Answers 2

47

The native version is just an implementation detail.

This pattern separates the public interface of the method from the actual implementation.

I see at least 5 reasons why this is useful:

  • test purpose (you can mock the java method call)
  • alternative implementation: a particular version of the java library may implement that method in pure java form without call to the native implementation (common in swing code).
  • backward compatibility: if in a new version, the method accepts some extra parameters, the original java method signature can be kept and the native implementation adapted.
  • input/output validation: most of the time, before calling the native version, the java code will do some checks on the input parameters and throw exception if needed.
  • isolation: the number of methods that directly uses the native code is limited, allowing more changes in the internal code structure.

You can probably find more advantages.

15
  • 16
    I don't see how the extra layer of indirection helps with any of this. 1) I don't think native screws with mocking. 2) Different Java implementations are free to make different methods native without conflicting with each other. 3) If the method accepts extra parameters in a new version (unlikely; that'd break backward compatilibity), the changes required are about the same either way. 4) I don't know whether that's actually true, and what about the ones where the non-native method just delegates to the native one directly? ... Aug 15, 2016 at 4:02
  • 22
    Indeed, as per stackoverflow.com/questions/10940502/… it seems that the native keyword isn't part of the method signature, so this answer doesn't actually make sense. Aug 15, 2016 at 4:37
  • 2
    native is not part of the interface.
    – user253751
    Aug 15, 2016 at 5:57
  • 5
    @OlivierSamyn: The spec does not specify whether methods are native. native might as well be part of the function body for all its visibility to outside users. native is just as hidden when the public method is native directly as it is when the native method is private and wrapped by public methods written in Java. Aug 15, 2016 at 17:02
  • 3
    @OlivierSamyn There is no "documentation point of view" because native is not part of the documented signature. javadoc ignores the native keyword. From docs.oracle.com/javase/7/docs/technotes/guides/javadoc/…: Remove "synchronized" and "native" from signatures. Javadoc generates an API specification. These two keywords do not belong in the signatures of a specification, because they are implementation-specific. The keyword "native" does not need to be documented.
    – jamesdlin
    Aug 16, 2016 at 6:43
14
private native int foo(double bar);

So, eventually this has to call a C++ function for its implementation. In particular, it'll end up calling a function with a name something like:

Java_MyClass_foo

What happens if there are multiple native foo methods with different signatures? Complications. If you do it, Java will add type information into the name of the method it looks for. But it is simply easier if you stick to non-overloaded methods.

public int foo(double bar) {
    return foo0(bar);
}

private native int foo0(double bar);

foo0 has been given a unique name, there should never be a reason to add another foo0. This keeps the implementation of the C++ simple, it never has to deal with mangled names. Even if foo eventually gains an overload, it will call foo1 instead of foo0 and the C++ JNI implementation won't have to deal with the extra complications of overloading.

2
  • 2
    That may be a reason, but a very unlikely one. Practically, there are no complications. Particularly, there will be no overloading: The javah header generator will detect ambiguities/overloaded methods, and do some name mangling to make the signatures unique. (It has to, in fact, because JNI is plain C, and not C++).
    – Marco13
    Aug 15, 2016 at 22:06
  • @Marco13, I explicitly noted that in my answer, "Java will add type information into the name of the method it looks for" and talked about mangled names. Aug 15, 2016 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.