Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor - adding different scala object detecting #656

Open
wants to merge 10 commits into
base: 2.17
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
package com.fasterxml.jackson.module.scala.deser

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind._
import com.fasterxml.jackson.databind.deser.Deserializers
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.fasterxml.jackson.databind._
import com.fasterxml.jackson.module.scala.JacksonModule
import com.fasterxml.jackson.module.scala.util.ClassW
import com.fasterxml.jackson.module.scala.util.ScalaObject

import scala.languageFeature.postfixOps
import scala.util.control.NonFatal

private class ScalaObjectDeserializer(clazz: Class[_]) extends StdDeserializer[Any](classOf[Any]) {
override def deserialize(p: JsonParser, ctxt: DeserializationContext): Any = {
try {
clazz.getField("MODULE$").get(null)
} catch {
case NonFatal(_) => null
}
}
private class ScalaObjectDeserializer(scalaObject: Any) extends StdDeserializer[Any](classOf[Any]) {
override def deserialize(p: JsonParser, ctxt: DeserializationContext): Any = scalaObject
}

private object ScalaObjectDeserializerResolver extends Deserializers.Base {
override def findBeanDeserializer(javaType: JavaType, config: DeserializationConfig, beanDesc: BeanDescription): JsonDeserializer[_] = {
val clazz = javaType.getRawClass
if (ClassW(clazz).isScalaObject)
new ScalaObjectDeserializer(clazz)
else null

Option(clazz)
.collect {
case ScalaObject(value) => new ScalaObjectDeserializer(value)
}
.orNull
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.fasterxml.jackson.module.scala.util

import java.lang.reflect.Field

object ScalaObject {
pjfanning marked this conversation as resolved.
Show resolved Hide resolved

private val MODULE_FIELD_NAME = "MODULE$"

private def getStaticField(field: Field): Option[Any] =
try Some(field.get(null))
catch {
case _: NullPointerException | _: IllegalAccessException => None
}

private def moduleFieldOption(clazz: Class[_]): Option[Field] =
try Some(clazz.getDeclaredField(MODULE_FIELD_NAME))
catch {
case _: NoSuchFieldException => None
}

private def moduleFieldValue(clazz: Class[_]): Option[Any] = for {
moduleField <- moduleFieldOption(clazz)
value <- getStaticField(moduleField)
} yield value

def unapply(clazz: Class[_]): Option[Any] =
if (clazz.getSimpleName.endsWith("$")) moduleFieldValue(clazz)
else None
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.fasterxml.jackson.module.scala.util

import org.scalatest.matchers.should.Matchers.{contain, convertToAnyShouldWrapper, empty}
import org.scalatest.wordspec.AnyWordSpecLike

object TestObject

case object TestCaseObject

class TestClass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not add to src/test/scala ? does this not work with scala 3?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it the classes with names ending in $ - maybe scala3 doesn't allow this?

It is weird to have classes ending in $ - is there any way not to use these in tests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are not necessary in tests but they are used just to make sure ScalaObject.unapply do not detect classes with names that end with a $ as scala singleton objects

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These class names are probably legal in scala 2 but illegal in scala 3. Since I haven't seen anyone in their right mind to use such class names I'm deleting them.


class TestClassWithModuleField {
val MODULE$: TestClassWithModuleField = this
}

class TestClass$

class TestClassWithModuleField$ {
val MODULE$: TestClassWithModuleField$ = this
}

class ScalaObjectTest extends AnyWordSpecLike {

"ScalaObject" must {
"return Some(TestObject) for unapply(TestObject.getClass)" in {
ScalaObject.unapply(TestObject.getClass) should contain(TestObject)
}

"return Some(TestCaseObject) for unapply(TestCaseObject.getClass)" in {
ScalaObject.unapply(TestCaseObject.getClass) should contain(TestCaseObject)
}

"return None for unapply(testClassInstance.getClass)" in {
val testClassInstance = new TestClass
ScalaObject.unapply(testClassInstance.getClass) shouldBe empty
}

"return None for unapply(testClassWithModuleFieldInstance.getClass)" in {
val testClassWithModuleFieldInstance = new TestClassWithModuleField
ScalaObject.unapply(testClassWithModuleFieldInstance.getClass) shouldBe empty
}

"return None for unapply(testClassWithADollarInstance.getClass)" in {
val testClassWithADollarInstance = new TestClass$
ScalaObject.unapply(testClassWithADollarInstance.getClass) shouldBe empty
}

"return None for unapply(testClassWithModuleFieldAndADollarInstance.getClass)" in {
val testClassWithModuleFieldAndADollarInstance = new TestClassWithModuleField$
ScalaObject.unapply(testClassWithModuleFieldAndADollarInstance.getClass) shouldBe empty
}
}
}
Loading