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