OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_ANDROID_JNI_ANDROID_H_ | 5 #ifndef BASE_ANDROID_JNI_ANDROID_H_ |
6 #define BASE_ANDROID_JNI_ANDROID_H_ | 6 #define BASE_ANDROID_JNI_ANDROID_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 | 10 |
11 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
12 #include "base/atomicops.h" | |
12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 namespace android { | 16 namespace android { |
16 | 17 |
17 // Used to mark symbols to be exported in a shared library's symbol table. | 18 // Used to mark symbols to be exported in a shared library's symbol table. |
18 #define JNI_EXPORT __attribute__ ((visibility("default"))) | 19 #define JNI_EXPORT __attribute__ ((visibility("default"))) |
19 | 20 |
20 // Contains the registration method information for initializing JNI bindings. | 21 // Contains the registration method information for initializing JNI bindings. |
21 struct RegistrationMethod { | 22 struct RegistrationMethod { |
(...skipping 28 matching lines...) Expand all Loading... | |
50 // Use HasClass if you need to check whether the class exists. | 51 // Use HasClass if you need to check whether the class exists. |
51 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name); | 52 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name); |
52 | 53 |
53 // Similar to the above, but the caller is responsible to manage the jclass | 54 // Similar to the above, but the caller is responsible to manage the jclass |
54 // lifetime. | 55 // lifetime. |
55 jclass GetUnscopedClass(JNIEnv* env, const char* class_name) WARN_UNUSED_RESULT; | 56 jclass GetUnscopedClass(JNIEnv* env, const char* class_name) WARN_UNUSED_RESULT; |
56 | 57 |
57 // Returns true iff the class |class_name| could be found. | 58 // Returns true iff the class |class_name| could be found. |
58 bool HasClass(JNIEnv* env, const char* class_name); | 59 bool HasClass(JNIEnv* env, const char* class_name); |
59 | 60 |
60 // Returns the method ID for the method with the specified name and signature. | 61 // This class is a wrapper for JNIEnv Get(Static)MethodID. |
61 // This method triggers a fatal assertion if the method could not be found. | 62 class MethodID { |
62 // Use HasMethod if you need to check whether a method exists. | 63 public: |
63 jmethodID GetMethodID(JNIEnv* env, | 64 enum MethodType { |
64 const JavaRef<jclass>& clazz, | 65 METHODTYPE_STATIC, |
65 const char* method_name, | 66 METHODTYPE_NORMAL, |
joth
2012/10/05 15:24:57
- METHOD seems redundant now this is in class Meth
bulach
2012/10/05 15:58:15
good idea joth, thanks!
done.
| |
66 const char* jni_signature); | 67 }; |
67 | 68 |
68 // Similar to GetMethodID, but takes a raw jclass. | 69 // Returns the method ID for the method with the specified name and signature. |
69 jmethodID GetMethodID(JNIEnv* env, | 70 // This method triggers a fatal assertion if the method could not be found. |
70 jclass clazz, | 71 template<MethodType method_type> |
71 const char* method_name, | 72 static jmethodID Get(JNIEnv* env, |
72 const char* jni_signature); | 73 jclass clazz, |
74 const char* method_name, | |
75 const char* jni_signature); | |
73 | 76 |
74 // Unlike GetMethodID, returns NULL if the method could not be found. | 77 // The caller is responsible to zero-initialize |atomic_method_id|. |
75 jmethodID GetMethodIDOrNull(JNIEnv* env, | 78 // It's fine to simultaneously call this on multiple threads referencing the |
76 jclass clazz, | 79 // same |atomic_method_id|. |
77 const char* method_name, | 80 template<MethodType method_type> |
78 const char* jni_signature); | 81 static jmethodID LazyGet(JNIEnv* env, |
79 | 82 jclass clazz, |
80 // Returns the method ID for the static method with the specified name and | 83 const char* method_name, |
81 // signature. | 84 const char* jni_signature, |
82 // This method triggers a fatal assertion if the method could not be found. | 85 base::subtle::AtomicWord* atomic_method_id); |
83 // Use HasMethod if you need to check whether a method exists. | 86 }; |
84 jmethodID GetStaticMethodID(JNIEnv* env, | |
85 const JavaRef<jclass>& clazz, | |
86 const char* method_name, | |
87 const char* jni_signature); | |
88 | |
89 // Similar to the GetStaticMethodID, but takes a raw jclass. | |
90 jmethodID GetStaticMethodID(JNIEnv* env, | |
91 jclass clazz, | |
92 const char* method_name, | |
93 const char* jni_signature); | |
94 | |
95 // Unlike GetStaticMethodID, returns NULL if the method could not be found. | |
96 jmethodID GetStaticMethodIDOrNull(JNIEnv* env, | |
97 jclass clazz, | |
98 const char* method_name, | |
99 const char* jni_signature); | |
100 | |
101 // Returns true iff |clazz| has a method with the specified name and signature. | |
102 bool HasMethod(JNIEnv* env, | |
103 const JavaRef<jclass>& clazz, | |
104 const char* method_name, | |
105 const char* jni_signature); | |
106 | 87 |
107 // Gets the method ID from the class name. Clears the pending Java exception | 88 // Gets the method ID from the class name. Clears the pending Java exception |
108 // and returns NULL if the method is not found. Caches results. Note that | 89 // and returns NULL if the method is not found. Caches results. Note that |
109 // GetMethodID() below avoids a class lookup, but does not cache results. | 90 // MethodID::Get() above avoids a class lookup, but does not cache results. |
110 // Strings passed to this function are held in the cache and MUST remain valid | 91 // Strings passed to this function are held in the cache and MUST remain valid |
111 // beyond the duration of all future calls to this function, across all | 92 // beyond the duration of all future calls to this function, across all |
112 // threads. In practice, this means that the function should only be used with | 93 // threads. In practice, this means that the function should only be used with |
113 // string constants. | 94 // string constants. |
114 jmethodID GetMethodIDFromClassName(JNIEnv* env, | 95 jmethodID GetMethodIDFromClassName(JNIEnv* env, |
115 const char* class_name, | 96 const char* class_name, |
116 const char* method, | 97 const char* method, |
117 const char* jni_signature); | 98 const char* jni_signature); |
118 | 99 |
119 // Gets the field ID for a class field. | 100 // Gets the field ID for a class field. |
(...skipping 24 matching lines...) Expand all Loading... | |
144 // and returns true. | 125 // and returns true. |
145 bool ClearException(JNIEnv* env); | 126 bool ClearException(JNIEnv* env); |
146 | 127 |
147 // This function will call CHECK() macro if there's any pending exception. | 128 // This function will call CHECK() macro if there's any pending exception. |
148 void CheckException(JNIEnv* env); | 129 void CheckException(JNIEnv* env); |
149 | 130 |
150 } // namespace android | 131 } // namespace android |
151 } // namespace base | 132 } // namespace base |
152 | 133 |
153 #endif // BASE_ANDROID_JNI_ANDROID_H_ | 134 #endif // BASE_ANDROID_JNI_ANDROID_H_ |
OLD | NEW |