John Mullins
John Mullins

Reputation: 1101

How to get manifest from string representation for type with parameters

There is a way for get class by name, for example Class.forName("com.project.model.User").

Is it possible to get manifest from string representation? For example, there are two situations:

Get manifest from type-name without type parameters - it works:

val str = "com.project.model.User"
val mf = Manifest.classType(Class.forName(str))

How to get manifest from type-name with type parameters:

val str = "com.project.model.Event[User]"

val mf: Manifest[Event[User]] = howToGetManifestByName(str)  // ???

environment: scala-2.11.8

Upvotes: 0

Views: 911

Answers (1)

Jon Anderson
Jon Anderson

Reputation: 696

You can use ManifestFactory.classType(cls) to get the manifest for a class instance.

import scala.reflect.ManifestFactory

class MyClass(str: String)
val c = Class.forName("MyClass")
val manifest = ManifestFactory.classType(c)

Upvotes: 4

Related Questions