* Purpose and Justification: Represents a java class that can be used * for caching access to Java objects and classes. * */
| 42 | * |
| 43 | */ |
| 44 | class JavaClass { |
| 45 | |
| 46 | public: |
| 47 | |
| 48 | JavaClass() |
| 49 | : class_ref_(nullptr) { |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Initializes the java class with the name ( package + class name ) |
| 54 | * @param name class name |
| 55 | * @param classref class reference |
| 56 | * @param jenv Java environment -- should be thread associated |
| 57 | */ |
| 58 | explicit JavaClass(const std::string &name, jclass classref, JNIEnv* jenv) |
| 59 | : name_(name) { |
| 60 | class_ref_ = classref; |
| 61 | cnstrctr = jenv->GetMethodID(class_ref_, "<init>", "()V"); |
| 62 | } |
| 63 | |
| 64 | ~JavaClass() = default; |
| 65 | |
| 66 | std::string getName() const { |
| 67 | return name_; |
| 68 | } |
| 69 | |
| 70 | jclass getReference() const { |
| 71 | return class_ref_; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Though this is implicitly created, we'd like to make this aspect |
| 76 | * clear that we're likely to copy JavaClasses around. |
| 77 | */ |
| 78 | JavaClass &operator=(const JavaClass &o) = default; |
| 79 | |
| 80 | /** |
| 81 | * Call empty constructor |
| 82 | * @param env if supplied |
| 83 | * @return jobject that is a global reference. |
| 84 | */ |
| 85 | JNIEXPORT |
| 86 | jobject newInstance(JNIEnv *env) { |
| 87 | std::string instanceName = "(L" + name_ + ";)V"; |
| 88 | JNIEnv *lenv = env; |
| 89 | |
| 90 | ThrowIf(lenv); |
| 91 | |
| 92 | auto rezobj = lenv->NewObject(class_ref_, cnstrctr); |
| 93 | |
| 94 | ThrowIf(lenv); |
| 95 | |
| 96 | return lenv->NewGlobalRef(rezobj); |
| 97 | } |
| 98 | |
| 99 | jmethodID getClassMethod(JNIEnv *env, const std::string &methodName, const std::string &type) { |
| 100 | jmethodID mid = env->GetMethodID(class_ref_, methodName.c_str(), type.c_str()); |
| 101 | return mid; |