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:
- I want the code to be reusable
- 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?