in scalafix/rules/src/main/scala/fix/v0_14_0/FixAvroCoder.scala [191:267]
override def fix(implicit doc: SemanticDocument): Patch = {
val usesAvroCoders = doc.tree
.collect {
// A method call with a type parameter requires an implicit Coder[T] for our type
case q"$expr[..$tpesnel]"
if findBoundedTypes(expr, tpesnel, CoderMatcher).exists(isAvroType) =>
true
// Coder[T] is a variable type where T is an avro type
case t @ q"..$mods val ..$patsnel: $tpeopt = $expr"
if findMatchingValTypeParams(t, CoderMatcher)
.exists(isAvroType) =>
true
case q"$fn(..$args)" if methodHasAvroCoderTypeBound(fn) => true
case q"..$mods def $ename(...$params): $tpe = $expr" if methodReturnsAvroSCollection(tpe) =>
true
case q"$fn(..$args)"
if JobTestBuilderMatcher.matches(fn) &&
(args.headOption match {
case Some(q"$io[$tpe](..$args)") if isAvroType(tpe.symbol) => true
case _ => false
}) =>
true
case q"$fn(..$args)" if ParallelizeMatcher.matches(fn) && (args.headOption exists {
case expr if hasAvroTypeSignature(expr, true) => true
case q"$seqLike($elem)"
if seqLike.symbol.value.startsWith("scala/collection/") &&
(isAvroType(elem.symbol) || hasAvroTypeSignature(elem, false)) =>
true
case _ => false
}) =>
true
case q"$expr(..$args)" if SmbReadMatchers.matches(expr) =>
args.tail.map(_.symbol.info.map(_.signature)).exists {
case Some(
MethodSignature(_, _, TypeRef(_, readSymbol, List(TypeRef(_, avroSymbol, _))))
) if AvroSmbReadMatchers.matches(readSymbol) && isAvroType(avroSymbol) =>
true
case Some(MethodSignature(parents, _, _)) if parents.map(_.signature).exists {
case TypeSignature(_, _, TypeRef(_, s, _)) if AvroMatcher.matches(s) => true
case _ => false
} =>
true
case _ => false
}
}
.foldLeft(false)(_ || _)
val importPatches = doc.tree.collect {
case importer"com.spotify.scio.coders.Coder.{..$imps}" =>
// fix direct import from Coder
imps.collect {
case i @ (importee"avroGenericRecordCoder" | importee"avroSpecificRecordCoder" |
importee"avroSpecificFixedCoder") =>
Patch.removeImportee(i)
}.asPatch
case importer"com.spotify.scio.avro.{..$imps}" =>
imps
.filterNot(_.isInstanceOf[Importee.Wildcard])
.map(Patch.removeImportee)
.asPatch
case t @ q"$obj.$fn" if AvroCoderMatcher.matches(fn.symbol) =>
// fix direct usage of Coder.avro*
Patch.replaceTree(t, q"$fn".syntax)
}.asPatch
val importWildcardPatch = {
val hasAvroWildcardImport = doc.tree
.collect { case importer"com.spotify.scio.avro.{..$imps}" =>
imps.exists(_.isInstanceOf[Importee.Wildcard])
}
.foldLeft(false)(_ || _)
if (hasAvroWildcardImport) Patch.empty else Patch.addGlobalImport(avroImport)
}
if (usesAvroCoders || importPatches.nonEmpty) importPatches + importWildcardPatch
else Patch.empty
}