1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 09:49:19 +02:00

Field class

This commit is contained in:
Nikolay Pultsin 2012-03-31 00:57:30 +01:00
parent 6b8470105c
commit a384a82a86
7 changed files with 48 additions and 13 deletions

View file

@ -243,7 +243,7 @@ JNIEXPORT jboolean JNICALL Java_org_geometerplus_fbreader_formats_NativeFormatPl
return JNI_FALSE;
}
jobject javaBook = env->GetObjectField(javaModel, AndroidUtil::FID_NativeBookModel_Book);
jobject javaBook = AndroidUtil::Field_NativeBookModel_Book->value(javaModel);
shared_ptr<Book> book = Book::loadFromJavaBook(env, javaBook);
shared_ptr<BookModel> model = new BookModel(book, javaModel);

View file

@ -87,7 +87,7 @@ shared_ptr<Book> Book::loadFromFile(const ZLFile &file) {
shared_ptr<Book> Book::loadFromJavaBook(JNIEnv *env, jobject javaBook) {
jstring javaString;
jobject javaFile = env->GetObjectField(javaBook, AndroidUtil::FID_Book_File);
jobject javaFile = AndroidUtil::Field_Book_File->value(javaBook);
javaString = AndroidUtil::Method_ZLFile_getPath->call(javaFile);
const std::string path = AndroidUtil::fromJavaString(env, javaString);
env->DeleteLocalRef(javaString);

View file

@ -66,7 +66,7 @@ shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_PluginCollection_Instan
shared_ptr<ObjectMethod> AndroidUtil::Method_Encoding_createConverter;
jfieldID AndroidUtil::FID_EncodingConverter_Name;
shared_ptr<ObjectField> AndroidUtil::Field_EncodingConverter_Name;
shared_ptr<IntMethod> AndroidUtil::Method_EncodingConverter_convert;
shared_ptr<VoidMethod> AndroidUtil::Method_EncodingConverter_reset;
@ -87,7 +87,7 @@ shared_ptr<Constructor> AndroidUtil::Constructor_ZLFileImage;
shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_Paths_cacheDirectory;
jfieldID AndroidUtil::FID_Book_File;
shared_ptr<ObjectField> AndroidUtil::Field_Book_File;
shared_ptr<StringMethod> AndroidUtil::Method_Book_getTitle;
shared_ptr<StringMethod> AndroidUtil::Method_Book_getLanguage;
shared_ptr<StringMethod> AndroidUtil::Method_Book_getEncodingNoDetection;
@ -101,7 +101,7 @@ shared_ptr<BooleanMethod> AndroidUtil::Method_Book_save;
shared_ptr<StaticObjectMethod> AndroidUtil::StaticMethod_Tag_getTag;
jfieldID AndroidUtil::FID_NativeBookModel_Book;
shared_ptr<ObjectField> AndroidUtil::Field_NativeBookModel_Book;
shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_initInternalHyperlinks;
shared_ptr<VoidMethod> AndroidUtil::Method_NativeBookModel_initTOC;
shared_ptr<ObjectMethod> AndroidUtil::Method_NativeBookModel_createTextModel;
@ -157,7 +157,7 @@ bool AndroidUtil::init(JavaVM* jvm) {
Method_Encoding_createConverter = new ObjectMethod(*Class_Encoding, "createConverter", "org/geometerplus/zlibrary/core/encodings/EncodingConverter", "()");
Class_EncodingConverter = new JavaClass(env, "org/geometerplus/zlibrary/core/encodings/EncodingConverter");
CHECK_NULL( FID_EncodingConverter_Name = env->GetFieldID(Class_EncodingConverter->j(), "Name", "Ljava/lang/String;") );
Field_EncodingConverter_Name = new ObjectField(*Class_EncodingConverter, "Name", "java/lang/String");
Method_EncodingConverter_convert = new IntMethod(*Class_EncodingConverter, "convert", "([BII[BI)");
Method_EncodingConverter_reset = new VoidMethod(*Class_EncodingConverter, "reset", "()");
@ -183,7 +183,7 @@ bool AndroidUtil::init(JavaVM* jvm) {
StaticMethod_Paths_cacheDirectory = new StaticObjectMethod(*Class_Paths, "cacheDirectory", "java/lang/String", "()");
Class_Book = new JavaClass(env, "org/geometerplus/fbreader/library/Book");
CHECK_NULL( FID_Book_File = env->GetFieldID(Class_Book->j(), "File", "Lorg/geometerplus/zlibrary/core/filesystem/ZLFile;") );
Field_Book_File = new ObjectField(*Class_Book, "File", "org/geometerplus/zlibrary/core/filesystem/ZLFile");
Method_Book_getTitle = new StringMethod(*Class_Book, "getTitle", "()");
Method_Book_getLanguage = new StringMethod(*Class_Book, "getLanguage", "()");
Method_Book_getEncodingNoDetection = new StringMethod(*Class_Book, "getEncodingNoDetection", "()");
@ -199,7 +199,7 @@ bool AndroidUtil::init(JavaVM* jvm) {
StaticMethod_Tag_getTag = new StaticObjectMethod(*Class_Tag, "getTag", "org/geometerplus/fbreader/library/Tag", "(Lorg/geometerplus/fbreader/library/Tag;Ljava/lang/String;)");
Class_NativeBookModel = new JavaClass(env, "org/geometerplus/fbreader/bookmodel/NativeBookModel");
CHECK_NULL( FID_NativeBookModel_Book = env->GetFieldID(Class_NativeBookModel->j(), "Book", "Lorg/geometerplus/fbreader/library/Book;") );
Field_NativeBookModel_Book = new ObjectField(*Class_NativeBookModel, "Book", "org/geometerplus/fbreader/library/Book");
Method_NativeBookModel_initInternalHyperlinks = new VoidMethod(*Class_NativeBookModel, "initInternalHyperlinks", "(Ljava/lang/String;Ljava/lang/String;I)");
Method_NativeBookModel_initTOC = new VoidMethod(*Class_NativeBookModel, "initTOC", "(Lorg/geometerplus/zlibrary/text/model/ZLTextModel;[I[I)");
Method_NativeBookModel_createTextModel = new ObjectMethod(*Class_NativeBookModel, "createTextModel", "org/geometerplus/zlibrary/text/model/ZLTextModel", "(Ljava/lang/String;Ljava/lang/String;I[I[I[I[I[BLjava/lang/String;Ljava/lang/String;I)");

View file

@ -29,6 +29,7 @@
class JavaClass;
class Constructor;
class ObjectField;
class VoidMethod;
class IntMethod;
class LongMethod;
@ -97,7 +98,7 @@ public:
static shared_ptr<ObjectMethod> Method_Encoding_createConverter;
static jfieldID FID_EncodingConverter_Name;
static shared_ptr<ObjectField> Field_EncodingConverter_Name;
static shared_ptr<IntMethod> Method_EncodingConverter_convert;
static shared_ptr<VoidMethod> Method_EncodingConverter_reset;
@ -108,7 +109,7 @@ public:
static shared_ptr<StaticObjectMethod> StaticMethod_Paths_cacheDirectory;
static jfieldID FID_Book_File;
static shared_ptr<ObjectField> Field_Book_File;
static shared_ptr<StringMethod> Method_Book_getTitle;
static shared_ptr<StringMethod> Method_Book_getLanguage;
static shared_ptr<StringMethod> Method_Book_getEncodingNoDetection;
@ -122,7 +123,7 @@ public:
static shared_ptr<StaticObjectMethod> StaticMethod_Tag_getTag;
static jfieldID FID_NativeBookModel_Book;
static shared_ptr<ObjectField> Field_NativeBookModel_Book;
static shared_ptr<VoidMethod> Method_NativeBookModel_initInternalHyperlinks;
static shared_ptr<VoidMethod> Method_NativeBookModel_initTOC;
static shared_ptr<ObjectMethod> Method_NativeBookModel_createTextModel;

View file

@ -52,6 +52,13 @@ jobject Constructor::call(...) {
return obj;
}
Field::Field(const JavaClass &cls, const std::string &name, const std::string &type) : Member(cls), myName(name) {
myId = env().GetFieldID(jClass(), name.c_str(), type.c_str());
}
Field::~Field() {
}
Method::Method(const JavaClass &cls, const std::string &name, const std::string &signature) : Member(cls), myName(name) {
myId = env().GetMethodID(jClass(), name.c_str(), signature.c_str());
}
@ -66,6 +73,16 @@ StaticMethod::StaticMethod(const JavaClass &cls, const std::string &name, const
StaticMethod::~StaticMethod() {
}
ObjectField::ObjectField(const JavaClass &cls, const std::string &name, const std::string &type) : Field(cls, name, "L" + type + ";") {
}
jobject ObjectField::value(jobject obj) const {
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "getting value of ObjectField " + myName);
jobject val = env().GetObjectField(obj, myId);
ZLLogger::Instance().println(JNI_LOGGER_CLASS, "got value of ObjectField " + myName);
return val;
}
VoidMethod::VoidMethod(const JavaClass &cls, const std::string &name, const std::string &signature) : Method(cls, name, signature + "V") {
}

View file

@ -41,7 +41,6 @@ private:
jclass myClass;
friend class Member;
friend class Method;
};
class Member {
@ -72,6 +71,17 @@ private:
jmethodID myId;
};
class Field : public Member {
public:
Field(const JavaClass &cls, const std::string &name, const std::string &type);
virtual ~Field();
protected:
const std::string myName;
jfieldID myId;
};
class Method : public Member {
public:
@ -94,6 +104,13 @@ protected:
jmethodID myId;
};
class ObjectField : public Field {
public:
ObjectField(const JavaClass &cls, const std::string &name, const std::string &type);
jobject value(jobject obj) const;
};
class VoidMethod : public Method {
public:

View file

@ -85,7 +85,7 @@ JavaEncodingConverter::~JavaEncodingConverter() {
std::string JavaEncodingConverter::name() const {
JNIEnv *env = AndroidUtil::getEnv();
jstring javaName = (jstring)env->GetObjectField(myJavaConverter, AndroidUtil::FID_EncodingConverter_Name);
jstring javaName = (jstring)AndroidUtil::Field_EncodingConverter_Name->value(myJavaConverter);
const std::string result = AndroidUtil::fromJavaString(env, javaName);
env->DeleteLocalRef(javaName);
return result;