2

For debugging purposes, I would like, at Runtime, to be able to display the .groovy source file that defined a given Class. Is it possibly natively ?

Note that the groovy class might not be in the stacktrace, but a class given in argument. The result will be a function with the signature:

String getSourceInfo(Class<?> clazz) {...} //returns /home/user/work/..../SomeClass.groovy

As an extension, we could also retrieve line numbers or use this function on fields and methods:

String getSourceInfo(Field field) {...} //returns /..../SomeClass.groovy#13:5

If not, I was thinking of adding a Groovy AST adding an annotation on the ClassNode. It should not be too difficult, as we have all the required information (in SEMANTIC_ANALYSIS for example) but there might be some standard way. Any suggestion?

Benoît
  • 1,080
  • 11
  • 28
  • Note on the margin: may exist Class without file, compiled "it the fly" for example from Java application hosting Groovy – Jacek Cz Sep 07 '15 at 11:18
  • try http://stackoverflow.com/questions/227486/find-where-java-class-is-loaded-from – Ashraf Purno Sep 07 '15 at 11:19
  • @Jacek : Yes indeed. In this case, the only information available will be the script's name (often generated).In our application, we mostly use .groovy files on the FileSystem, that the system loads and compiles at Runtime – Benoît Sep 07 '15 at 11:21
  • @Ashraf : The .class file location does not interest me in this case. I want to be able to direct the user to the .groovy file he wrote – Benoît Sep 07 '15 at 11:23

1 Answers1

0

I remembered the technique to get some runtime info in Java, if Groovy script is still live is OK, but when has been died (analyze post mortem) not fit:

            e= new Exception();
            StackTraceElement[] st = e.getStackTrace();
            for(StackTraceElement s:st)
            {
                s.getLineNumber()
            }

// no throw, only get info

Jacek Cz
  • 1,872
  • 1
  • 15
  • 22
  • Good point, but in my case, the current stacktrace might not go through the wanted groovy class. Will edit my question to be clearer – Benoît Sep 07 '15 at 12:57