Skip to content

Instantly share code, notes, and snippets.

@varnerac
Created October 19, 2016 16:18
Show Gist options
  • Save varnerac/4bac246dc76afd61b08ae6b58dd4d138 to your computer and use it in GitHub Desktop.
Save varnerac/4bac246dc76afd61b08ae6b58dd4d138 to your computer and use it in GitHub Desktop.
Attempt to Instantiate Protobuf Companion Object from Name (String)
def getProtoCompanionClass(protoName: String): Option[GeneratedMessageCompanion[_]] = {
try {
val protoClassSymbol = currentMirror.staticClass(protoName)
val protoCompanionObjectSymbol = protoClassSymbol.companion.asModule
Some(currentMirror.reflectModule(protoCompanionObjectSymbol).instance.asInstanceOf[GeneratedMessageCompanion[_]])
} catch {
case _: ScalaReflectionException => None
}
}
// ProtoReflection.scala:6: type arguments [_$2] do not conform to trait GeneratedMessageCompanion's type parameter bounds [A <: com.trueaccord.scalapb.GeneratedMessage with com.trueaccord.scalapb.Message[A]]
// [error] Some(currentMirror.reflectModule(protoCompanionObjectSymbol).instance.asInstanceOf[GeneratedMessageCompanion[_]])
// [error] ^
// [error] one error found
// [error] (compile:compileIncremental) Compilation failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment