2

I have a file called a.groovy In a.groovy I have:

println(this.class.name)

When load a.groovy (which runs it) I see "Script1" as the outcome of the println.

How do I get the loaded file's name - "a" or "a.groovy?"


UPDATE 1

I tried the following:

class TrustedLib {
    void Test(Class scriptClass) {
        String scriptPath = 
            scriptClass.protectionDomain.codeSource.location.path
        println(scriptPath)
    }
}

This is called from the script (node) as:

Test(getClass())

Test is in a global trusted library because:

  1. I want the code to be reusable
  2. I can't call for the protectionDomain inside a sandbox.

The scriptPath appears as "/Groovy/shell" (the scriptClass, btw is Script1)

Note: If I change Test to:

class TrustedLib {
    void Test() {
        String scriptPath = 
            getClass().protectionDomain.codeSource.location.path
        println(scriptPath)
    }
}

THE_PATH/TrustedLib.groovy is printed, which is the correct path

So, foiled again - how do I get the real file name? How do I get the underlying class?

mkobit
  • 43,979
  • 12
  • 156
  • 150
Amir
  • 151
  • 4
  • 13
  • https://stackoverflow.com/questions/11512978/why-does-groovy-class-return-a-different-value-than-getclass – rohit thomas Jul 11 '18 at 03:06
  • I appreciate the ability to copy and paste, but that has absolutely nothing to do with my question. – Amir Jul 11 '18 at 14:39
  • What are you trying to achieve? Maybe there is a different way to model your requirement? – mkobit Jul 12 '18 at 14:17
  • I am trying to load a /vars library that is specific to the script based on the script name. – Amir Jul 12 '18 at 15:00
  • An alternative would be to be able to pass a parameter to the load function, as that would allow me to pass the name of the config file as a parameter to the script. I.e., are there any parameters i can pass to `script.load` function? – Amir Jul 12 '18 at 15:10

0 Answers0