Line data Source code
1 : /****************************************************************************** 2 : * Copyright (c) 2005, 2006 Los Alamos National Security, LLC. This 3 : * material was produced under U.S. Government contract 4 : * DE-AC52-06NA25396 for Los Alamos National Laboratory (LANL), which 5 : * is operated by the Los Alamos National Security, LLC (LANS) for the 6 : * U.S. Department of Energy. The U.S. Government has rights to use, 7 : * reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR 8 : * LANS MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY 9 : * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to 10 : * produce derivative works, such modified software should be clearly 11 : * marked, so as not to confuse it with the version available from 12 : * LANL. 13 : * 14 : * Additionally, this program and the accompanying materials 15 : * are made available under the terms of the Eclipse Public License v1.0 16 : * which accompanies this distribution, and is available at 17 : * http://www.eclipse.org/legal/epl-v10.html 18 : *****************************************************************************/ 19 : 20 : #ifdef __cplusplus 21 : extern "C" { 22 : #endif 23 : 24 : #include <stdlib.h> 25 : #include "jni.h" 26 : #include "token.h" 27 : 28 : 29 5234 : Token_t *convert_Java_token(JNIEnv *env, jobject token) 30 : { 31 5234 : jclass cls; 32 5234 : jint type; 33 5234 : jstring java_string; 34 5234 : const char *text = NULL; 35 5234 : jmethodID method_id; 36 5234 : Token_t *our_token = NULL; 37 5234 : int line = -1; 38 5234 : int col = -1; 39 : 40 : /* Need to get the object class so we can use it to retrieve the methods. */ 41 5234 : cls = (*env)->GetObjectClass(env, token); 42 : /* Get the method ID for the getType() method. */ 43 5234 : method_id = (*env)->GetMethodID(env, cls, "getType", "()I"); 44 : /* Call the getType method to get the token type. */ 45 5234 : type = (*env)->CallIntMethod(env, token, method_id); 46 : 47 : /* Get the method ID for the getText() method. */ 48 5234 : method_id = (*env)->GetMethodID(env, cls, "getText", "()Ljava/lang/String;"); 49 : /* Call getText() to get the Java String object. */ 50 5234 : java_string = (*env)->CallObjectMethod(env, token, method_id); 51 : /* Get a C char string for the Java String if the Java String exists. */ 52 5234 : if(java_string != NULL) 53 : { 54 5234 : text = (*env)->GetStringUTFChars(env, java_string, NULL); 55 5234 : if(text == NULL) 56 : { 57 0 : fprintf(stderr, "Unable to retrieve text string from String object\n"); 58 0 : exit(1); 59 : } 60 : } 61 : else 62 : { 63 : text = NULL; 64 : } 65 : 66 : /* Get the method ID for the getLine() method. */ 67 5234 : method_id = (*env)->GetMethodID(env, cls, "getLine", "()I"); 68 : /* Call the getLine() method to get the line number for this token. */ 69 5234 : line = (*env)->CallIntMethod(env, token, method_id); 70 : 71 : /* Get the method ID for the getCharPositionInLine() method. */ 72 5234 : method_id = (*env)->GetMethodID(env, cls, "getCharPositionInLine", "()I"); 73 : /* Call getCharPositionInLine() to get the column position for the token. */ 74 5234 : col = (*env)->CallIntMethod(env, token, method_id); 75 : 76 : /* build a Token of our own. */ 77 5234 : our_token = create_token(line, col, type, text); 78 : 79 : /* Release the string now that we've made a new copy for ourselves. */ 80 5234 : if(java_string != NULL) 81 5234 : (*env)->ReleaseStringUTFChars(env, java_string, text); 82 : 83 5234 : return our_token; 84 : } 85 : 86 : #ifdef __cplusplus 87 : } /* End extern C. */ 88 : #endif 89 :