0

I have let's say "Merge-Classes" that I call when I need to merge specific data with a specific JasperReport (Template). For each of those reports I need its own class to merge the data. I fill this reports dynamicaly, depending on which my custom wants.

Is it possible that I give my programm the name of the class and the method to call by a variable String and call that class/method? So it would be easy to add new Merge-Classes to my program (only copy paste the file and call by given name)

For example: I have a main program and a class named Report.java with a same name class and the method Test() in my main i have something like:

String classname = "Report.Test";
String parameters = "(var1, var2, var3)";

String end = """call class with params stored in classname+parameters"""
Flow
  • 13
  • 4
  • 2
    Possible duplicate of [Is there a way to instantiate a class by name in Java?](https://stackoverflow.com/questions/9886266/is-there-a-way-to-instantiate-a-class-by-name-in-java) and [How do I invoke a Java method when given the method name as a string?](https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string) – jhamon Nov 19 '19 at 08:52
  • i added same "sample code", hope it helps – Flow Nov 19 '19 at 08:59
  • check the two linked posts, mix them and that should fit your need – jhamon Nov 19 '19 at 09:00
  • Unclear what you're asking: is the `parameters` string literal or does it refer to named variables in scope? The latter is near-impossible to do in pure Java but may be a lot easier in a JavaScript interpreter (which is conveniently inluded in Java) – Mark Jeronimus Nov 19 '19 at 09:09
  • String parameters is just the second half of the class-call, you can also put the classname and the parameters in one String. I only wanted to show that everything should be flexible. Those are the tranfer parameters – Flow Nov 19 '19 at 09:18

0 Answers0