简体   繁体   English

Eclipse内容不支持Groovy文件中的Java对象

[英]Eclipse content assist not working for Java objects within Groovy files

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 . 我发现这个问题Eclipse Groovy和自动完成并遇到了同样的问题,但是,差不多三年了,我使用当前版本的Groovy Eclipse插件,并且似乎没有任何语法错误会让人困惑ANTLR

I also tried using the Groovy/Grails Tool Suite , and got the same results. 我也尝试使用Groovy / Grails工具套件 ,并得到了相同的结果。

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. 我已经模拟了一个简单的测试用例,其中内容辅助适用于Groovy对象的方法,以及Java对象的静态成员,但不适用于Java对象的方法。

The Groovy class: Groovy类:

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: Java类:

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. 我正在使用Eclipse Kepler Service Release 2, Groovy编译器2.0.7和Groovy Eclipse插件版本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). Java>编辑器>内容辅助>高级首选项菜单下,除了Groovy内容 (Java非类型,Java,Java类型提议)之外,我还确保检查所有Java选项。

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. 由于这似乎对一些人来说通常起作用,我想知道我在这里可能缺少什么,这将允许内容辅助工作Java对象方法。 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: 我正在继续考虑这个问题,并且考虑到了我刚刚测试过的内容辅助功能,尽管在较少的Groovy条件下:

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 , ie: 似乎问题的根源是Groovy Eclipse无法在Java对象上自动完成,如果它们是使用defGroovy方式声明的,即:

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. 我怀疑它与def本质上是Object的别名这一事实有关,即使该类型在技术上已知,它也可能被视为Object ,因此这是内容辅助操作的类型。

If I declare it in the traditional Java way, ie: 如果我用传统的Java方式声明它,即:

FooJava fooJava3 = new FooJava(4, "foo")

Content assist then allows me to find the members, methods, etc. of the FooJava object. 然后内容辅助允许我找到FooJava对象的成员,方法等。

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. 因此,我当然可以在将来解决这个问题,但是我想知道是否有任何方法可以基本上拥有我的蛋糕并且也可以吃它。

ie 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? ie有没有人找到一种方法来获取内容辅助来处理通过def语法声明的Java对象,只要该类型是已知的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM