mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-03 09:49:19 +02:00
JavaClass class + first usage
This commit is contained in:
parent
86a07a0d1b
commit
ab83e1a79a
4 changed files with 58 additions and 9 deletions
|
@ -25,7 +25,7 @@
|
||||||
|
|
||||||
JavaVM *AndroidUtil::ourJavaVM = 0;
|
JavaVM *AndroidUtil::ourJavaVM = 0;
|
||||||
|
|
||||||
const char * const AndroidUtil::Class_java_lang_String = "java/lang/String";
|
shared_ptr<JavaClass> AndroidUtil::Class_java_lang_String;
|
||||||
const char * const AndroidUtil::Class_java_util_Collection = "java/util/Collection";
|
const char * const AndroidUtil::Class_java_util_Collection = "java/util/Collection";
|
||||||
const char * const AndroidUtil::Class_java_util_Locale = "java/util/Locale";
|
const char * const AndroidUtil::Class_java_util_Locale = "java/util/Locale";
|
||||||
const char * const AndroidUtil::Class_java_io_InputStream = "java/io/InputStream";
|
const char * const AndroidUtil::Class_java_io_InputStream = "java/io/InputStream";
|
||||||
|
@ -124,10 +124,9 @@ bool AndroidUtil::init(JavaVM* jvm) {
|
||||||
JNIEnv *env = getEnv();
|
JNIEnv *env = getEnv();
|
||||||
jclass cls;
|
jclass cls;
|
||||||
|
|
||||||
CHECK_NULL( cls = env->FindClass(Class_java_lang_String) );
|
Class_java_lang_String = new JavaClass(env, "java/lang/String");
|
||||||
Method_java_lang_String_toLowerCase = new StringMethod(env, cls, "toLowerCase", "()");
|
Method_java_lang_String_toLowerCase = new StringMethod(*Class_java_lang_String, "toLowerCase", "()");
|
||||||
Method_java_lang_String_toUpperCase = new StringMethod(env, cls, "toUpperCase", "()");
|
Method_java_lang_String_toUpperCase = new StringMethod(*Class_java_lang_String, "toUpperCase", "()");
|
||||||
env->DeleteLocalRef(cls);
|
|
||||||
|
|
||||||
CHECK_NULL( cls = env->FindClass(Class_java_util_Collection) );
|
CHECK_NULL( cls = env->FindClass(Class_java_util_Collection) );
|
||||||
CHECK_NULL( MID_java_util_Collection_toArray = env->GetMethodID(cls, "toArray", "()[Ljava/lang/Object;") );
|
CHECK_NULL( MID_java_util_Collection_toArray = env->GetMethodID(cls, "toArray", "()[Ljava/lang/Object;") );
|
||||||
|
@ -313,8 +312,7 @@ jbyteArray AndroidUtil::createJavaByteArray(JNIEnv *env, const std::vector<jbyte
|
||||||
|
|
||||||
jobjectArray AndroidUtil::createJavaStringArray(JNIEnv *env, const std::vector<std::string> &data) {
|
jobjectArray AndroidUtil::createJavaStringArray(JNIEnv *env, const std::vector<std::string> &data) {
|
||||||
size_t size = data.size();
|
size_t size = data.size();
|
||||||
jclass cls = env->FindClass("java/lang/String");
|
jobjectArray array = env->NewObjectArray(size, Class_java_lang_String->j(), 0);
|
||||||
jobjectArray array = env->NewObjectArray(size, cls, 0);
|
|
||||||
for (size_t i = 0; i < size; ++i) {
|
for (size_t i = 0; i < size; ++i) {
|
||||||
const std::string &str = data[i];
|
const std::string &str = data[i];
|
||||||
if (str.length() > 0) {
|
if (str.length() > 0) {
|
||||||
|
@ -323,7 +321,6 @@ jobjectArray AndroidUtil::createJavaStringArray(JNIEnv *env, const std::vector<s
|
||||||
env->DeleteLocalRef(javaStr);
|
env->DeleteLocalRef(javaStr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
env->DeleteLocalRef(cls);
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
|
|
||||||
#include <shared_ptr.h>
|
#include <shared_ptr.h>
|
||||||
|
|
||||||
|
class JavaClass;
|
||||||
class VoidMethod;
|
class VoidMethod;
|
||||||
class IntMethod;
|
class IntMethod;
|
||||||
class LongMethod;
|
class LongMethod;
|
||||||
|
@ -44,7 +45,7 @@ private:
|
||||||
static JavaVM *ourJavaVM;
|
static JavaVM *ourJavaVM;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static const char * const Class_java_lang_String;
|
static shared_ptr<JavaClass> Class_java_lang_String;
|
||||||
static const char * const Class_java_util_Collection;
|
static const char * const Class_java_util_Collection;
|
||||||
static const char * const Class_java_util_Locale;
|
static const char * const Class_java_util_Locale;
|
||||||
static const char * const Class_java_io_InputStream;
|
static const char * const Class_java_io_InputStream;
|
||||||
|
|
|
@ -23,12 +23,28 @@
|
||||||
|
|
||||||
static const std::string JNI_LOGGER_CLASS = "JniLog";
|
static const std::string JNI_LOGGER_CLASS = "JniLog";
|
||||||
|
|
||||||
|
JavaClass::JavaClass(JNIEnv *env, const std::string &name) : myName(name), myEnv(env) {
|
||||||
|
jclass ref = env->FindClass(name.c_str());
|
||||||
|
myClass = (jclass)env->NewGlobalRef(ref);
|
||||||
|
env->DeleteLocalRef(ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
JavaClass::~JavaClass() {
|
||||||
|
myEnv->DeleteGlobalRef(myClass);
|
||||||
|
}
|
||||||
|
|
||||||
Method::Method(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) : myName(name) {
|
Method::Method(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) : myName(name) {
|
||||||
//ZLLogger::Instance().registerClass(JNI_LOGGER_CLASS);
|
//ZLLogger::Instance().registerClass(JNI_LOGGER_CLASS);
|
||||||
myEnv = env;
|
myEnv = env;
|
||||||
myId = env->GetMethodID(cls, name.c_str(), signature.c_str());
|
myId = env->GetMethodID(cls, name.c_str(), signature.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Method::Method(const JavaClass &cls, const std::string &name, const std::string &signature) : myName(name) {
|
||||||
|
//ZLLogger::Instance().registerClass(JNI_LOGGER_CLASS);
|
||||||
|
myEnv = cls.myEnv;
|
||||||
|
myId = myEnv->GetMethodID(cls.myClass, name.c_str(), signature.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
Method::~Method() {
|
Method::~Method() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,6 +111,9 @@ jboolean BooleanMethod::call(jobject base, ...) {
|
||||||
StringMethod::StringMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) : Method(env, cls, name, signature + "Ljava/lang/String;") {
|
StringMethod::StringMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) : Method(env, cls, name, signature + "Ljava/lang/String;") {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StringMethod::StringMethod(const JavaClass &cls, const std::string &name, const std::string &signature) : Method(cls, name, signature + "Ljava/lang/String;") {
|
||||||
|
}
|
||||||
|
|
||||||
jstring StringMethod::call(jobject base, ...) {
|
jstring StringMethod::call(jobject base, ...) {
|
||||||
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "calling StringMethod " + myName);
|
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "calling StringMethod " + myName);
|
||||||
va_list lst;
|
va_list lst;
|
||||||
|
|
|
@ -24,12 +24,37 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
class JavaClass {
|
||||||
|
|
||||||
|
public:
|
||||||
|
JavaClass(JNIEnv *env, const std::string &name);
|
||||||
|
~JavaClass();
|
||||||
|
jclass j();
|
||||||
|
|
||||||
|
private:
|
||||||
|
JavaClass(const JavaClass&);
|
||||||
|
const JavaClass &operator = (const JavaClass&);
|
||||||
|
|
||||||
|
private:
|
||||||
|
const std::string myName;
|
||||||
|
JNIEnv *myEnv;
|
||||||
|
jclass myClass;
|
||||||
|
|
||||||
|
friend class Method;
|
||||||
|
friend class StaticMethod;
|
||||||
|
};
|
||||||
|
|
||||||
class Method {
|
class Method {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Method(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature);
|
Method(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature);
|
||||||
|
Method(const JavaClass &cls, const std::string &name, const std::string &signature);
|
||||||
virtual ~Method();
|
virtual ~Method();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Method(const Method&);
|
||||||
|
const Method &operator = (const Method&);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const std::string myName;
|
const std::string myName;
|
||||||
JNIEnv *myEnv;
|
JNIEnv *myEnv;
|
||||||
|
@ -42,6 +67,10 @@ public:
|
||||||
StaticMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature);
|
StaticMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature);
|
||||||
virtual ~StaticMethod();
|
virtual ~StaticMethod();
|
||||||
|
|
||||||
|
private:
|
||||||
|
StaticMethod(const StaticMethod&);
|
||||||
|
const StaticMethod &operator = (const StaticMethod&);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const std::string myName;
|
const std::string myName;
|
||||||
JNIEnv *myEnv;
|
JNIEnv *myEnv;
|
||||||
|
@ -79,6 +108,7 @@ public:
|
||||||
class StringMethod : public Method {
|
class StringMethod : public Method {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
StringMethod(const JavaClass &cls, const std::string &name, const std::string &signature);
|
||||||
StringMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature);
|
StringMethod(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature);
|
||||||
jstring call(jobject base, ...);
|
jstring call(jobject base, ...);
|
||||||
};
|
};
|
||||||
|
@ -97,4 +127,6 @@ public:
|
||||||
jobject call(jclass cls, ...);
|
jobject call(jclass cls, ...);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline jclass JavaClass::j() { return myClass; }
|
||||||
|
|
||||||
#endif /* __JNIENVELOPE_H__ */
|
#endif /* __JNIENVELOPE_H__ */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue