How to access @Singleton classes from Java classes?
I was just playing around with the Groovy support of IntelliJ. How can I access classes that are marked with @Singleton from a Java class?
Suppose, I have something like
@Singleton
class PointHolder {
def registeredPoints = []
def addPoint(Point point) {
registeredPoints << point
}
}
As I understand the description, there should be a method
public static PointHolder getInstance()
available but IDEA highlights that as error and also gives a compilation error.
Thanks,
Dirk
请先登录再写评论。
> How can I access classes that are marked with @Singleton from a Java class?
You cannot. This is not an IDEA issue, but a known limitation of the Groovy compiler.
Cheers,
Peter
More accurately, this does work when Groovy and Java code are compiled separately (live in separate modules), but not when they are compiled together (live in the same module).
I have separated Java and Groovy code into two modules. I can now compile the code but code completion doesn't work anymore. But that's something that I can live with for the time being.
How do you set up your projects? Do you mix Java and Groovy classes or do you keep them separate?
Thanks for the info,
Dirk
Groovy's goal is to allow arbitrary mixing of Java and Groovy code. Although this already works in principle, there are still quite a few problems to solve for it to work reliably in different build environments (command line, build tools, IDEs) and for non-trivial projects. Depending on your needs and your willingness to live on the cutting edge, I would choose one of the following strategies:
Cheers,
Peter
9 years later this still seems to be an issue. Has there been any progress? I am using Groovy Eclipse Compiler 2.4.15 from Maven.