I'm making a Scala app that sets by reflection field values. This works OK.
However, in order to set field values I need a created instance. If I have a class with an empty constructor, I can do this easily with classOf[Person].getConstructors....
However, when I try doing this with a Case class with a non empty constructor It doesn't work. I have all of the field names and its values, as well as the Object type I need to create. Can I instance the Case Class somehow with what I've got?
The only thing I don't have is the parameter names from the Case Class constructor or a way to create this without parameter and then setting the values via reflection.
Let's go to the example.
I have the following
case class Person(name : String, age : Int)
class Dog(name : String) {
def this() = {
name = "Tony"
}
}
class Reflector[O](obj : O) {
def setValue[F](propName : String, value : F) = ...
def getValue(propName : String) = ...
}
//This works
val dog = classOf[Dog].newInstance()
new Reflector(dog).setValue("name", "Doggy")
//This doesn't
val person = classOf[Person].newInstance //Doesn't work
val ctor = classOf[Person].getConstructors()(0)
val ctor.newInstance(parameters) //I have the property names and values, but I don't know
// which of them is for each parameter, nor I name the name of the constructor parameters
The approach below works for any Scala class that has either a no-arg ctor or has an all defaulted primary ctor.
It makes fewer assumptions than some others about how much information is available at the point of call as all it needs is a Class[_] instance and not implicits etc. Also the approach does not rely on the class having to be a case class or having a companion at all.
FYI During construction precedence is given to the no-arg ctor if present.
I ran into a similar problem. Given the ease of using Macro Paradise, Macro Annotations are a solution (for scala 2.10.X and 2.11 so far).
Check out this question and the example project linked in the comments below.
If you are looking for a way to instantiate the object with no arguments, you could do the same as you did in your example, just so long as your reflection setter can handle setting the immutable vals.
You would provide an alternate constructor, as below:
Note that the case class will not generate a zero-arg companion object, so you will need to instantiate it as:
new Person()
orclassOf[Person].newInstance()
. However, that should be what you are looking to do.Should give you output like:
The case class should have default args, so that you can just
Person()
; in the absence of a default arg, supplying a null for name might (or ought to) hit a require(name != null).Alternatively, use reflection to figure out which params have defaults and then supply nulls or zeros for the rest.