|
A small example how to read the lastModified date from a groovy class file without Reflection.
Attached the original testcase for the bug again.
See discussion on groovy-dev mailing list - the difficulty is that the compiler uses Field.get to access a timestamp that was added to the compiled class. However, this causes the class to be initialized, which in turn causes static initializers to be executed.
http://www.nabble.com/Groovy-runs-code-in-static-initializers-during-compile-tf3607689.html I don't think there's anything wrong with using Class objects to access type information about classes, so I don't think it's necessary to find an alternative way of reading a class file - however, care must be taken not to initialize the class file during compilation. An alternative approach to solving this problem is to check for the existence of a source file and object file, and compare the file modification times - this avoids the need to store a modification time in the class file. I've attached a patch that uses this approach - it runs the attached example without triggering the runtime exception from the compiler: http://www.nabble.com/Groovy-runs-code-in-static-initializers-during-compile-tf3607689.html This is just a "proof of concept", but I think it suggests another way of tackling this issue. In which revision has it been fixed?
In which revision in trunk?
I'd like to review the fix. 8517 and above
The fix: The test: The fix looks good to me, thanks!
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The problem is this: Foo does some strange stuff in its constructor.
StaticInitBug has a static initializer of type Foo.
Bar uses StaticInitBug. While the compiler compiles Bar, it tries to find out the lastModificationTime of the class StaticInitBug. This code resolves the class using reflection. The net result is that StaticInitBug is loaded and initialized which leads to the execution of the code in Foo.
This should not happen. I guess using reflection in the Groovy compiler is a "must not". Some other means to analyze a class without actually executing any code in it must be found.