There are a lot of related questions, but so far I haven't found any that covers this exact problem.
I'm using TypeTag to try and get the class name for a method in a class with a type parameter.
class ValidatePCollectionSize[T](implicit tt: TypeTag[T]) extends PTransform[PCollection[T], PCollection[T]] {
private val className = tt.getClass.getSimpleName
override def expand(input: PCollection[T]): PCollection[T] = {
input
.apply(s"CountElementsIn$className", Count.globally())
.apply("ValidOrThrow", ParDo.of(new PCollectionSizeValidator(className)))
input
}
}
I call the transform like this:
val foo: PCollection[Foo] = {
pipeline
.apply(
"ReadFooFromSource",
// read transform, details elided
SourceRead[Foo]
)
.apply("ValidateFooRead", new ValidatePCollectionSize[Foo])
}
And the logic of the method does work, but the val className
gets evaluated to the string "TypeTagImpl", instead of the actual name of the class in the type argument.
Any ideas why it's not getting the right name?
tt.getClass
obtains the Class[TypeTag[?]]
rather than Class[T]
.
You probably wanted implicit ClassTag[A]
and then classTag[A].runtimeClass
if you wanted clazz.getSimpleName
.