~ubuntu-branches/ubuntu/karmic/gears/karmic

« back to all changes in this revision

Viewing changes to gears/base/android/java_class.h

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Lesicnik
  • Date: 2009-04-30 19:15:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090430191525-0790sb5wzg8ou0xb
Tags: upstream-0.5.21.0~svn3334+dfsg
ImportĀ upstreamĀ versionĀ 0.5.21.0~svn3334+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2008, Google Inc.
 
2
//
 
3
// Redistribution and use in source and binary forms, with or without 
 
4
// modification, are permitted provided that the following conditions are met:
 
5
//
 
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.
 
14
//
 
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.
 
25
 
 
26
#ifndef GEARS_BASE_ANDROID_JAVA_CLASS_H__
 
27
#define GEARS_BASE_ANDROID_JAVA_CLASS_H__
 
28
 
 
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"
 
34
#else
 
35
#  define GEARS_JAVA_PACKAGE "android/webkit/gears"
 
36
#endif
 
37
 
 
38
#include "gears/base/android/java_global_ref.h"
 
39
#include "gears/base/android/java_jni.h"
 
40
#include <string>
 
41
 
 
42
// JNI "jclass" wrapper, with helper functions for finding classes and
 
43
// their methods. Copy constructor and assignment are safe.
 
44
// Sample usage:
 
45
//   JavaClass clazz;
 
46
//   if (clazz.FindClass("java/lang/String")) {
 
47
//     jmethodID id = clazz.GetMethodID(JavaClass::kNonStatic,
 
48
//                                      "hashCode",
 
49
//                                      "()I");
 
50
//     if (id)
 
51
//       ...
 
52
//   }
 
53
class JavaClass {
 
54
 public:
 
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;
 
61
 
 
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
 
65
  // callee.
 
66
  struct Method {
 
67
    MethodScope scope;
 
68
    const char *name;
 
69
    const char *signature;
 
70
    jmethodID id;
 
71
  };
 
72
 
 
73
  // Construct with name "uninitialized" and no JNI class.
 
74
  JavaClass();
 
75
 
 
76
  // Construct with given name and JNI class.
 
77
  JavaClass(const char *name, jclass class_ref);
 
78
 
 
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_) { }
 
83
 
 
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_;
 
88
    return *this;
 
89
  }
 
90
 
 
91
  // Return the contained jclass global reference if initialized, or
 
92
  // NULL otherwise.
 
93
  jclass Get() const { return class_ref_.Get(); }
 
94
 
 
95
  // Initializes name and JNI class by finding "name" in the global
 
96
  // namespace. Returns true on success.
 
97
  bool FindClass(const char *name);
 
98
 
 
99
  // Returns the method ID for a method in this class, or 0 if not found.
 
100
  jmethodID GetMethodID(MethodScope scope,
 
101
                        const char *name,
 
102
                        const char *signature) const;
 
103
 
 
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;
 
108
 
 
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,
 
112
                             int num_methods);
 
113
 
 
114
 private:
 
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_;
 
119
};
 
120
 
 
121
#endif // GEARS_BASE_ANDROID_JAVA_CLASS_H__