Skip to content

Commit dde11b7

Browse files
Report more info for errors from jdi
1 parent 6b4ea1f commit dde11b7

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

eval4j/src/org/jetbrains/eval4j/jdi/jdiEval.kt

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class JDIEval(
183183

184184
override fun getStaticField(fieldDesc: FieldDescription): Value {
185185
val field = findStaticField(fieldDesc)
186-
return mayThrow { field.declaringType().getValue(field) }.asValue()
186+
return mayThrow { field.declaringType().getValue(field) }.ifFail(field).asValue()
187187
}
188188

189189
override fun setStaticField(fieldDesc: FieldDescription, newValue: Value) {
@@ -199,7 +199,7 @@ class JDIEval(
199199
}
200200

201201
val jdiValue = newValue.asJdiValue(vm, field.type().asType())
202-
mayThrow { _class.setValue(field, jdiValue) }
202+
mayThrow { _class.setValue(field, jdiValue) }.ifFail(field)
203203
}
204204

205205
private fun findMethod(methodDesc: MethodDescription, _class: ReferenceType = methodDesc.ownerType.asReferenceType()): Method {
@@ -231,7 +231,7 @@ class JDIEval(
231231
}
232232

233233
args.disableCollection()
234-
val result = mayThrow { _class.invokeMethod(thread, method, args, invokePolicy) }
234+
val result = mayThrow { _class.invokeMethod(thread, method, args, invokePolicy) }.ifFail(method)
235235
args.enableCollection()
236236
return result.asValue()
237237
}
@@ -240,7 +240,7 @@ class JDIEval(
240240
val field = findField(fieldDesc)
241241
val obj = instance.jdiObj.checkNull()
242242

243-
return mayThrow { obj.getValue(field) }.asValue()
243+
return mayThrow { obj.getValue(field) }.ifFail(field, obj).asValue()
244244
}
245245

246246
override fun setField(instance: Value, fieldDesc: FieldDescription, newValue: Value) {
@@ -288,7 +288,7 @@ class JDIEval(
288288
val _class = (instance as NewObjectValue).asmType.asReferenceType() as ClassType
289289
val args = mapArguments(arguments, ctor.safeArgumentTypes())
290290
args.disableCollection()
291-
val result = mayThrow { _class.newInstance(thread, ctor, args, invokePolicy) }
291+
val result = mayThrow { _class.newInstance(thread, ctor, args, invokePolicy) }.ifFail(ctor)
292292
args.enableCollection()
293293
instance.value = result
294294
return result.asValue()
@@ -302,7 +302,7 @@ class JDIEval(
302302
}
303303

304304
args.disableCollection()
305-
val result = mayThrow { obj.invokeMethod(thread, method, args, policy) }
305+
val result = mayThrow { obj.invokeMethod(thread, method, args, policy) }.ifFail(method, obj)
306306
args.enableCollection()
307307
return result.asValue()
308308
}
@@ -403,11 +403,41 @@ class JDIEval(
403403
}
404404
}
405405

406-
fun <T> mayThrow(f: () -> T): T {
406+
private sealed class JdiOperationResult<T> {
407+
class Fail<T>(val cause: Exception): JdiOperationResult<T>()
408+
class OK<T>(val value: T): JdiOperationResult<T>()
409+
}
410+
411+
private fun <T> mayThrow(f: () -> T): JdiOperationResult<T> {
407412
try {
408-
return f()
413+
return JdiOperationResult.OK(f())
414+
}
415+
catch (e: IllegalArgumentException) {
416+
return JdiOperationResult.Fail<T>(e)
409417
}
410418
catch (e: InvocationException) {
411419
throw ThrownFromEvaluatedCodeException(e.exception().asValue())
412420
}
421+
}
422+
423+
private fun memberInfo(member: TypeComponent, thisObj: ObjectReference?): String {
424+
return "\nmember = $member\nobjectRef = $thisObj"
425+
}
426+
427+
private fun <T> JdiOperationResult<T>.ifFail(member: TypeComponent, thisObj: ObjectReference? = null): T {
428+
return ifFail { memberInfo(member, thisObj) }
429+
}
430+
431+
private fun <T> JdiOperationResult<T>.ifFail(lazyMessage: () -> String): T {
432+
return when(this) {
433+
is JdiOperationResult.OK -> this.value
434+
is JdiOperationResult.Fail -> {
435+
if (cause is IllegalArgumentException) {
436+
throwBrokenCodeException(IllegalArgumentException(lazyMessage(), this.cause))
437+
}
438+
else {
439+
throwBrokenCodeException(IllegalStateException(lazyMessage(), this.cause))
440+
}
441+
}
442+
}
413443
}

idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluationBuilder.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ class KotlinEvaluator(val codeFragment: KtCodeFragment, val sourcePosition: Sour
301301
if (this.kind == ExceptionThrown.ExceptionKind.FROM_EVALUATED_CODE) {
302302
exception(InvocationException(this.exception.value as ObjectReference))
303303
}
304+
else if (this.kind == ExceptionThrown.ExceptionKind.BROKEN_CODE) {
305+
throw exception.value as Throwable
306+
}
304307
else {
305308
exception(exception.toString())
306309
}

0 commit comments

Comments
 (0)