1

I need to dynamically instantiate an Object in Scala. The class name is passed as a parameter to the main method. The actual class which has all methods is imported from another package

I referred solutions in below links

instantiate object with reflection using constructor arguments Scala: How do I dynamically instantiate an object and invoke a method using reflection?

My problems is I am not able pass the class name parameter to asInstanceOf. Below is the code.

import com.xxx.xxx.warehouse.sbi
val classParam = "sbi" //to make it easy, i hardcoded the value.
val constructor = Class.forName(classParam).getConstructors()(0)
val args = Array[AnyRef](String,String)
val instance1 
=constructor.newInstance(args:_*).asInstanceOf[classParam]

Last line in above code throws error classParam cannot be resolved. asInstanceOf would need aparameter of type T. My class "sbi" has so many methods .But in this case, how do I solve this. There are multiple classes similar to "sbi"

What I am trying to solve:

I have 500 case classes to be imported from com.xxx.xxx.warehouse package. Let us name them sbi1, sbi2, sbi3...sbi500. All of those classes extend another class which has multiple methods. There is a method "getLatestVesrion" which finds latest version of the S3 file based on the class name. Each case class takes a S3 file as input

I need to fetch the latest version of each S3 file for all 500 data sets in S3 using above classes. Instead of calling each class explicitly,I want to invoke those classes dynamically as per the need. If I invoke them explicitly, I need to write 2 lines of code for each class. So, I would be passing the class name sbi1, sbi2,sbi3 ..sbi500 during runtime. Based on the classname, latest version needs to be fetched.

So technically, given a string for class name, how can I access methods in that class name?

Lakshmi
  • 11
  • 2

1 Answers1

0

I don't think I understand what you're trying to achieve here !

Let's recap :

  • The actual class type will only be known at runtime

  • You're trying to cast instance1 in the code, which would force its type at compile time

  • But if you knew at compile how to cast instance1, you wouldn't need the runtime parameter to begin with !

I suspect you won't be able to cast instance1 in any way, because there's no known type at compile time to which to cast it ^^

If you have a kind of common interface for all your classes, you can plug it in there though, it should be fine.

Edit : I think you're after a "classic" reflection invoke after all, look here : https://stackoverflow.com/a/39135220/2404988 :-)

C4stor
  • 8,355
  • 6
  • 29
  • 47
  • Hi C4stor, sorry I was not clear initially. I edited the original question. I hope that will help you understand what I am trying to solve. I agree with all your 3 points. There is no common interface for my classes – Lakshmi Nov 08 '18 at 18:11
  • Hi, I edited my answer :-) Please have a look there, I think that's what you want : https://stackoverflow.com/a/39135220/2404988 – C4stor Nov 09 '18 at 08:50