thanneeruvenu / spock

Automatically exported from code.google.com/p/spock
0 stars 0 forks source link

@Shared property that is overridden in derived class causes problems #81

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
class CleanInsertXSpec extends Specification {
 @Shared
 @Inject
 Things thingFactory
...

 void setup() {
   thingFactory.xxxxxx --> NullPointerException() 
 }
....
}

class Y extends CleanInsertXSpec {
 ThingFactory thingFactory = new ThingFactory()
}

@Shared turns field access thingFactory.xxx into property access, and getter is 
overridden in 
derived class. Thus we end up accessing the wrong field (and that field isn't 
initialized yet). One 
(brute force) way around this might be to make getter/setter of @Shared 
properties private.

Original issue reported on code.google.com by pnied...@gmail.com on 9 Mar 2010 at 7:08

GoogleCodeExporter commented 8 years ago

Original comment by pnied...@gmail.com on 24 May 2010 at 11:35

GoogleCodeExporter commented 8 years ago

Original comment by pnied...@gmail.com on 8 Dec 2010 at 1:38

GoogleCodeExporter commented 8 years ago
Unfortunately it's not clear how to solve this issue while keeping the benefits 
of @Shared. One possible solution is to simply make @Shared an alias for the 
`static` keyword. This gives up on the strict isolation between shared fields 
but I doubt whether this matters in practice. On the plus side, it would not 
only solve this issue but also allow us to get rid of quite some magic (which 
can, for example, get in the way during debugging).

Original comment by pnied...@gmail.com on 20 Feb 2012 at 12:08