Changeset 1753 for trunk/lang/java


Ignore:
Timestamp:
Jun 9, 2010 11:05:15 AM (10 years ago)
Author:
braffert
Message:

Added java exceptions where applicable. Also added Library.getInt

Location:
trunk/lang/java
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/lang/java/Library.java

    r1749 r1753  
    2727  }
    2828
     29  public Library(){
     30    libPtr = jRpLibrary(null);
     31  }
     32
    2933  // Pseudo-Destructor.  Called when the object is garbage collected.----------
    3034  protected void finalize() throws Throwable {
     
    4852  public String getString(String path){
    4953    return jRpGetString(libPtr, path);
     54  }
     55
     56  public int getInt(String path){
     57    return jRpGetInt(libPtr, path);
    5058  }
    5159
     
    99107  private native byte[] jRpGetData(long libPtr, String path);
    100108  private native double jRpGetDouble(long libPtr, String path);
     109  private native int jRpGetInt(long libPtr, String path);
    101110  private native String jRpGetString(long libPtr, String path);
    102111
  • trunk/lang/java/Units.java

    r1732 r1753  
    2727  }
    2828
     29  public static String convert(String fromVal, String to){
     30    return jRpUnitsConvert(fromVal, to, true);
     31  }
     32
    2933  private static native String jRpUnitsConvert(String fromVal, String to, boolean units);
    3034}
  • trunk/lang/java/jRpLibrary.cc

    r1749 r1753  
    1 /*
    2  * ======================================================================
     1/* * ======================================================================
    32 *  AUTHOR:  Ben Rafferty, Purdue University
    43 *  Copyright (c) 2010  Purdue Research Foundation
     
    1817
    1918/*
    20  * Constructor.  Returns the address of the newly created RpLibrary object to
     19 * Constructor.  Creates a C++ RpLibrary object and returns its address to
    2120 * java as a long int.
    2221 */
    2322JNIEXPORT jlong JNICALL Java_rappture_Library_jRpLibrary
    2423  (JNIEnv *env, jobject obj, jstring javaPath){
    25   const char* nativePath = env->GetStringUTFChars(javaPath, 0);
    26   RpLibrary* lib = new RpLibrary(nativePath);
    27   env->ReleaseStringUTFChars(javaPath, nativePath);
     24  RpLibrary* lib = NULL;
     25  const char* nativePath;
     26  jclass ex;
     27 
     28  if (javaPath == NULL){
     29    lib = new RpLibrary();
     30  }
     31  else{
     32    nativePath = env->GetStringUTFChars(javaPath, 0);
     33    lib = new RpLibrary(nativePath);
     34    env->ReleaseStringUTFChars(javaPath, nativePath);
     35  }
     36
     37  if (lib == NULL){
     38    ex = env->FindClass("java/lang/NullPointerException");
     39    if (ex){
     40      env->ThrowNew(ex, "Could not create rappture library.");
     41    }
     42    env->DeleteLocalRef(ex);
     43  }
     44
    2845  return (jlong)lib;
    2946}
    3047
    3148/*
    32  * Pseudo-destructor.  This function is called by the java Library class's
    33  * finalizer.
     49 * Destructor.  This function is called by the java Library class's
     50 * finalizer.  Deletes the C++ RpLibrary object when the java Library
     51 * is garbage collected.
    3452 */
    3553JNIEXPORT void JNICALL Java_rappture_Library_jRpDeleteLibrary
     
    5169}
    5270
    53 
    5471// getDouble
    5572JNIEXPORT jdouble JNICALL Java_rappture_Library_jRpGetDouble
     
    5976  env->ReleaseStringUTFChars(javaPath, nativePath);
    6077  return (jdouble)retDVal;
     78}
     79
     80// getInt
     81JNIEXPORT jint JNICALL Java_rappture_Library_jRpGetInt
     82  (JNIEnv *env, jobject obj, jlong libPtr, jstring javaPath){
     83  const char* nativePath = env->GetStringUTFChars(javaPath, 0);
     84  int retIVal = ((RpLibrary*)libPtr)->getInt(nativePath);
     85  env->ReleaseStringUTFChars(javaPath, nativePath);
     86  return (jint)retIVal;
    6187}
    6288
     
    119145}
    120146
    121 
  • trunk/lang/java/jRpUnits.cc

    r1727 r1753  
    2222  const char* nativeFromVal = env->GetStringUTFChars(javaFromVal, 0);
    2323  const char* nativeTo = env->GetStringUTFChars(javaTo, 0);
    24   std::string retStr = RpUnits::convert(nativeFromVal, nativeTo, (int)units);
     24  int err;
     25  jclass ex;
     26  std::string errorMsg;
     27
     28  // perform conversion
     29  std::string retStr = RpUnits::convert(nativeFromVal, nativeTo,
     30                                       (int)units, &err);
     31
     32  // raise a runtime exception on error
     33  if (err){
     34    ex = env->FindClass("java/lang/RuntimeException");
     35    if (ex){
     36      errorMsg = "Connot convert ";
     37      errorMsg += nativeFromVal;
     38      errorMsg += " to ";
     39      errorMsg += nativeTo;
     40      env->ThrowNew(ex, errorMsg.c_str());
     41    }
     42    env->DeleteLocalRef(ex);
     43  }
     44
    2545  env->ReleaseStringUTFChars(javaFromVal, nativeFromVal);
    2646  env->ReleaseStringUTFChars(javaTo, nativeTo);
     47
     48  // create new java string and return
    2749  return (env->NewStringUTF(retStr.c_str()));
    2850}
  • trunk/lang/java/jRpUtils.cc

    r1727 r1753  
    2020JNIEXPORT void JNICALL Java_rappture_Utils_jRpUtilsProgress
    2121  (JNIEnv *env, jclass cls, jint percent, jstring javaText){
    22           const char* nativeText = env->GetStringUTFChars(javaText, 0);
    23   Rappture::Utils::progress(percent, nativeText);
     22  const char* nativeText = env->GetStringUTFChars(javaText, 0);
     23  int err = Rappture::Utils::progress(percent, nativeText);
     24  jclass ex;
     25  if (err){
     26    ex = env->FindClass("java/lang/RuntimeException");
     27    if (ex){
     28      env->ThrowNew(ex, "rappture.Utils.progress failed.");
     29    }
     30    env->DeleteLocalRef(ex);
     31  }
    2432  env->ReleaseStringUTFChars(javaText, nativeText);
    2533  return;
Note: See TracChangeset for help on using the changeset viewer.