0

I have around 250 classes with almost the same functions. The selection of classes is based on the parameter.

The name of the class is customObject(n) where n is 1-250.

Instead of adding 250 if else conditions based on the parameter I want to dynamically create the instance of the class based on the given parameter.

I have created a string field for the class names and I've tried the below.

String ClassName = "FullPath" + className + n + "object.create()";

assert "className";
def object = ClassName.execte();

assert is working fine but I can't return the object from assert as I need to do further operations with this dynamic object.

def test = assert "className";
assert "def test = " + className;
return test;

the above lines aren't working and execute method is throwing an error that the file is not found.

I have also tried groovy -e "script" but it's not working as excepted as well.

How can I create a dynamic object to optimize my code instead of creating 250 if conditions?

The only piece of code that I want to change is

Long ObjectRk = 1000; //ObjectRk is the function parameter
def Object = CustomObject110.object.fetchIfExist(ObjectRk);

here at 110, I want to write the class of my choice and the remaining piece of code is general.

Azeem112
  • 337
  • 1
  • 8
  • 23

0 Answers0