1
// Copyright 2008, Google Inc.
3
// Redistribution and use in source and binary forms, with or without
4
// modification, are permitted provided that the following conditions are met:
6
// 1. Redistributions of source code must retain the above copyright notice,
7
// this list of conditions and the following disclaimer.
8
// 2. Redistributions in binary form must reproduce the above copyright notice,
9
// this list of conditions and the following disclaimer in the documentation
10
// and/or other materials provided with the distribution.
11
// 3. Neither the name of Google Inc. nor the names of its contributors may be
12
// used to endorse or promote products derived from this software without
13
// specific prior written permission.
15
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
#ifndef GEARS_BASE_ANDROID_JAVA_CLASS_H__
27
#define GEARS_BASE_ANDROID_JAVA_CLASS_H__
29
// The package name for Gears classes depends on whether they're baked
30
// into the Android tree or not. If we don't have access to a class
31
// loader, we have to use the Android tree package.
32
#if USING_CLASS_LOADER
33
# define GEARS_JAVA_PACKAGE "com/google/android/gears"
35
# define GEARS_JAVA_PACKAGE "android/webkit/gears"
38
#include "gears/base/android/java_global_ref.h"
39
#include "gears/base/android/java_jni.h"
42
// JNI "jclass" wrapper, with helper functions for finding classes and
43
// their methods. Copy constructor and assignment are safe.
46
// if (clazz.FindClass("java/lang/String")) {
47
// jmethodID id = clazz.GetMethodID(JavaClass::kNonStatic,
55
// The scope of the instance used in the call to a method. Use
56
// kStatic for methods declared with the "static" keyword, and
57
// kNonStatic for others, including constructors.
58
typedef int MethodScope;
59
static const MethodScope kNonStatic = 0;
60
static const MethodScope kStatic = 1;
62
// Structure used as an in-out parameter to
63
// GetMultipleMethodIDs(). The scope, name and signature fields are
64
// initialized by the caller. The id field is initialized by the
69
const char *signature;
73
// Construct with name "uninitialized" and no JNI class.
76
// Construct with given name and JNI class.
77
JavaClass(const char *name, jclass class_ref);
79
// Copy constructor. The class members are copy-safe.
80
JavaClass(const JavaClass &other)
81
: class_name_(other.class_name_),
82
class_ref_(other.class_ref_) { }
84
// Assignment operator. The class members are assignment-safe.
85
JavaClass &operator=(const JavaClass &rhs) {
86
class_name_ = rhs.class_name_;
87
class_ref_ = rhs.class_ref_;
91
// Return the contained jclass global reference if initialized, or
93
jclass Get() const { return class_ref_.Get(); }
95
// Initializes name and JNI class by finding "name" in the global
96
// namespace. Returns true on success.
97
bool FindClass(const char *name);
99
// Returns the method ID for a method in this class, or 0 if not found.
100
jmethodID GetMethodID(MethodScope scope,
102
const char *signature) const;
104
// Initializes multiple method IDs. The ID fields of the array are
105
// assigned in-place. On failure, all IDs are guaranteed to be reset
106
// to 0. Returns true on success.
107
bool GetMultipleMethodIDs(Method *methods, int num_methods) const;
109
// Register native methods. The class must be initialized prior to
110
// this call. Returns true on success, false on failure.
111
bool RegisterNativeMethods(const JNINativeMethod *methods,
115
// The name of the class if initialized, or "uninitialized" if not.
116
std::string class_name_;
117
// Global reference to the class if initialized, or NULL if not.
118
JavaGlobalRef<jclass> class_ref_;
121
#endif // GEARS_BASE_ANDROID_JAVA_CLASS_H__