Gradle equivalent for Class.forName

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

Why wouldn’t you iterate over the classes, instead of the names?

What I am trying to achieve in my plug-in is to simplify the following:

project.allprojects {

    new EarConfigurator(it).configure()
    new WarConfigurator(it).configure()        
    new JarConfigurator(it).configure()        
    new RepoConfigurator(it).configure()        
    new VersionConfigurator(it).configure()
    etc
}

I have to run all the configurations, because this project is a corporate plug-in for all company projects. These company projects projects can vary from one to many jars ears and wars.

It’s still not clear for me what you want to achieve, but how about creating class which implements Plugin<Project>?

for exsample…

public class MyPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        new EarConfigurator(project).configure();
        // ... and more
    }
}

and its build script…

allprojects {
    apply plugin: 'your-plugin-id'
}

This class and build script will lead you to achieve your requirement.

That is exactly what I am doing. I have a plug-in and in the I am running the project.allprojects:

public class BuildDefaultsPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.allprojects {
            new EarConfigurator(it).configure()
            new WarConfigurator(it).configure()
            new JarConfigurator(it).configure()
            new RepoConfigurator(it).configure()
            new VersionConfigurator(it).configure()
        }
    }
}

If I understand correctly, you’d probably make each of your ‘Configurator’ classes plugins and just apply each one (e.g., project.apply(plugin: EarConfigurator)). I’d probably make BuildDefaultsPlugin only apply to one project and put the allprojects {} in the build script. That would make it easier to compose plugins.

How about this?

def classNames = ['EarConfigurator',...]
project.allprojects { p ->
  classNames.each { Eval.x(p, "new $it(x).configure()") }
}

Close, but no cigar.

Script1.groovy: 1: unable to resolve class EarConfigurator
@ line 1, column 1.
new EarConfigurator(x).configure()
^

Ok, so what if you put the full class name in?

Same problem

Script1.groovy: 1: unable to resolve class za.co.quinn.EarConfigurator
@ line 1, column 1.
new za.co.quinn.EarConfigurator(x).configure()
^

1 error

I gave up and did it with Java:

def classNames = ['EarConfigurator', 'JarConfigurator', 'WarConfigurator']

void apply(Project project) {

    project.allprojects { p ->

        classNames.each { className ->
            getConfigurator(className, p).configure()
        }
    }
}

Configurator getConfigurator(String name, Project project) {
    return Class.forName(name).newInstance(project)
}