2

Is there a gradle equivalent for Class.forName or a more gradle/groovy way of doing it.

What I am trying to achieve in gradle is the following:

    for(String name : new String[]{
            MyImpl1.class.getName() ,
            MyImpl2.class.getName() ,
            MyImpl3.class.getName()}) {
        MyInterface myInterface = (MyInterface)Class.forName(name).newInstance();
        myInterface.configure();
    }

Where MyImpl1, MyImpl2 and MyImpl3 implements MyInterface

javapenguin
  • 966
  • 2
  • 22
  • 38

1 Answers1

0

As per Guillaume LaForge's (one of the main contributors to the Groovy language) answer here:

def name = "java.lang.Integer"// Fully qualified class name
def s = ("$name" as Class).parseInt("10")
println s

But I guess if you want greater control over the specific classloader, doing it the Java way Class.forName("Foo", true, this.getClass().getClassLoader()) works in Groovy too.

Community
  • 1
  • 1
real_paul
  • 574
  • 2
  • 22