@@ -50,6 +50,30 @@ jobject asJvmBuffer(uast::Buffer buf) {
50
50
return env->NewDirectByteBuffer (buf.ptr , buf.size );
51
51
}
52
52
53
+ bool assertNotContext (jobject obj) {
54
+ JNIEnv *env = getJNIEnv ();
55
+ if (isContext (obj, env)) {
56
+ auto reCls = env->FindClass (CLS_RE);
57
+ checkJvmException (" failed to find class " + std::string (CLS_RE));
58
+
59
+ env->ThrowNew (reCls, " cannot use UAST Context as a Node" );
60
+ return false ;
61
+ }
62
+ return true ;
63
+ }
64
+
65
+ bool isContext (jobject obj, JNIEnv *env) {
66
+ if (!obj) return false ;
67
+
68
+ jclass nodeCls = env->FindClass (CLS_NODE);
69
+ checkJvmException (" failed to find class " + std::string (CLS_NODE));
70
+
71
+ jclass jnodeCls = env->FindClass (CLS_JNODE);
72
+ checkJvmException (" failed to find class " + std::string (CLS_JNODE));
73
+
74
+ return env->IsInstanceOf (obj, nodeCls) || env->IsInstanceOf (obj, jnodeCls);
75
+ }
76
+
53
77
// ==========================================
54
78
// External UAST Context (managed by libuast)
55
79
// ==========================================
@@ -76,7 +100,9 @@ class ContextExt {
76
100
checkJvmException (" failed to find class " + std::string (CLS_NODE));
77
101
78
102
if (!env->IsInstanceOf (obj, cls)) {
79
- const char *err = " ContextExt.toHandle() called not on Node type" ;
103
+ auto err = std::string (" ContextExt.toHandle() called not on" )
104
+ .append (CLS_NODE)
105
+ .append (" type" );
80
106
ctx->SetError (err);
81
107
return 0 ;
82
108
}
@@ -103,7 +129,7 @@ class ContextExt {
103
129
// Encode serializes the external UAST.
104
130
// Borrows the reference.
105
131
jobject Encode (jobject node, UastFormat format) {
106
- // if (!assertNotContext(node)) return nullptr;
132
+ if (!assertNotContext (node)) return nullptr ;
107
133
108
134
uast::Buffer data = ctx->Encode (toHandle (node), format);
109
135
return asJvmBuffer (data);
@@ -358,7 +384,7 @@ class Interface : public uast::NodeCreator<Node *> {
358
384
// toJ returns a JVM object associated with a node.
359
385
// Returns a new reference.
360
386
jobject toJ (Node *node) {
361
- if (node == nullptr ) return nullptr ;
387
+ if (! node) return nullptr ;
362
388
jobject obj = getJNIEnv ()->NewGlobalRef (node->obj );
363
389
return obj;
364
390
}
@@ -420,7 +446,7 @@ class Context {
420
446
// toJ returns a JVM object associated with a node.
421
447
// Returns a new reference.
422
448
jobject toJ (Node *node) {
423
- if (node == nullptr ) return nullptr ;
449
+ if (! node) return nullptr ;
424
450
return iface->toJ (node);
425
451
}
426
452
// toNode returns a node associated with a JVM object.
@@ -452,7 +478,7 @@ class Context {
452
478
// Encode serializes UAST.
453
479
// Creates a new reference.
454
480
jobject Encode (jobject node, UastFormat format) {
455
- // if (!assertNotContext(node)) return nullptr;
481
+ if (!assertNotContext (node)) return nullptr ;
456
482
457
483
Node *n = toNode (node);
458
484
uast::Buffer data = ctx->Encode (n, format);
0 commit comments