OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.example.jni_generator; |
| 6 |
| 7 // This class serves as a reference test for the bindings generator, and as exam
ple documentation |
| 8 // for how to use the jni generator. |
| 9 // The C++ counter-part is sample_for_tests.cc. |
| 10 // jni_generator.gyp has a jni_generator_tests target that will: |
| 11 // * Generate a header file for the JNI bindings based on this file. |
| 12 // * Compile sample_for_tests.cc using the generated header file. |
| 13 // * link a native executable to prove the generated header + cc file are self
-contained. |
| 14 // All comments are informational only, and are ignored by the jni generator. |
| 15 class SampleForTests { |
| 16 // Classes can store their C++ pointer counter part as an int that is normally
initialized by |
| 17 // calling out a nativeInit() function. |
| 18 int nativePtr; |
| 19 |
| 20 // You can define methods and attributes on the java class just like any other
. |
| 21 // Methods without the @CalledByNative annotation won't be exposed to JNI. |
| 22 public SampleForTests() { |
| 23 } |
| 24 |
| 25 public void startExample() { |
| 26 // Calls native code and holds a pointer to the C++ class. |
| 27 nativePtr = nativeInit("myParam"); |
| 28 } |
| 29 |
| 30 public void doStuff() { |
| 31 // This will call CPPClass::Method() using nativePtr as a pointer to the o
bject. This must be |
| 32 // done to: |
| 33 // * avoid leaks. |
| 34 // * using finalizers are not allowed to destroy the cpp class. |
| 35 nativeMethod(nativePtr); |
| 36 } |
| 37 |
| 38 public void finishExample() { |
| 39 // We're done, so let's destroy nativePtr object. |
| 40 nativeDestroy(nativePtr); |
| 41 } |
| 42 |
| 43 // ---------------------------------------------------------------------------
-------------------- |
| 44 // The following methods demonstrate exporting Java methods for invocation fro
m C++ code. |
| 45 // Java functions are mapping into C global functions by prefixing the method
name with |
| 46 // "Java_<Class>_" |
| 47 // This is triggered by the @CalledByNative annotation; the methods may be nam
ed as you wish. |
| 48 |
| 49 // Exported to C++ as: |
| 50 // Java_Example_javaMethod(JNIEnv* env, jobject obj, jint foo, jint bar) |
| 51 // Typically the C++ code would have obtained the jobject via the Init() call
described above. |
| 52 @CalledByNative |
| 53 public int javaMethod(int foo, |
| 54 int bar) { |
| 55 return 0; |
| 56 } |
| 57 |
| 58 // Exported to C++ as Java_Example_staticJavaMethod(JNIEnv* env) |
| 59 // Note no jobject argument, as it is static. |
| 60 @CalledByNative |
| 61 public static boolean staticJavaMethod() { |
| 62 return true; |
| 63 } |
| 64 |
| 65 // No prefix, so this method is package private. It will still be exported. |
| 66 @CalledByNative |
| 67 void packagePrivateJavaMethod() {} |
| 68 |
| 69 // Note the "Unchecked" suffix. By default, @CalledByNative will always genera
te bindings that |
| 70 // call CheckException(). With "@CalledByNativeUnchecked", the client C++ code
is responsible to |
| 71 // call ClearException() and act as appropriate. |
| 72 // See more details at the "@CalledByNativeUnchecked" annotation. |
| 73 @CalledByNativeUnchecked |
| 74 void methodThatThrowsException() throws Exception {} |
| 75 |
| 76 //----------------------------------------------------------------------------
-------------------- |
| 77 // Java fields which are accessed from C++ code must be annotated with @Access
edByNative to |
| 78 // prevent them being eliminated when unreferenced code is stripped. |
| 79 @AccessedByNative |
| 80 private int javaField; |
| 81 |
| 82 //----------------------------------------------------------------------------
-------------------- |
| 83 // The following methods demonstrate declaring methods to call into C++ from J
ava. |
| 84 // The generator detects the "native" and "static" keywords, the type and name
of the first |
| 85 // parameter, and the "native" prefix to the function name to determine the C+
+ function |
| 86 // signatures. Besides these constraints the methods can be freely named. |
| 87 |
| 88 // This declares a C++ function which the application code must implement: |
| 89 // static jint Init(JNIEnv* env, jobject obj); |
| 90 // The jobject parameter refers back to this java side object instance. |
| 91 // The implementation must return the pointer to the C++ object cast to jint. |
| 92 // The caller of this method should store it, and supply it as a the nativeCPP
Class param to |
| 93 // subsequent native method calls (see the methods below that take an "int nat
ive..." as first |
| 94 // param). |
| 95 private native int nativeInit(); |
| 96 |
| 97 // This defines a function binding to the associated C++ class member function
. The name is |
| 98 // derived from |nativeDestroy| and |nativeCPPClass| to arrive at CPPClass::De
stroy() (i.e. native |
| 99 // prefixes stripped). |
| 100 // The |nativeCPPClass| is automatically cast to type CPPClass* in order to ob
tain the object on |
| 101 // which to invoke the member function. |
| 102 private native void nativeDestroy(int nativeCPPClass); |
| 103 |
| 104 // This declares a C++ function which the application code must implement: |
| 105 // static jdouble GetDoubleFunction(JNIEnv* env, jobject obj); |
| 106 // The jobject parameter refers back to this java side object instance. |
| 107 private native double nativeGetDoubleFunction(); |
| 108 |
| 109 // Similar to nativeGetDoubleFunction(), but here the C++ side will receive a
jclass rather than |
| 110 // jobject param, as the function is declared static. |
| 111 private static native float nativeGetFloatFunction(); |
| 112 |
| 113 // This function takes a non-POD datatype. We have a list mapping them to thei
r full classpath in |
| 114 // jni_generator.py JavaParamToJni. If you require a new datatype, make sure y
ou add to that |
| 115 // function. |
| 116 private native void nativeSetNonPODDatatype(Rect rect); |
| 117 |
| 118 // This declares a C++ function which the application code must implement: |
| 119 // static ScopedJavaLocalRef<jobject> GetNonPODDatatype(JNIEnv* env, jobject
obj); |
| 120 // The jobject parameter refers back to this java side object instance. |
| 121 // Note that it returns a ScopedJavaLocalRef<jobject> so that you don' have to
worry about |
| 122 // deleting the JNI local reference. This is similar with Strings and arrays. |
| 123 private native Object nativeGetNonPODDatatype(); |
| 124 |
| 125 // Similar to nativeDestroy above, this will cast nativeCPPClass into pointer
of CPPClass type and |
| 126 // call its Method member function. |
| 127 private native int nativeMethod(int nativeCPPClass); |
| 128 |
| 129 // Similar to nativeMethod above, but here the C++ fully qualified class name
is taken from the |
| 130 // comment rather than parameter name, which can thus be chosen freely. |
| 131 private native double nativeMethodOtherP0(int nativeCPPClass /* cpp_namespace:
:CPPClass */); |
| 132 |
| 133 // An inner class has some special attributes for annotation. |
| 134 class InnerClass { |
| 135 @CalledByNative("InnerClass") |
| 136 public float JavaInnerMethod() { |
| 137 } |
| 138 |
| 139 @CalledByNative("InnerClass") |
| 140 public static void javaInnerFunction() { |
| 141 } |
| 142 |
| 143 @NativeCall("InnerClass") |
| 144 private static native int nativeInnerFunction(); |
| 145 |
| 146 @NativeCall("InnerClass") |
| 147 private static native String nativeInnerMethod(int nativeCPPClass); |
| 148 |
| 149 } |
| 150 } |
OLD | NEW |