-
-
Notifications
You must be signed in to change notification settings - Fork 142
Refactor - adding different scala object detecting #656
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
base: 2.17
Are you sure you want to change the base?
Changes from 7 commits
641b96e
49e6234
9cf38e7
65cf493
dc128d0
f40112b
8981eb1
fb860be
517a8a4
17f6362
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not add to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it the classes with names ending in It is weird to have classes ending in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.