2
2
3
3
#include " jni_utils.h"
4
4
#include " org_bblfsh_client_v2_Context.h"
5
+ #include " org_bblfsh_client_v2_ContextExt.h"
5
6
#include " org_bblfsh_client_v2_Context__.h"
6
7
#include " org_bblfsh_client_v2_Node.h"
7
8
#include " org_bblfsh_client_v2_libuast_Libuast.h"
@@ -72,9 +73,7 @@ class ContextExt {
72
73
73
74
JNIEnv *env = getJNIEnv ();
74
75
jclass cls = env->FindClass (CLS_NODE);
75
- if (env->ExceptionOccurred () || !cls) {
76
- return 0 ;
77
- }
76
+ checkJvmException (" failed to find class " + std::string (CLS_NODE));
78
77
79
78
if (!env->IsInstanceOf (obj, cls)) {
80
79
const char *err = " ContextExt.toHandle() called not on Node type" ;
@@ -84,9 +83,7 @@ class ContextExt {
84
83
85
84
auto handle =
86
85
(NodeHandle)env->GetLongField (obj, getField (env, obj, " handle" ));
87
- if (env->ExceptionOccurred () || !handle) {
88
- return 0 ;
89
- }
86
+ checkJvmException (" failed to get field Node.handle" );
90
87
91
88
return handle;
92
89
}
@@ -103,11 +100,12 @@ class ContextExt {
103
100
return toJ (root);
104
101
}
105
102
106
- // Encode serializes external UAST.
103
+ // Encode serializes the external UAST.
107
104
// Borrows the reference.
108
105
jobject Encode (jobject node, UastFormat format) {
109
- NodeHandle h = toHandle (node);
110
- uast::Buffer data = ctx->Encode (h, format);
106
+ // if (!assertNotContext(node)) return nullptr;
107
+
108
+ uast::Buffer data = ctx->Encode (toHandle (node), format);
111
109
return asJvmBuffer (data);
112
110
}
113
111
};
@@ -131,7 +129,7 @@ class Node : public uast::Node<Node *> {
131
129
static NodeKind kindOf (jobject obj) {
132
130
JNIEnv *env = getJNIEnv ();
133
131
// TODO(bzz): expose JNode.kind & replace type comparison \w a string test
134
- if (!obj) {
132
+ if (!obj || env-> IsInstanceOf (obj, env-> FindClass (CLS_JNULL)) ) {
135
133
return NODE_NULL;
136
134
} else if (env->IsInstanceOf (obj, env->FindClass (CLS_JSTR))) {
137
135
return NODE_STRING;
@@ -189,49 +187,71 @@ class Node : public uast::Node<Node *> {
189
187
190
188
NodeKind Kind () { return kind; }
191
189
192
- // TODO(#90): implement and test (all 'As*' are unused stubs for now)
193
- std::string *AsString () {
190
+ std::string *AsString () { // new ref
194
191
if (!str) {
192
+ const char methodName[] = " str" ;
195
193
JNIEnv *env = getJNIEnv ();
196
- const char *utf = env->GetStringUTFChars ((jstring)obj, 0 );
194
+ jstring jstr = (jstring)ObjectMethod (
195
+ env, methodName, " ()Ljava/lang/String;" , CLS_JSTR, &obj);
196
+
197
+ const char *utf = env->GetStringUTFChars (jstr, 0 );
197
198
str = new std::string (utf);
198
- env->ReleaseStringUTFChars ((jstring)obj , utf);
199
+ env->ReleaseStringUTFChars (jstr , utf);
199
200
}
200
201
201
202
std::string *s = new std::string (*str);
202
203
return s;
203
204
}
204
205
int64_t AsInt () {
206
+ const char methodName[] = " num" ;
205
207
JNIEnv *env = getJNIEnv ();
206
- jclass cls = env->FindClass (" java/lang/Integer" );
207
- jmethodID valueId = env->GetMethodID (cls, " longValue" , " ()J" );
208
- long long value = (long long )env->CallLongMethod (obj, valueId);
208
+ jmethodID mID = MethodID (env, methodName, " ()J" , CLS_JINT);
209
+
210
+ long long value = (long long )env->CallLongMethod (obj, mID );
211
+ checkJvmException (std::string (" failed to call " )
212
+ .append (CLS_JINT)
213
+ .append (" ." )
214
+ .append (methodName)
215
+ .append (" at Node::AsInt()" ));
209
216
return (int64_t )(value);
210
217
}
211
218
uint64_t AsUint () {
219
+ const char methodName[] = " get" ;
212
220
JNIEnv *env = getJNIEnv ();
213
- jclass cls = env->FindClass (" java/lang/Integer" );
214
- jmethodID valueId = env->GetMethodID (cls, " intValue" , " ()I" );
215
- jlong value = env->CallIntMethod (obj, valueId);
221
+ jmethodID mID = MethodID (env, methodName, " ()J" , CLS_JUINT);
216
222
217
- jmethodID mId = env->GetMethodID (cls, " toUnsignedLong" , " (I)J" );
218
- jlong v = env->CallLongMethod (obj, mId , value);
219
-
220
- return (uint64_t )(v);
223
+ jlong value = env->CallLongMethod (obj, mID );
224
+ checkJvmException (std::string (" failed to call " )
225
+ .append (CLS_JUINT)
226
+ .append (" ." )
227
+ .append (methodName)
228
+ .append (" at Node::AsUint()" ));
229
+ return (uint64_t )(value);
221
230
}
222
231
double AsFloat () {
232
+ const char methodName[] = " num" ;
223
233
JNIEnv *env = getJNIEnv ();
224
- jclass cls = env->FindClass (" java/lang/Double" );
225
- jmethodID valueId = env->GetMethodID (cls, " floatValue" , " ()F" );
226
- float value = (float )env->CallFloatMethod (obj, valueId);
234
+ jmethodID mID = MethodID (env, methodName, " ()D" , CLS_JFLT);
235
+
236
+ double value = (double )env->CallDoubleMethod (obj, mID );
237
+ checkJvmException (std::string (" failed to call " )
238
+ .append (CLS_JFLT)
239
+ .append (" ." )
240
+ .append (methodName)
241
+ .append (" at Node::AsFloat()" ));
227
242
return value;
228
243
}
229
244
bool AsBool () {
245
+ const char methodName[] = " value" ;
230
246
JNIEnv *env = getJNIEnv ();
231
- // TODO(bzz) check failures, cache classes, read 'value' filed
232
- jclass cls = env->FindClass (" java/lang/Boolean" );
233
- jmethodID valueId = env->GetMethodID (cls, " booleanValue" , " ()Z" );
234
- bool value = (bool )env->CallBooleanMethod (obj, valueId);
247
+ jmethodID mID = MethodID (env, methodName, " ()Z" , CLS_JBOOL);
248
+
249
+ bool value = (bool )env->CallBooleanMethod (obj, mID );
250
+ checkJvmException (std::string (" failed to call " )
251
+ .append (CLS_JBOOL)
252
+ .append (" ." )
253
+ .append (methodName)
254
+ .append (" at Node::AsBool()" ));
235
255
return value;
236
256
}
237
257
size_t Size () {
@@ -245,7 +265,7 @@ class Node : public uast::Node<Node *> {
245
265
246
266
JNIEnv *env = getJNIEnv ();
247
267
jstring key = (jstring)ObjectMethod (env, " keyAt" , METHOD_JNODE_KEY_AT,
248
- CLS_JNODE, &obj);
268
+ CLS_JNODE, &obj, i );
249
269
250
270
const char *k = env->GetStringUTFChars (key, 0 );
251
271
std::string *s = new std::string (k);
@@ -258,7 +278,7 @@ class Node : public uast::Node<Node *> {
258
278
259
279
JNIEnv *env = getJNIEnv ();
260
280
jobject val =
261
- ObjectMethod (env, " valueAt" , METHOD_JNODE_VALUE_AT, CLS_JNODE, &obj);
281
+ ObjectMethod (env, " valueAt" , METHOD_JNODE_VALUE_AT, CLS_JNODE, &obj, i );
262
282
return lookupOrCreate (env->NewGlobalRef (val)); // new ref
263
283
}
264
284
@@ -287,8 +307,7 @@ class Node : public uast::Node<Node *> {
287
307
288
308
jstring k = env->NewStringUTF (key.data ());
289
309
290
- jobject res =
291
- ObjectMethod (env, " add" , METHOD_JOBJ_ADD, CLS_JOBJ, &obj, k, v);
310
+ ObjectMethod (env, " add" , METHOD_JOBJ_ADD, CLS_JOBJ, &obj, k, v);
292
311
checkJvmException (
293
312
std::string (" failed to call JObject.add() from Node::SetKeyValue(" )
294
313
.append (key)
@@ -404,6 +423,9 @@ class Context {
404
423
if (node == nullptr ) return nullptr ;
405
424
return iface->toJ (node);
406
425
}
426
+ // toNode returns a node associated with a JVM object.
427
+ // Returns a new reference.
428
+ Node *toNode (jobject obj) { return iface->lookupOrCreate (obj); }
407
429
408
430
public:
409
431
Context () {
@@ -427,6 +449,16 @@ class Context {
427
449
return toJ (root); // new ref
428
450
}
429
451
452
+ // Encode serializes UAST.
453
+ // Creates a new reference.
454
+ jobject Encode (jobject node, UastFormat format) {
455
+ // if (!assertNotContext(node)) return nullptr;
456
+
457
+ Node *n = toNode (node);
458
+ uast::Buffer data = ctx->Encode (n, format);
459
+ return asJvmBuffer (data);
460
+ }
461
+
430
462
jobject LoadFrom (jobject src) { // JNode
431
463
JNIEnv *env = getJNIEnv ();
432
464
@@ -474,13 +506,13 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_decode(
474
506
jCtxExt = nullptr ;
475
507
delete (ctx);
476
508
delete (p);
477
- checkJvmException (" failed to instantiate Context class" );
509
+ checkJvmException (" failed to instantiate ContextExt class" );
478
510
}
479
511
480
512
return jCtxExt;
481
513
}
482
514
483
- // TODO(#86 ): implement
515
+ // TODO(#83 ): implement
484
516
JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_filter (
485
517
JNIEnv *, jobject, jobject, jstring) {
486
518
return nullptr ;
@@ -490,30 +522,40 @@ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_libuast_Libuast_filter(
490
522
// v2.Context()
491
523
// ==========================================
492
524
525
+ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_encode (
526
+ JNIEnv *env, jobject self, jobject node) {
527
+ UastFormat fmt = UAST_BINARY; // TODO(bzz): make it argument
528
+
529
+ Context *p = getHandle<Context>(env, self, nativeContext);
530
+ return p->Encode (node, fmt);
531
+ }
532
+
493
533
JNIEXPORT jlong JNICALL
494
534
Java_org_bblfsh_client_v2_Context_00024_create (JNIEnv *env, jobject self) {
495
535
auto c = new Context ();
496
- uast::Context<NodeHandle> *ctx; // TODO(#90): init from c on encode() impl
497
- auto p = new ContextExt (ctx);
498
- return (long )p;
536
+ return (long )c;
499
537
}
500
538
501
- JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_root (JNIEnv *env,
502
- jobject self) {
539
+ // ==========================================
540
+ // v2.ContextExt()
541
+ // ==========================================
542
+
543
+ JNIEXPORT jobject JNICALL
544
+ Java_org_bblfsh_client_v2_ContextExt_root (JNIEnv *env, jobject self) {
503
545
ContextExt *p = getHandle<ContextExt>(env, self, nativeContext);
504
546
return p->RootNode ();
505
547
}
506
548
507
- JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_Context_encode (
549
+ JNIEXPORT jobject JNICALL Java_org_bblfsh_client_v2_ContextExt_encode (
508
550
JNIEnv *env, jobject self, jobject node) {
509
551
UastFormat fmt = UAST_BINARY; // TODO(bzz): make it argument & enum
510
552
511
553
ContextExt *p = getHandle<ContextExt>(env, self, nativeContext);
512
554
return p->Encode (node, fmt);
513
555
}
514
556
515
- JNIEXPORT void JNICALL Java_org_bblfsh_client_v2_Context_dispose (JNIEnv *env,
516
- jobject self) {
557
+ JNIEXPORT void JNICALL
558
+ Java_org_bblfsh_client_v2_ContextExt_dispose (JNIEnv *env, jobject self) {
517
559
ContextExt *p = getHandle<ContextExt>(env, self, nativeContext);
518
560
setHandle<ContextExt>(env, self, 0 , nativeContext);
519
561
delete p;
0 commit comments