OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JavaClass.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2012-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include <android/log.h>
22 
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cmath>
26 
27 #include "JavaClass.h"
28 #include "AndroidPlatform.h"
29 
30 namespace MaliSDK
31 {
32  JavaClass::JavaClass(JNIEnv* env, const char* requiredClassPath):
33  classPath(AndroidPlatform::copyString(requiredClassPath)),
34  JNIEnvironment(env),
35  intialized(false)
36  {
37  if (JNIEnvironment == NULL)
38  {
39  LOGE("ERROR - JavaClass: Environment cannot be NULL.\n");
40  return;
41  }
42  jClass = JNIEnvironment->FindClass(classPath);
43  if (jClass == NULL)
44  {
45  LOGE("ERROR - JavaClass: Java class not found.");
46  return;
47  }
48  intialized = true;
49  }
50 
52  {
53  if (classPath != NULL)
54  {
55  free(classPath);
56  }
57  }
58 
59  bool JavaClass::staticField(const char* fieldName, char** result)
60  {
61  if (!intialized || fieldName == NULL)
62  {
63  return false;
64  }
65  jfieldID fieldID = JNIEnvironment->GetStaticFieldID(jClass, fieldName, TJString);
66  if (fieldID == NULL)
67  {
68  LOGE("ERROR - JavaClass: Field %s not found in %s\n", fieldName, classPath);
69  return false;
70  }
71  jstring jValue = (jstring)JNIEnvironment->GetStaticObjectField(jClass, fieldID);
72  const char *value = JNIEnvironment->GetStringUTFChars(jValue, 0);
73  char * returnValue = AndroidPlatform::copyString(value);
74  JNIEnvironment->ReleaseStringUTFChars(jValue, value);
75  *result = returnValue;
76  return true;
77  }
78 
79  bool JavaClass::staticField(const char* fieldName, int* result)
80  {
81  if (!intialized || fieldName == NULL) return false;
82  jfieldID fieldID = JNIEnvironment->GetStaticFieldID(jClass, fieldName, TJInt);
83  if (fieldID == NULL)
84  {
85  LOGE("ERROR - JavaClass: Field %s not found in %s\n", fieldName, classPath);
86  return false;
87  }
88  *result = JNIEnvironment->GetStaticIntField(jClass, fieldID);
89  return true;
90  }
91 
92  bool JavaClass::staticMethod(const char* methodName, int** returnValue, const char* param01)
93  {
94  if (!intialized || methodName == NULL || returnValue == NULL)
95  {
96  return false;
97  }
98  jmethodID methodID = JNIEnvironment->GetStaticMethodID(jClass, methodName, JM(TJIntArr, TJString));
99  if (methodID == NULL)
100  {
101  LOGE("ERROR - JavaClass: Method %s not found in %s.\n", methodName, classPath);
102  return false;
103  }
104  jintArray javaReturn = (jintArray)JNIEnvironment->CallStaticObjectMethod(jClass, methodID, JNIEnvironment->NewStringUTF(param01));
105  if (javaReturn == NULL)
106  {
107  LOGE("ERROR - JavaClass: A call to static method %s in %s failed.\n", methodName, classPath);
108  return false;
109  }
110  jboolean copy; /* copy flag */
111  *returnValue = (int*)JNIEnvironment->GetIntArrayElements(javaReturn, &copy);
112  if (*returnValue == NULL)
113  {
114  LOGE("ERROR - JavaClass: An attempt to retrieve array data in method %s in %s failed.\n", methodName, classPath);
115  return false;
116  }
117  return true;
118  }
119 
120  bool JavaClass::staticMethod(const char* methodName, const char* param01, const char* param02)
121  {
122  if (!intialized || methodName == NULL)
123  {
124  return false;
125  }
126  jmethodID methodID = JNIEnvironment->GetStaticMethodID(jClass, methodName, JM(TJVoid, TJString TJString));
127  if (methodID == NULL)
128  {
129  LOGE("ERROR - JavaClass: Method %s not found in %s.\n", methodName, classPath);
130  return false;
131  }
132  JNIEnvironment->CallStaticVoidMethod(jClass, methodID, JNIEnvironment->NewStringUTF(param01), JNIEnvironment->NewStringUTF(param02));
133  return true;
134  }
135 }
#define TJVoid
Definition: JavaClass.h:34
#define TJIntArr
Definition: JavaClass.h:35
JavaClass(JNIEnv *JNIEnvironment, const char *classPath)
Constructor taking the Java environment and the required class path.
Definition: JavaClass.cpp:32
GLint value
Definition: gl2ext.h:558
static char * copyString(const char *string)
Deep copy a string using memcopy().
Functions specific to the Android Platform.
bool staticField(const char *fieldName, char **result)
Access a static String field of the Java class.
Definition: JavaClass.cpp:59
#define JM(ret, params)
Definition: JavaClass.h:30
#define TJString
Definition: JavaClass.h:32
#define TJInt
Definition: JavaClass.h:33
#define LOGE(...)
Definition: AstcTextures.h:30
JNIEnv * JNIEnvironment
Definition: JavaClass.h:47
bool staticMethod(const char *methodName, int **returnValue, const char *param01)
Call a static method with one parameter which returns an integer array within the Java class...
Definition: JavaClass.cpp:92
virtual ~JavaClass()
Definition: JavaClass.cpp:51