I found this question Eclipse Groovy and autocompletion and am experiencing the same issue, however, it is almost three years hence and I am using the current version of the Groovy Eclipse plugin, and there do not appear to be any syntax errors that would be confusing ANTLR.
I also tried using the Groovy/Grails Tool Suite, and got the same results.
I have mocked up a simple test case under which the content assist works for Groovy objects' methods, and for Java objects' static members, but not Java objects' methods.
The Groovy class:
package test_groovy
import test_groovy.FooJava
class FooGroovy
{
def fooJava = [2, "baz"] as FooJava
def x = 4
def FooGroovy()
{
// empty constructor
}
def useFooJava()
{
// only displays Groovy methods, not the java ones
def str = fooJava.getStr()
println "str: ${str}"
// static members like this *can* be found via content assist
def str2 = FooJava.FOO_STR
println "str2: ${str2}"
// This is also not found via content assist
def str3 = fooJava.dumpToStr()
println "str3: ${str3}"
}
def fooBar()
{
return x + 3
}
static void main(def args)
{
def fooGroovy = [] as FooGroovy
// Groovy object methods can be found via content assist
def res = fooGroovy.fooBar()
println "res: ${res}"
fooGroovy.useFooJava()
}
}
The Java class:
package test_groovy;
public class FooJava
{
private long bar;
private String str;
public static final String FOO_STR = "foo";
public FooJava(long bar, String str)
{
super();
this.bar = bar;
this.str = str;
}
public long getBar()
{
return bar;
}
public void setBar(long bar)
{
this.bar = bar;
}
public String getStr()
{
return str;
}
public void setStr(String str)
{
this.str = str;
}
public String dumpToStr()
{
return new String("Str: " + str + ", bar: " + bar);
}
}
I am using Eclipse Kepler Service Release 2, Groovy compiler 2.0.7 and Groovy Eclipse plugin version 2.8.0.xx-20130703-1600-e43-RELEASE.
Under the Java > Editor > Content Assist > Advanced preferences menu, I have made sure all the Java options are checked off in addition to Groovy Content (Java Non-Type, Java, Java type Proposals).
Since this does seem to work in general for some, I am wondering what I could be missing here that would allow content assist to work for Java object methods. Thanks.
I was continuing to think about this, and had a thought which I just tested out which allows content-assist to work, albeit under less-Groovy conditions:
It appears that the root of the problem is that Groovy Eclipse can't do auto-complete on Java objects if they are declared in a Groovy way using def
, i.e.:
def fooJava = [2, "baz"] as FooJava
def fooJava2 = new FooJava(3, "bar")
even though in both of these cases, the type is known at compile time. I suspect it has something to do with the fact that def
is essentially an alias for Object
, and even though the type is technically known, it is probably treated as Object
, and so that's the type on which the content-assist operates.
If I declare it in the traditional Java way, i.e.:
FooJava fooJava3 = new FooJava(4, "foo")
Content assist then allows me to find the members, methods, etc. of the FooJava
object.
So, I can certainly do this in the future to work around the issue, but I do wonder if there is any way to essentially have my cake and eat it too.
i.e. Has anyone found a way to get content assist to work on Java objects declared via the def
syntax, so long as the type is known?