javac 是否应该查找同名匿名类之外的方法?

发布于 2024-07-07 11:29:06 字数 1192 浏览 7 评论 0原文

这个问题是以下问题的后续: 为什么可以我不是在同名的匿名类之外调用方法 上一个

问题回答为什么,但现在我想知道 javac 应该 是否找到 run (int栏)? (请参阅上一个问题以了解 run(42) 失败的原因)

如果不应该,是否是由于规范所致? 它会产生不明确的代码吗? 我的观点是,我认为这是一个错误。 虽然上一个问题解释了为什么此代码无法编译,但我认为如果 javac 在当前级别找不到匹配项,并且在树中的更高位置进行搜索,则应该编译它。 IE。 如果 this.run() 不匹配,它应该自动检查 NotApplicable.this 的 run 方法。

另请注意, foo(int bar) 已正确找到。 如果您给出不应找到 run(int bar) 的任何原因,它还必须解释为什么找到 foo(int bar) 。

public class NotApplicable {

    public NotApplicable() {
        new Runnable() {
            public void run() {

                // this works just fine, it automatically used NotApplicable.this when it couldn't find this.foo
                foo(42);

                // this fails to compile, javac find this.run(), and it does not match
                run(42);

                // to force javac to find run(int bar) you must use the following
                //NotApplicable.this.run(42);
            }
        };
    }

    private void run(int bar) {
    }

    public void foo(int bar) {
    }
}

This question is a follow up to:
Why can’t I call a method outside of an anonymous class of the same name

This previous question answer why, but now I want to know if javac should find run(int bar)? (See previous question to see why run(42) fails)

If it shouldn't, is it due to a spec? Does it produce ambiguous code? My point is, I think this is a bug. While the previous question explained why this code fails to compile, I feel it should compile if javac searched higher in the tree if it fails to find a match at the current level. IE. If this.run() does not match, it should automatically check NotApplicable.this for a run method.

Also note that foo(int bar) is correctly found. If you give any reason why run(int bar) shouldn't be found, it must also explain why foo(int bar) is found.

public class NotApplicable {

    public NotApplicable() {
        new Runnable() {
            public void run() {

                // this works just fine, it automatically used NotApplicable.this when it couldn't find this.foo
                foo(42);

                // this fails to compile, javac find this.run(), and it does not match
                run(42);

                // to force javac to find run(int bar) you must use the following
                //NotApplicable.this.run(42);
            }
        };
    }

    private void run(int bar) {
    }

    public void foo(int bar) {
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不回头走下去 2024-07-14 11:29:07

对我来说,这听起来像是一个导致歧义和脆弱的秘诀——一旦在基类中添加了新方法(好吧,对于接口来说不太可能......),代码的含义就会完全改变。

匿名类已经相当丑陋了 - 使这一点明确一点也不困扰我。

Sounds like a recipe for ambiguity and fragility to me - as soon as a new method is added in your base class (okay, not so likely for an interface...) the meaning of your code changes completely.

Anonymous classes are pretty ugly already - making this bit of explicit doesn't bother me at all.

找回味觉 2024-07-14 11:29:07

尝试

NotApplicable.this.run(42);

一下。

Try

NotApplicable.this.run(42);

instead.

丘比特射中我 2024-07-14 11:29:06

javac 的这种行为符合规范。 请参见§15.12 方法调用表达式 Java 语言规范,特别是“编译时步骤 1”下的段落解释了非限定方法调用的含义:

如果标识符出现在具有该名称的可见方法声明的范围 (§6.3) 内,则必须有一个包含该方法的成员的封闭类型声明。 令 T 为最里面的此类类型声明。 要搜索的类或接口是 T。

换句话说,在所有封闭范围中搜索非限定方法 name,以及最里面的“类型声明”(意味着类或接口声明)其中找到的名称就是将搜索整个签名的名称(在“编译时步骤 2”中)。

This behavior of javac conforms to the spec. See §15.12 Method Invocation Expressions in the Java Language Specification, specifically the paragraph under "Compile Time Step 1" explaining the meaning of an unqualified method invocation:

If the Identifier appears within the scope (§6.3) of a visible method declaration with that name, then there must be an enclosing type declaration of which that method is a member. Let T be the innermost such type declaration. The class or interface to search is T.

In other words, the unqualified method name is searched for in all enclosing scopes, and the innermost "type declaration" (which means either a class or an interface declaration) in which the name is found is the one that will be searched for the whole signature (in "Compile Time Step 2").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文