Skip to content

Commit

Permalink
Fix compatibility with a latest Scala version (2.13.8) (#144)
Browse files Browse the repository at this point in the history
* Fix compatibility with a latest Scala version (2.13.8)

* Apply scalafix changes
  • Loading branch information
miloradvojnovic authored Feb 1, 2022
1 parent fdf3192 commit 4daaaff
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.tools.nsc.Phase
import scala.tools.nsc.plugins.PluginComponent

import org.virtuslab.ash.CodecRegistrationCheckerCompilerPlugin.classSweepPhaseName
import org.virtuslab.ash.annotation.SerializabilityTrait
import org.virtuslab.ash.CodecRegistrationCheckerCompilerPlugin.serializabilityTraitType

class ClassSweepCompilerPluginComponent(options: CodecRegistrationCheckerOptions, override val global: Global)
extends PluginComponent {
Expand All @@ -18,8 +18,6 @@ class ClassSweepCompilerPluginComponent(options: CodecRegistrationCheckerOptions
val foundTypes: mutable.Buffer[(String, String)] = mutable.ListBuffer()
val typesToUpdate: mutable.Buffer[(String, String)] = mutable.ListBuffer()

private val serializabilityTraitType = typeOf[SerializabilityTrait]

override def newPhase(prev: Phase): Phase =
new StdPhase(prev) {
private val up = options.oldTypes.groupBy(_._2)
Expand All @@ -31,7 +29,7 @@ class ClassSweepCompilerPluginComponent(options: CodecRegistrationCheckerOptions
case x: ClassDef => x.impl
}
.flatMap(x => x.parents.map((_, x)))
.filter(_._1.symbol.annotations.map(_.tpe).contains(serializabilityTraitType))
.filter(_._1.symbol.annotations.map(_.tpe.toString()).contains(serializabilityTraitType))
.map(x => (x._1.tpe.typeSymbol.fullName, x._2.tpe.typeSymbol.fullName))
typesToUpdate ++= body
.collect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ object CodecRegistrationCheckerCompilerPlugin {
val classSweepPhaseName = "codec-registration-class-sweep"
val serializerCheckPhaseName = "codec-registration-serializer-check"
val cacheFileName = "codec-registration-checker-cache.csv"
val serializabilityTraitType = "org.virtuslab.ash.annotation.SerializabilityTrait"
val serializerType = "org.virtuslab.ash.annotation.Serializer"

def parseCacheFile(buffer: ByteBuffer): Seq[(String, String)] = {
StandardCharsets.UTF_8.decode(buffer).toString.split("\n").toSeq.filterNot(_.isBlank).map(_.split(",")).map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import scala.tools.nsc.Phase
import scala.tools.nsc.plugins.PluginComponent

import org.virtuslab.ash.CodecRegistrationCheckerCompilerPlugin.classSweepPhaseName
import org.virtuslab.ash.CodecRegistrationCheckerCompilerPlugin.serializabilityTraitType
import org.virtuslab.ash.CodecRegistrationCheckerCompilerPlugin.serializerCheckPhaseName
import org.virtuslab.ash.annotation.SerializabilityTrait
import org.virtuslab.ash.annotation.Serializer
import org.virtuslab.ash.CodecRegistrationCheckerCompilerPlugin.serializerType

class SerializerCheckCompilerPluginComponent(
classSweep: ClassSweepCompilerPluginComponent,
Expand All @@ -29,9 +29,6 @@ class SerializerCheckCompilerPluginComponent(
override def description: String =
s"Checks marked serializer for references to classes found in $serializerCheckPhaseName"

private val serializerType = typeOf[Serializer]
private val serializabilityTraitType = typeOf[SerializabilityTrait]

private var typesNotDumped = true
private val typesToCheck = mutable.Map[String, List[(String, String)]]().withDefaultValue(Nil)

Expand Down Expand Up @@ -69,7 +66,7 @@ class SerializerCheckCompilerPluginComponent(
.collect {
case x: ImplDef => (x, x.symbol.annotations)
}
.map(x => (x._1, x._2.filter(_.tpe =:= serializerType)))
.map(x => (x._1, x._2.filter(_.tpe.toString() == serializerType)))
.filter(_._2.nonEmpty)
.foreach { x =>
val (implDef, annotations) = x
Expand All @@ -86,12 +83,12 @@ class SerializerCheckCompilerPluginComponent(
val (fqcn, filterRegex) = serializerAnnotation.args match {
case List(clazzTree, regexTree) =>
val fqcn = extractValueOfLiteralConstantFromTree[Type](clazzTree).flatMap { tpe =>
if (tpe.typeSymbol.annotations.map(_.tpe).contains(serializabilityTraitType))
if (tpe.typeSymbol.annotations.map(_.tpe.toString()).contains(serializabilityTraitType))
Some(tpe.typeSymbol.fullName)
else {
reporter.error(
serializerAnnotation.pos,
s"Type given in annotation argument must be annotated with ${serializabilityTraitType.typeSymbol.fullName}")
s"Type given in annotation argument must be annotated with $serializabilityTraitType")
None
}
}
Expand Down Expand Up @@ -140,7 +137,7 @@ class SerializerCheckCompilerPluginComponent(
reporter.error(
serializerImplDef.pos,
s"""No codecs for ${missingFqcn
.mkString(", ")} are registered in class annotated with @${serializabilityTraitType.typeSymbol.fullName}.
.mkString(", ")} are registered in class annotated with @$serializabilityTraitType.
|This will lead to a missing codec for Akka serialization in the runtime.
|Current filtering regex: $filterRegex""".stripMargin)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ object SerializabilityCheckerCompilerPlugin {
val disableMethodsUntyped = "--disable-detection-untyped-methods"
val disableHigherOrderFunctions = "--disable-detection-higher-order-function"
}
val serializabilityTraitType = "org.virtuslab.ash.annotation.SerializabilityTrait"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.tools.nsc.Global
import scala.tools.nsc.Phase
import scala.tools.nsc.plugins.PluginComponent

import org.virtuslab.ash.annotation.SerializabilityTrait
import org.virtuslab.ash.SerializabilityCheckerCompilerPlugin.serializabilityTraitType

class SerializabilityCheckerCompilerPluginComponent(
val pluginOptions: SerializabilityCheckerOptions,
Expand All @@ -20,8 +20,6 @@ class SerializabilityCheckerCompilerPluginComponent(

override def newPhase(prev: Phase): Phase =
new StdPhase(prev) {
private val serializabilityTraitType = typeOf[SerializabilityTrait]

private val akkaSerializabilityTraits = Seq(
"com.google.protobuf.GeneratedMessage",
"com.google.protobuf.GeneratedMessageV3",
Expand Down Expand Up @@ -162,13 +160,13 @@ class SerializabilityCheckerCompilerPluginComponent(
reporter.error(
detectedPosition,
s"""${tpe
.toString()} is used as Akka ${classType.name} but does not extend a trait annotated with ${serializabilityTraitType.toLongString}.
|Passing an object of class NOT extending ${serializabilityTraitType.nameAndArgsString} as a ${classType.name} may cause Akka to fall back to Java serialization during runtime.
.toString()} is used as Akka ${classType.name} but does not extend a trait annotated with $serializabilityTraitType.
|Passing an object of class NOT extending $serializabilityTraitType as a ${classType.name} may cause Akka to fall back to Java serialization during runtime.
|
|""".stripMargin)
reporter.error(
tpe.typeSymbol.pos,
s"""Make sure this type is itself annotated, or extends a type annotated with @${serializabilityTraitType.toLongString}.""")
s"""Make sure this type is itself annotated, or extends a type annotated with @$serializabilityTraitType.""")
annotatedTraits
}
}
Expand All @@ -180,7 +178,7 @@ class SerializabilityCheckerCompilerPluginComponent(
private def findSuperclassAnnotatedWithSerializabilityTrait(tp: Type): Option[Type] = {
if (tp =:= typeTag[AnyRef].tpe || tp =:= typeTag[Any].tpe)
None
else if (tp.typeSymbol.annotations.exists(_.atp =:= serializabilityTraitType))
else if (tp.typeSymbol.annotations.exists(_.atp.toString() == serializabilityTraitType))
Some(tp)
else if (akkaSerializabilityTraits.contains(tp.typeSymbol.fullName))
Some(tp)
Expand Down

0 comments on commit 4daaaff

Please sign in to comment.