This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extendible way to pickle specialized classes
* Reuse code in `RuntimePicklerRegistry` and `sourcegen`. * Create `ClassMapper` to delegate the tasks of checking class equality.
- Loading branch information
Showing
3 changed files
with
51 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package scala.pickling.util | ||
|
||
object ClassMapper { | ||
|
||
val specializedTuplesTemplate = | ||
List( | ||
"$mcII$sp", "$mcIJ$sp", "$mcID$sp", "$mcIZ$sp", "$mcJI$sp", "$mcJJ$sp", | ||
"$mcJD$sp", "$mcJC$sp", "$mcJZ$sp", "$mcDI$sp", "$mcDJ$sp", "$mcDD$sp", | ||
"$mcDC$sp", "$mcDZ$sp", "$mcCI$sp", "$mcCJ$sp", "$mcCD$sp", "$mcCC$sp", | ||
"$mcCZ$sp", "$mcZI$sp", "$mcZJ$sp", "$mcZD$sp", "$mcZC$sp", "$mcZZ$sp" | ||
) | ||
|
||
/* Map specialized classes to classes. Canonical use case: tuples. | ||
* We map classes instead of strings to check at runtime that they exist. */ | ||
val specialMappingClasses: Map[Class[_], Class[_]] = | ||
mapSpecializedTuplesFor("scala.Tuple2") // add also other special cases | ||
|
||
def specializedTupleNamesFor(tupleClassName: String): List[String] = | ||
specializedTuplesTemplate.map(tupleClassName + _) | ||
|
||
def mapSpecializedTuplesFor(tupleClassName: String): Map[Class[_], Class[_]] = { | ||
val tupleClass = Class.forName(tupleClassName) | ||
specializedTupleNamesFor(tupleClassName) | ||
.map(Class.forName).map(_ -> tupleClass).toMap | ||
} | ||
|
||
@inline def isSpecializedClass(specialized: Class[_], clazz: Class[_]) = | ||
specialMappingClasses.get(specialized).contains(clazz) | ||
|
||
def areSameClasses(clazz: Class[_], clazzT: Class[_]): Boolean = | ||
clazz == clazzT || isSpecializedClass(clazz, clazzT) | ||
|
||
} |