1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 
  34 #ifndef JAVA_H
  35 #define JAVA_H
  36 
  37 #include "Platform.h"
  38 #include "Messages.h"
  39 
  40 #include "jni.h"
  41 
  42 
  43 class JavaClass;
  44 class JavaStaticMethod;
  45 class JavaMethod;
  46 class JavaStringArray;
  47 
  48 
  49 class JavaException : public Exception {
  50 // Prohibit Heap-Based Classes.
  51 private:
  52     static void *operator new(size_t size);
  53 
  54 private:
  55 #ifdef DEBUG
  56     static TString CreateExceptionMessage(JNIEnv* Env, jthrowable Exception,
  57         jmethodID GetCauseMethod, jmethodID GetStackTraceMethod, jmethodID ThrowableToStringMethod,
  58         jmethodID FrameToStringMethod);
  59 #endif //DEBUG
  60 
  61     jthrowable FException;
  62     JNIEnv *FEnv;
  63 
  64 public:
  65     explicit JavaException();
  66     explicit JavaException(JNIEnv *Env, const TString message);
  67     virtual ~JavaException() throw() {}
  68 
  69     void Rethrow();
  70 };
  71 
  72 
  73 class JavaStaticMethod {
  74 // Prohibit Heap-Based Classes.
  75 private:
  76     static void *operator new(size_t size);
  77     static void operator delete(void *ptr);
  78 
  79 private:
  80     JNIEnv *FEnv;
  81     jmethodID FMethod;
  82     jclass FClass;
  83 public:
  84     JavaStaticMethod(JNIEnv *Env, jclass Class, jmethodID Method);
  85 
  86     void CallVoidMethod(int Count, ...);
  87     operator jmethodID ();
  88 };
  89 
  90 
  91 class JavaMethod {
  92 // Prohibit Heap-Based Classes.
  93 private:
  94     static void *operator new(size_t size);
  95     static void operator delete(void *ptr);
  96 
  97     JavaMethod(JavaMethod const&); // Don't Implement.
  98     void operator=(JavaMethod const&); // Don't implement
  99 
 100 private:
 101     JNIEnv *FEnv;
 102     jmethodID FMethod;
 103     jobject FObj;
 104 public:
 105     JavaMethod(JNIEnv *Env, jobject Obj, jmethodID Method);
 106 
 107     void CallVoidMethod(int Count, ...);
 108     operator jmethodID ();
 109 };
 110 
 111 
 112 class JavaClass {
 113 // Prohibit Heap-Based Classes.
 114 private:
 115     static void *operator new(size_t size);
 116     static void operator delete(void *ptr);
 117 
 118     JavaClass(JavaClass const&); // Don't Implement.
 119     void operator=(JavaClass const&); // Don't implement
 120 
 121 private:
 122     JNIEnv *FEnv;
 123     jclass FClass;
 124     TString FClassName;
 125 
 126 public:
 127     JavaClass(JNIEnv *Env, TString Name);
 128     ~JavaClass();
 129 
 130     JavaStaticMethod GetStaticMethod(TString Name, TString Signature);
 131     operator jclass ();
 132 };
 133 
 134 
 135 class JavaStringArray {
 136 // Prohibit Heap-Based Classes.
 137 private:
 138     static void *operator new(size_t size);
 139     static void operator delete(void *ptr);
 140 
 141     JavaStringArray(JavaStringArray const&); // Don't Implement.
 142     void operator=(JavaStringArray const&); // Don't implement
 143 
 144 private:
 145     JNIEnv *FEnv;
 146     jobjectArray FData;
 147 
 148     void Initialize(size_t Size);
 149 
 150 public:
 151     JavaStringArray(JNIEnv *Env, size_t Size);
 152     JavaStringArray(JNIEnv *Env, jobjectArray Data);
 153     JavaStringArray(JNIEnv *Env, std::list<TString> Array);
 154 
 155     jobjectArray GetData();
 156     void SetValue(jsize Index, jstring Item);
 157     jstring GetValue(jsize Index);
 158     unsigned int Count();
 159 };
 160 
 161 #endif //JAVA_H