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 std::exception {
  50 // Prohibit Heap-Based Classes.
  51 private:
  52     static void *operator new(size_t size);
  53 
  54 private:
  55 #ifdef DEBUG
  56     static std::string CreateExceptionMessage(JNIEnv* Env, jthrowable Exception,
  57         jmethodID GetCauseMethod, jmethodID GetStackTraceMethod, jmethodID ThrowableToStringMethod,
  58         jmethodID FrameToStringMethod);
  59 #endif //DEBUG
  60 
  61     std::string FMessage;
  62     jthrowable FException;
  63     JNIEnv *FEnv;
  64 
  65 public:
  66     explicit JavaException();
  67     explicit JavaException(const char* const message);
  68     explicit JavaException(JNIEnv *Env, const char* const message);
  69     virtual ~JavaException() throw() {}
  70 
  71     virtual const char* what();
  72     void Rethrow();
  73 };
  74 
  75 
  76 class JavaStaticMethod {
  77 // Prohibit Heap-Based Classes.
  78 private:
  79     static void *operator new(size_t size);
  80     static void operator delete(void *ptr);
  81 
  82 private:
  83     JNIEnv *FEnv;
  84     jmethodID FMethod;
  85     jclass FClass;
  86 public:
  87     JavaStaticMethod(JNIEnv *Env, jclass Class, jmethodID Method);
  88 
  89     void CallVoidMethod(int Count, ...);
  90     operator jmethodID ();
  91 };
  92 
  93 
  94 class JavaMethod {
  95 // Prohibit Heap-Based Classes.
  96 private:
  97     static void *operator new(size_t size);
  98     static void operator delete(void *ptr);
  99 
 100     JavaMethod(JavaMethod const&); // Don't Implement.
 101     void operator=(JavaMethod const&); // Don't implement
 102 
 103 private:
 104     JNIEnv *FEnv;
 105     jmethodID FMethod;
 106     jobject FObj;
 107 public:
 108     JavaMethod(JNIEnv *Env, jobject Obj, jmethodID Method);
 109 
 110     void CallVoidMethod(int Count, ...);
 111     operator jmethodID ();
 112 };
 113 
 114 
 115 class JavaClass {
 116 // Prohibit Heap-Based Classes.
 117 private:
 118     static void *operator new(size_t size);
 119     static void operator delete(void *ptr);
 120 
 121     JavaClass(JavaClass const&); // Don't Implement.
 122     void operator=(JavaClass const&); // Don't implement
 123 
 124 private:
 125     JNIEnv *FEnv;
 126     jclass FClass;
 127     TString FClassName;
 128 
 129 public:
 130     JavaClass(JNIEnv *Env, TString Name);
 131     ~JavaClass();
 132 
 133     JavaStaticMethod GetStaticMethod(TString Name, TString Signature);
 134     operator jclass ();
 135 };
 136 
 137 
 138 class JavaStringArray {
 139 // Prohibit Heap-Based Classes.
 140 private:
 141     static void *operator new(size_t size);
 142     static void operator delete(void *ptr);
 143 
 144     JavaStringArray(JavaStringArray const&); // Don't Implement.
 145     void operator=(JavaStringArray const&); // Don't implement
 146 
 147 private:
 148     JNIEnv *FEnv;
 149     jobjectArray FData;
 150     
 151     void Initialize(size_t Size);
 152 
 153 public:
 154     JavaStringArray(JNIEnv *Env, size_t Size);
 155     JavaStringArray(JNIEnv *Env, jobjectArray Data);
 156     JavaStringArray(JNIEnv *Env, std::list<TString> Array);
 157 
 158     jobjectArray GetData();
 159     void SetValue(jsize Index, jstring Item);
 160     jstring GetValue(jsize Index);
 161     unsigned int Count();
 162 };
 163 
 164 #endif //JAVA_H