1

I'm pretty new to Groovy an I want to import a class from another groovy script in the main groovy script. I saw already this post. However due to environment restrictions I can't us functions like GroovyClassLoader or or import any libs/frameworks. The only possibility I have is to use the evaluate method.

I tried this:

main script (The evaluate does not throw any error)

File csvFile = new File("./Csvreader.groovy");
evaluate(csvFile);

Csvreader script

class CSVReader{
    def csvFile = new File('./test.csv');
    def separatorChar = ";"
    def values = [];
    def headers = [];

    void setSeperator(String s){
        if(s != ""){
            separatorChar = s;
        }
    }

    void readCsv(){
        csvFile.eachLine{ line, number ->
            if(number == 1){
                def head = line.split(separatorChar);
                for (entry in head) {
                    headers.add(entry);
                }
            } else{
                def value = line.split(separatorChar);
                def map =[:];
                def i = 0;
                for (entry in value) {
                    map.put(headers[i], entry);
                    i++;
                }
                values.add(map);
            }
        }

    }

    def getValues(){
        return values;
    }

    def getHeaders(){
        return headers;
    }

    def getSize(){
        return values.size();
    }

    def getLine(def keyParam, def value){
        for(int i = 0; i < values.size(); i++){
            def line = values[i];
            if(values[i][keyParam] == value){
                return values[i];
            }
        }
        
    }

}

However I would need to pass parameters with the evaluate call (and I would move the methods outside the class) or I need to create an instance of the class. Unfortunately I don't really know how I can do that, does anyone have a solution for that?

user15980267
  • 47
  • 1
  • 4
  • Why you can't use class loader? Are you in jenkins pipeline? – daggett May 20 '21 at 11:37
  • Not exactly but the product I am using is so restricted that not all Groovy methods are allowed – user15980267 May 20 '21 at 11:54
  • ok. there is no such method as `evaluate` in groovy. seems it's provided by your product and you have to read a manual of this product... – daggett May 20 '21 at 12:40
  • Are you sure about that? Cause in the linked [post](https://stackoverflow.com/questions/9136328/including-a-groovy-script-in-another-groovy?noredirect=1&lq=1) the top answer was evaluate – user15980267 May 20 '21 at 13:32
  • ok. i got it - you mean Script.evaluate method https://docs.groovy-lang.org/latest/html/api/groovy/lang/Script.html#evaluate(java.io.File) – daggett May 20 '21 at 13:43
  • Ahh ok thanks for the link. As I can see the method does not allow to add parameters. Therefore I have to think of something else. Thanks for your help. – user15980267 May 20 '21 at 13:47

1 Answers1

0

you could try to put at the end of your Csvreader.groovy file the expression return CSVReader.class that will return you a compiled class.

Then in your caller script you could create an instance of this class and use it as any other class instance.

Csvreader.groovy

class CSVReader{
    def separatorChar = ";"
    
    void setSeparator(String s){
        if(s != ""){
            separatorChar = s;
        }
    }
}

return CSVReader.class

caller script

File csvFile = new File("./Csvreader.groovy")
def CSVReaderClass = evaluate(csvFile)
def cvsReader = CSVReaderClass.newInstance()

cvsReader.setSeparator("Z")
println cvsReader.separatorChar
daggett
  • 26,404
  • 3
  • 40
  • 56