質問

I am using a GroovyClassLoader in my Java class to parse a certain (ideally complex) groovy file (to be executed in the next step):

Call in MyClass.java

final Class parsedClass = groovyClassLoader.parseClass(groovyFile);

Knowing that:

  • Groovy files need to be stored in the filesystem because would need to be change without redeployment.
  • This groovy file would need several imports:

GroovyFile.groovy imports

import com.my.import.one.Import1DTO
import com.my.import.two.Import2DTO
import com.my.import.three.Import3DTO
import com.my.import.four.Import4DTO
import com.my.import.five.Import5DTO

When the parseClass method is invoked, this exeception raises:

Exceptions

unable to resolve class com.my.import.one.Import1DTO;
unable to resolve class com.my.import.two.Import2DTO;
unable to resolve class com.my.import.three.Import3DTO;
unable to resolve class com.my.import.four.Import4DTO;
unable to resolve class com.my.import.five.Import5DTO;

Can I obtain the behaviour I expect without parse every import class before parse the base class?

Thanks!

正しい解決策はありません

他のヒント

Here is an example MyClass.java which uses the addClasspath() method on GroovyClassLoader

import groovy.lang.GroovyClassLoader;

public class MyClass {
    public static void main(String... args) {
        GroovyClassLoader groovyClassLoader = new GroovyClassLoader();

        // add "lib" to the classpath
        groovyClassLoader.addClasspath("lib");

        String groovyFile = "GroovyFile.groovy";
        Class parsedClass = groovyClassLoader.parseClass(groovyFile);
        System.out.println("class is " + parsedClass.toString());
    }
}

I assume that the DTOs are written in Groovy and that we use "myimport", since "my.import.x" will fail due to illegal syntax. If we have a "lib" directory like so, with compiled classes:

lib/com/myimport/one/Import1DTO.groovy
lib/com/myimport/one/Import1DTO.class
lib/com/myimport/two/Import2DTO.groovy
lib/com/myimport/two/Import2DTO.class

and that GroovyFile.groovy exists in the main directory. e.g.

import com.myimport.one.Import1DTO
import com.myimport.two.Import2DTO

println "hi there"

then the above Java code works for me.

I am using Groovy 2.2.1 with groovy-all-2.2.1.jar on the classpath (for GroovyClassLoader).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top