But honestly, "new XXX()" for getting the singleton instance? This solution can also be found in the AspectJ cookbook. IMHO this is a no go! There is also a quote:
The presented implementation should be well documented, because nothing would be more irritating than a novice developer in your team who doesn't know about this magic behind the scenes and relies on different instances of the singleton class ("I thought new would be doing...").
I cannot believe that the trivial singleton code above cannot be weaved into some classes (e.g. with MySingletonAnnotation). Is there no solution which allows the writing of "XXX.getInstance()"?
Because of the singleton pattern's reliance on static methods, there would be no clean solution in an aspect. I agree with you that the solution posted in the blog is not what I would suggest. I'll play around a bit and see if I can come up with a better solution myself. However, my guess is that no matter what, there will always be a cast involved around the getInstance() method.
Probably, there is no better solution. Otherwise, somebody might have found it already. However, using the word "find" for such a trivial case speaks volumes *sigh* 10 years ago I really expected that aspect-oriented programming could revolutionize programming (or clean the mess, at least). I tried using AOP in about 5 or 6 new Projects now. Each time I run into some trouble in the beginning :/ Either there are bugs in some products, other techical issues (modern platforms etc.) or I cannot solve trivial problems which should be in the scope of AOP like the posted issue. Colleagues of mine gave up a long time ago :/
The best thing to do is to post to the AJ users mailing list, see here: http://eclipse.org/aspectj/userlists.php. And you are correct that not all problems are solvable using AspectJ. However, there is a certain class of problems that it excels at (modularizing crosscutting concerns like security, transactions, logging, etc).