jni_helper.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIBHDFS_JNI_HELPER_H
  19. #define LIBHDFS_JNI_HELPER_H
  20. #include <jni.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <stdarg.h>
  24. #include <search.h>
  25. #include <pthread.h>
  26. #include <errno.h>
  27. #define PATH_SEPARATOR ':'
  28. /** Denote the method we want to invoke as STATIC or INSTANCE */
  29. typedef enum {
  30. STATIC,
  31. INSTANCE
  32. } MethType;
  33. /** Used for returning an appropriate return value after invoking
  34. * a method
  35. */
  36. typedef jvalue RetVal;
  37. /** Used for returning the exception after invoking a method */
  38. typedef jthrowable Exc;
  39. /** invokeMethod: Invoke a Static or Instance method.
  40. * className: Name of the class where the method can be found
  41. * methName: Name of the method
  42. * methSignature: the signature of the method "(arg-types)ret-type"
  43. * methType: The type of the method (STATIC or INSTANCE)
  44. * instObj: Required if the methType is INSTANCE. The object to invoke
  45. the method on.
  46. * env: The JNIEnv pointer
  47. * retval: The pointer to a union type which will contain the result of the
  48. method invocation, e.g. if the method returns an Object, retval will be
  49. set to that, if the method returns boolean, retval will be set to the
  50. value (JNI_TRUE or JNI_FALSE), etc.
  51. * exc: If the methods throws any exception, this will contain the reference
  52. * Arguments (the method arguments) must be passed after methSignature
  53. * RETURNS: -1 on error and 0 on success. If -1 is returned, exc will have
  54. a valid exception reference, and the result stored at retval is undefined.
  55. */
  56. int invokeMethod(JNIEnv *env, RetVal *retval, Exc *exc, MethType methType,
  57. jobject instObj, const char *className, const char *methName,
  58. const char *methSignature, ...);
  59. /** constructNewObjectOfClass: Invoke a constructor.
  60. * className: Name of the class
  61. * ctorSignature: the signature of the constructor "(arg-types)V"
  62. * env: The JNIEnv pointer
  63. * exc: If the ctor throws any exception, this will contain the reference
  64. * Arguments to the ctor must be passed after ctorSignature
  65. */
  66. jobject constructNewObjectOfClass(JNIEnv *env, Exc *exc, const char *className,
  67. const char *ctorSignature, ...);
  68. jmethodID methodIdFromClass(const char *className, const char *methName,
  69. const char *methSignature, MethType methType,
  70. JNIEnv *env);
  71. jclass globalClassReference(const char *className, JNIEnv *env);
  72. /** classNameOfObject: Get an object's class name.
  73. * @param jobj: The object.
  74. * @param env: The JNIEnv pointer.
  75. * @return Returns a pointer to a string containing the class name. This string
  76. * must be freed by the caller.
  77. */
  78. char *classNameOfObject(jobject jobj, JNIEnv *env);
  79. /** getJNIEnv: A helper function to get the JNIEnv* for the given thread.
  80. * If no JVM exists, then one will be created. JVM command line arguments
  81. * are obtained from the LIBHDFS_OPTS environment variable.
  82. * @param: None.
  83. * @return The JNIEnv* corresponding to the thread.
  84. * */
  85. JNIEnv* getJNIEnv(void);
  86. jarray constructNewArrayString(JNIEnv *env, Exc *exc, const char **elements, int size) ;
  87. #endif /*LIBHDFS_JNI_HELPER_H*/
  88. /**
  89. * vim: ts=4: sw=4: et:
  90. */