Skip to content

Commit 93963c2

Browse files
committed
Try fix ObjectCollectedException
1 parent 8d0ca5c commit 93963c2

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

modules/core/src/main/scala/ch/epfl/scala/debugadapter/internal/evaluator/JdiClass.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private[internal] class JdiClass(
2828
for {
2929
_ <- prepareMethod(ctr)
3030
instance <- Safe(cls.newInstance(thread, ctr, args.map(_.value).asJava, ObjectReference.INVOKE_SINGLE_THREADED))
31-
.recoverWith(recoverInvocationException(thread))
31+
.recoverWith(wrapInvocationException(thread))
3232
} yield JdiObject(instance, thread)
3333

3434
// Load the argument types of the method to avoid ClassNotLoadedException
@@ -64,7 +64,7 @@ private[internal] class JdiClass(
6464
private def invokeStatic(method: Method, args: Seq[JdiValue]): Safe[JdiValue] =
6565
Safe(cls.invokeMethod(thread, method, args.map(_.value).asJava, ObjectReference.INVOKE_SINGLE_THREADED))
6666
.map(JdiValue(_, thread))
67-
.recoverWith(recoverInvocationException(thread))
67+
.recoverWith(wrapInvocationException(thread))
6868
}
6969

7070
object JdiClass {

modules/core/src/main/scala/ch/epfl/scala/debugadapter/internal/evaluator/JdiObject.scala

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,19 @@ private[internal] class JdiObject(
2929
protected def invoke(method: Method, args: Seq[JdiValue]): Safe[JdiValue] = {
3030
Safe(reference.invokeMethod(thread, method, args.map(_.value).asJava, ObjectReference.INVOKE_SINGLE_THREADED))
3131
.map(JdiValue(_, thread))
32-
.recoverWith(recoverInvocationException(thread))
32+
.recoverWith(wrapInvocationException(thread))
3333
}
3434

35-
protected def recoverInvocationException(thread: ThreadReference): PartialFunction[Throwable, Safe[Nothing]] = {
36-
case t: InvocationException =>
37-
extractMessage(t, thread).map { message =>
38-
throw new MethodInvocationFailed(message, JdiObject(t.exception, thread))
35+
protected def wrapInvocationException(thread: ThreadReference): PartialFunction[Throwable, Safe[Nothing]] = {
36+
case invocationException: InvocationException =>
37+
for {
38+
exception <- Safe(invocationException.exception).map(JdiObject(_, thread))
39+
message <- exception.invoke("toString", List()).map(_.asString.stringValue).recover { case _ => "" }
40+
} yield {
41+
throw new MethodInvocationFailed(message, exception)
3942
}
4043
}
4144

42-
private def extractMessage(invocationException: InvocationException, thread: ThreadReference): Safe[String] =
43-
JdiObject(invocationException.exception(), thread)
44-
.invoke("toString", List())
45-
.map(_.asString.stringValue)
46-
.recover { case _ => "" }
47-
4845
// we use a Seq instead of a Map because the ScalaEvaluator rely on the order of the fields
4946
def fields: Seq[(String, JdiValue)] =
5047
reference.referenceType.fields.asScala.toSeq

modules/core/src/main/scala/ch/epfl/scala/debugadapter/internal/evaluator/ScalaEvaluator.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ private[internal] class ScalaEvaluator(
4242
classLoader <- frame.classLoader()
4343
(names, values) <- extractValuesAndNames(frame, classLoader)
4444
localNames = names.map(_.stringValue).toSet
45-
_ <- Safe(
46-
compiler.compile(outDir, expressionClassName, sourceFile, line, expression, localNames, packageName, testMode)
47-
)
45+
_ <- compiler
46+
.compile(outDir, expressionClassName, sourceFile, line, expression, localNames, packageName, testMode)
47+
.toSafe
4848
} yield CompiledExpression(outDir, expressionFqcn)
4949
compiledExpression.getResult
5050
}
@@ -85,7 +85,6 @@ private[internal] class ScalaEvaluator(
8585
args: List[JdiObject]
8686
): Safe[JdiObject] = {
8787
val expressionClassPath = classDir.toUri.toString
88-
getClass()
8988
for {
9089
expressionClassLoader <- classLoader.createChildLoader(classDir)
9190
expressionClass <- expressionClassLoader.loadClass(className)

modules/core/src/main/scala/ch/epfl/scala/debugadapter/internal/evaluator/package.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ch.epfl.scala.debugadapter.internal
22

3+
import scala.util.Try
4+
35
package object evaluator {
46
implicit class SafeSeq[A](seq: Seq[Safe[A]]) {
57
def traverse: Safe[Seq[A]] = {
@@ -13,4 +15,8 @@ package object evaluator {
1315
def traverse: Safe[Option[A]] =
1416
opt.map(s => s.map(Option.apply)).getOrElse(Safe(None))
1517
}
18+
19+
implicit class TryToSafe[A](t: Try[A]) {
20+
def toSafe: Safe[A] = Safe(t)
21+
}
1622
}

0 commit comments

Comments
 (0)