mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-03 09:49:19 +02:00
Constructor class
This commit is contained in:
parent
3f2836844e
commit
4f6ef22b33
5 changed files with 48 additions and 26 deletions
|
@ -17,11 +17,10 @@
|
|||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <AndroidUtil.h>
|
||||
#include <JniEnvelope.h>
|
||||
|
||||
#include "fbreader/src/formats/FormatPlugin.h"
|
||||
|
||||
|
@ -29,18 +28,17 @@ extern "C"
|
|||
JNIEXPORT jobjectArray JNICALL Java_org_geometerplus_fbreader_formats_PluginCollection_nativePlugins(JNIEnv* env, jobject thiz) {
|
||||
const std::vector<shared_ptr<FormatPlugin> > plugins = PluginCollection::Instance().plugins();
|
||||
const size_t size = plugins.size();
|
||||
jclass cls = env->FindClass(AndroidUtil::Class_NativeFormatPlugin);
|
||||
jclass cls = AndroidUtil::Class_NativeFormatPlugin->j();
|
||||
// TODO: memory leak?
|
||||
jobjectArray javaPlugins = env->NewObjectArray(size, cls, 0);
|
||||
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
jstring fileType = AndroidUtil::createJavaString(env, plugins[i]->supportedFileType());
|
||||
jobject p = env->NewObject(cls, AndroidUtil::MID_NativeFormatPlugin_init, fileType);
|
||||
jobject p = AndroidUtil::Constructor_NativeFormatPlugin->call(fileType);
|
||||
env->SetObjectArrayElement(javaPlugins, i, p);
|
||||
env->DeleteLocalRef(p);
|
||||
env->DeleteLocalRef(fileType);
|
||||
}
|
||||
env->DeleteLocalRef(cls);
|
||||
return javaPlugins;
|
||||
}
|
||||
|
||||
|
|
|
@ -30,14 +30,14 @@ const char * const AndroidUtil::Class_java_util_Collection = "java/util/Collecti
|
|||
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_ZLibrary = "org/geometerplus/zlibrary/core/library/ZLibrary";
|
||||
const char * const AndroidUtil::Class_NativeFormatPlugin = "org/geometerplus/fbreader/formats/NativeFormatPlugin";
|
||||
shared_ptr<JavaClass> AndroidUtil::Class_NativeFormatPlugin;
|
||||
const char * const AndroidUtil::Class_PluginCollection = "org/geometerplus/fbreader/formats/PluginCollection";
|
||||
const char * const AndroidUtil::Class_Encoding = "org/geometerplus/zlibrary/core/encodings/Encoding";
|
||||
const char * const AndroidUtil::Class_EncodingConverter = "org/geometerplus/zlibrary/core/encodings/EncodingConverter";
|
||||
const char * const AndroidUtil::Class_JavaEncodingCollection = "org/geometerplus/zlibrary/core/encodings/JavaEncodingCollection";
|
||||
const char * const AndroidUtil::Class_Paths = "org/geometerplus/fbreader/Paths";
|
||||
const char * const AndroidUtil::Class_ZLFile = "org/geometerplus/zlibrary/core/filesystem/ZLFile";
|
||||
const char * const AndroidUtil::Class_ZLFileImage = "org/geometerplus/zlibrary/core/image/ZLFileImage";
|
||||
shared_ptr<JavaClass> AndroidUtil::Class_ZLFileImage;
|
||||
const char * const AndroidUtil::Class_Book = "org/geometerplus/fbreader/library/Book";
|
||||
const char * const AndroidUtil::Class_Tag = "org/geometerplus/fbreader/library/Tag";
|
||||
const char * const AndroidUtil::Class_NativeBookModel = "org/geometerplus/fbreader/bookmodel/NativeBookModel";
|
||||
|
@ -58,7 +58,7 @@ shared_ptr<LongMethod> AndroidUtil::Method_java_io_InputStream_skip;
|
|||
shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_ZLibrary_Instance;
|
||||
shared_ptr<StringMethod> AndroidUtil::Method_ZLibrary_getVersionName;
|
||||
|
||||
jmethodID AndroidUtil::MID_NativeFormatPlugin_init;
|
||||
shared_ptr<Constructor> AndroidUtil::Constructor_NativeFormatPlugin;
|
||||
shared_ptr<StringMethod> AndroidUtil::Method_NativeFormatPlugin_supportedFileType;
|
||||
|
||||
shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_PluginCollection_Instance;
|
||||
|
@ -82,7 +82,7 @@ shared_ptr<StringMethod> AndroidUtil::Method_ZLFile_getPath;
|
|||
shared_ptr<BooleanMethod> AndroidUtil::Method_ZLFile_isDirectory;
|
||||
shared_ptr<LongMethod> AndroidUtil::Method_ZLFile_size;
|
||||
|
||||
jmethodID AndroidUtil::MID_ZLFileImage_init;
|
||||
shared_ptr<Constructor> AndroidUtil::Constructor_ZLFileImage;
|
||||
|
||||
shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_Paths_cacheDirectory;
|
||||
|
||||
|
@ -149,10 +149,9 @@ bool AndroidUtil::init(JavaVM* jvm) {
|
|||
Method_ZLibrary_getVersionName = new StringMethod(env, cls, "getVersionName", "()");
|
||||
env->DeleteLocalRef(cls);
|
||||
|
||||
CHECK_NULL( cls = env->FindClass(Class_NativeFormatPlugin) );
|
||||
CHECK_NULL( MID_NativeFormatPlugin_init = env->GetMethodID(cls, "<init>", "(Ljava/lang/String;)V") );
|
||||
Method_NativeFormatPlugin_supportedFileType = new StringMethod(env, cls, "supportedFileType", "()");
|
||||
env->DeleteLocalRef(cls);
|
||||
Class_NativeFormatPlugin = new JavaClass(env, "org/geometerplus/fbreader/formats/NativeFormatPlugin");
|
||||
Constructor_NativeFormatPlugin = new Constructor(*Class_NativeFormatPlugin, "(Ljava/lang/String;)V");
|
||||
Method_NativeFormatPlugin_supportedFileType = new StringMethod(*Class_NativeFormatPlugin, "supportedFileType", "()");
|
||||
|
||||
CHECK_NULL( cls = env->FindClass(Class_PluginCollection) );
|
||||
StaticMethod_PluginCollection_Instance = new StaticObjectMethod(env, cls, "Instance", "org/geometerplus/fbreader/formats/PluginCollection", "()");
|
||||
|
@ -185,9 +184,8 @@ bool AndroidUtil::init(JavaVM* jvm) {
|
|||
Method_ZLFile_size = new LongMethod(env, cls, "size", "()");
|
||||
env->DeleteLocalRef(cls);
|
||||
|
||||
CHECK_NULL( cls = env->FindClass(Class_ZLFileImage) );
|
||||
CHECK_NULL( MID_ZLFileImage_init = env->GetMethodID(cls, "<init>", "(Ljava/lang/String;Lorg/geometerplus/zlibrary/core/filesystem/ZLFile;Ljava/lang/String;II)V") );
|
||||
env->DeleteLocalRef(cls);
|
||||
Class_ZLFileImage = new JavaClass(env, "org/geometerplus/zlibrary/core/image/ZLFileImage");
|
||||
Constructor_ZLFileImage = new Constructor(*Class_ZLFileImage, "(Ljava/lang/String;Lorg/geometerplus/zlibrary/core/filesystem/ZLFile;Ljava/lang/String;II)V");
|
||||
|
||||
CHECK_NULL( cls = env->FindClass(Class_Paths) );
|
||||
StaticMethod_Paths_cacheDirectory = new StaticObjectMethod(env, cls, "cacheDirectory", "java/lang/String", "()");
|
||||
|
@ -244,14 +242,11 @@ jobject AndroidUtil::createJavaImage(JNIEnv *env, const ZLFileImage &image) {
|
|||
jobject javaFile = createJavaFile(env, image.file().path());
|
||||
jstring javaEncoding = createJavaString(env, image.encoding());
|
||||
|
||||
jclass cls = env->FindClass(Class_ZLFileImage);
|
||||
jobject javaImage = env->NewObject(
|
||||
cls, MID_ZLFileImage_init,
|
||||
jobject javaImage = Constructor_ZLFileImage->call(
|
||||
javaMimeType, javaFile, javaEncoding,
|
||||
image.offset(), image.size()
|
||||
);
|
||||
|
||||
env->DeleteLocalRef(cls);
|
||||
env->DeleteLocalRef(javaEncoding);
|
||||
env->DeleteLocalRef(javaFile);
|
||||
env->DeleteLocalRef(javaMimeType);
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include <shared_ptr.h>
|
||||
|
||||
class JavaClass;
|
||||
class Constructor;
|
||||
class VoidMethod;
|
||||
class IntMethod;
|
||||
class LongMethod;
|
||||
|
@ -51,8 +52,8 @@ public:
|
|||
static const char * const Class_java_io_InputStream;
|
||||
static const char * const Class_ZLibrary;
|
||||
static const char * const Class_ZLFile;
|
||||
static const char * const Class_ZLFileImage;
|
||||
static const char * const Class_NativeFormatPlugin;
|
||||
static shared_ptr<JavaClass> Class_ZLFileImage;
|
||||
static shared_ptr<JavaClass> Class_NativeFormatPlugin;
|
||||
static const char * const Class_PluginCollection;
|
||||
static const char * const Class_Encoding;
|
||||
static const char * const Class_EncodingConverter;
|
||||
|
@ -86,9 +87,9 @@ public:
|
|||
static shared_ptr<BooleanMethod> Method_ZLFile_isDirectory;
|
||||
static shared_ptr<LongMethod> Method_ZLFile_size;
|
||||
|
||||
static jmethodID MID_ZLFileImage_init;
|
||||
static shared_ptr<Constructor> Constructor_ZLFileImage;
|
||||
|
||||
static jmethodID MID_NativeFormatPlugin_init;
|
||||
static shared_ptr<Constructor> Constructor_NativeFormatPlugin;
|
||||
static shared_ptr<StringMethod> Method_NativeFormatPlugin_supportedFileType;
|
||||
|
||||
static shared_ptr<StaticObjectMethod> StaticMethod_PluginCollection_Instance;
|
||||
|
|
|
@ -33,6 +33,18 @@ JavaClass::~JavaClass() {
|
|||
myEnv->DeleteGlobalRef(myClass);
|
||||
}
|
||||
|
||||
Constructor::Constructor(const JavaClass &cls, const std::string &signature) : myClass(cls) {
|
||||
myId = cls.myEnv->GetMethodID(cls.j(), "<init>", signature.c_str());
|
||||
}
|
||||
|
||||
jobject Constructor::call(...) {
|
||||
va_list lst;
|
||||
va_start(lst, this);
|
||||
jobject obj = myClass.myEnv->NewObjectV(myClass.j(), myId, lst);
|
||||
va_end(lst);
|
||||
return obj;
|
||||
}
|
||||
|
||||
Method::Method(JNIEnv *env, jclass cls, const std::string &name, const std::string &signature) : myName(name) {
|
||||
//ZLLogger::Instance().registerClass(JNI_LOGGER_CLASS);
|
||||
myEnv = env;
|
||||
|
|
|
@ -29,7 +29,7 @@ class JavaClass {
|
|||
public:
|
||||
JavaClass(JNIEnv *env, const std::string &name);
|
||||
~JavaClass();
|
||||
jclass j();
|
||||
jclass j() const;
|
||||
|
||||
private:
|
||||
JavaClass(const JavaClass&);
|
||||
|
@ -42,6 +42,22 @@ private:
|
|||
|
||||
friend class Method;
|
||||
friend class StaticMethod;
|
||||
friend class Constructor;
|
||||
};
|
||||
|
||||
class Constructor {
|
||||
|
||||
public:
|
||||
Constructor(const JavaClass &cls, const std::string &signature);
|
||||
jobject call(...);
|
||||
|
||||
private:
|
||||
Constructor(const Constructor&);
|
||||
const Constructor &operator = (const Constructor&);
|
||||
|
||||
protected:
|
||||
const JavaClass &myClass;
|
||||
jmethodID myId;
|
||||
};
|
||||
|
||||
class Method {
|
||||
|
@ -127,6 +143,6 @@ public:
|
|||
jobject call(jclass cls, ...);
|
||||
};
|
||||
|
||||
inline jclass JavaClass::j() { return myClass; }
|
||||
inline jclass JavaClass::j() const { return myClass; }
|
||||
|
||||
#endif /* __JNIENVELOPE_H__ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue