Skip to content

Commit

Permalink
fix AbstractMethodError
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Jun 26, 2019
1 parent 97548b7 commit 864d22b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 9 additions & 5 deletions core/src/main/scala/org/json4s/Formats.scala
Expand Up @@ -271,7 +271,6 @@ trait KeySerializer[A] {
* </pre>
*/
trait TypeHints {
import ClassDelta._

val hints: List[Class[_]]

Expand All @@ -295,9 +294,13 @@ trait TypeHints {
/**
* Adds the specified type hints to this type hints.
*/
def + (hints: TypeHints): TypeHints = CompositeTypeHints(hints.components ::: components)
def + (hints: TypeHints): TypeHints = TypeHints.CompositeTypeHints(hints.components ::: components)

private[TypeHints] case class CompositeTypeHints(override val components: List[TypeHints]) extends TypeHints {
}

private[json4s] object TypeHints {

private case class CompositeTypeHints(override val components: List[TypeHints]) extends TypeHints {
val hints: List[Class[_]] = components.flatMap(_.hints)

/**
Expand All @@ -307,15 +310,15 @@ trait TypeHints {
(components.reverse
filter (_.containsHint(clazz))
map (th => (th.hintFor(clazz), th.classFor(th.hintFor(clazz)).getOrElse(sys.error("hintFor/classFor not invertible for " + th))))
sortWith ((x, y) => (delta(x._2, clazz) - delta(y._2, clazz)) <= 0)).head._1
sortWith((x, y) => (ClassDelta.delta(x._2, clazz) - ClassDelta.delta(y._2, clazz)) <= 0)).head._1
}

def classFor(hint: String): Option[Class[_]] = {
def hasClass(h: TypeHints) =
scala.util.control.Exception.allCatch opt (h.classFor(hint)) map (_.isDefined) getOrElse(false)

components find (hasClass) flatMap (_.classFor(hint))
}
}

override def deserialize: PartialFunction[(String, JObject), Any] = components.foldLeft[PartialFunction[(String, JObject),Any]](Map()) {
(result, cur) => result.orElse(cur.deserialize)
Expand All @@ -325,6 +328,7 @@ trait TypeHints {
(result, cur) => result.orElse(cur.serialize)
}
}

}

private[json4s] object ClassDelta {
Expand Down
12 changes: 11 additions & 1 deletion tests/src/test/scala/org/json4s/FormatsSpec.scala
Expand Up @@ -28,5 +28,15 @@ class FormatsSpec extends Specification {
}
}


// https://github.com/json4s/json4s/issues/550
// https://github.com/scala/bug/issues/9948
"issue#550 avoid scalac bug" in {
val a = new TypeHints {
override val hints: List[Class[_]] = List.empty
override def hintFor(clazz: Class[_]): String = ""
override def classFor(hint: String): Option[Class[_]] = None
}
a + a
success
}
}

0 comments on commit 864d22b

Please sign in to comment.