50

我的同事建议使一些 Eclipse 代码格式和警告设置更加严格。这些更改中的大多数都是有意义的,但我在 Java 中得到了这个奇怪的警告。这是一些重现“问题”的测试代码:

package com.example.bugs;

public class WeirdInnerClassJavaWarning {
    private static class InnerClass
    {
        public void doSomething() {}
    }

    final private InnerClass anInstance;

    {
        this.anInstance = new InnerClass();   // !!!
        this.anInstance.doSomething();
    }
}
// using "this.anInstance" instead of "anInstance" prevents another warning,
// Unqualified access to the field WeirdInnerClassJavaWarning.anInstance    

与 !!! 在 Eclipse 中使用我的新警告设置给我这个警告:

对封闭构造函数 WeirdInnerClassJavaWarning.InnerClass() 的访问由合成访问器方法模拟。提高其知名度将提高您的绩效。

这是什么意思?当我将“私有静态类”更改为“受保护的静态类”时,警告消失了,这对我来说毫无意义。


编辑:我终于想出了“正确”的修复方法。这里真正的问题似乎是这个嵌套的私有静态类缺少公共构造函数。这一调整删除了警告:

package com.example.bugs;

public class WeirdInnerClassJavaWarning {
    private static class InnerClass
    {
        public void doSomething() {}
        public InnerClass() {}
    }

    final private InnerClass anInstance;

    {
        this.anInstance = new InnerClass();
        this.anInstance.doSomething();
    }
}

我希望该类是一个私有嵌套类(因此没有其他类可以访问它,包括封闭类的子类)并且我希望它是一个静态类。

我仍然不明白为什么使嵌套类受保护而不是私有是解决“问题”的另一种方法,但这可能是 Eclipse 的一个怪癖/错误。

(抱歉,为了更清楚,我应该称它为 NestedClass 而不是 InnerClass。)

4

6 回答 6

45

You can get rid of the warning as follows:

package com.example.bugs;

public class WeirdInnerClassJavaWarning {
    private static class InnerClass {
        protected InnerClass() {}  // This constructor makes the warning go away
        public void doSomething() {}
    }

    final private InnerClass anInstance;
    {
        this.anInstance = new InnerClass(); 
        this.anInstance.doSomething();
    }
}

As others have said, Eclipse is complaining because a private class with no explicit constructor cannot be instantiated from outside, except via the synthetic method that the Java compiler creates. If you take your code, compile it, and then decompile it with jad (*), you get the following (reformatted):

public class Test {
  private static class InnerClass {
    public void doSomething() {}
    // DEFAULT CONSTRUCTOR GENERATED BY COMPILER:
    private InnerClass() {}

    // SYNTHETIC METHOD GENERATED BY THE JAVA COMPILER:    
    InnerClass(InnerClass innerclass) {
      this();
    }
  }

  public Test() {
    anInstance.doSomething();
  }

  // Your instance initialization as modified by the compiler:
  private final InnerClass anInstance = new InnerClass(null);
}

If you add a protected constructor, the synthetic code is unnecessary. The synthetic code is theoretically, I suppose, slower by a minescule amount than non-synthetic code using a public or protected constructor.

(*) For jad, I linked to a Wikipedia page ... the domain that hosted this program has expired, but Wikipedia links to another that I have not tested myself. I know there are other (possibly more recent) decompilers, but this is the one I started using. Note: It complains when decompiling recent Java class files, but it still does a good job.

于 2009-05-28T14:37:32.013 回答
21

By the way, the setting to turn the warning off is in the Java Errors/Warnings page under "Code style" and is called:

Access to a non-accessible member of an enclosing type

于 2011-08-30T17:27:09.653 回答
9

您不能从 WeirdInnerClassJavaWarning 实例化 InnerClass。它是私有的,JVM 不会让你这样做,但 Java 语言(出于某种原因)会。

Therefore, javac would make an additional method in InnerClass that would just return new InnerClass(), therefore permitting you to create InnerClass instances from WeirdInnerClassJavaWarning.

i don't think you really need to get rid of it because the perfomance drop would be inmeasureably tiny. However, you can if you really want to.

于 2009-05-28T14:11:26.433 回答
4

I still don't understand why making the nested class protected rather than private is another method of fixing the "problem", but maybe that is a quirk/bug of Eclipse

That is not a quirk/bug of Eclipse, just a feature of Java. The Java Language Specification, 8.8.9 says:

... if the class is declared protected, then the default constructor is implicitly given the access modifier protected ...

于 2009-05-28T14:52:36.617 回答
3

To help folks out, here is what you get if you use the original class code in the question with

javac -XD-printflat WeirdInnerClassJavaWarning.java -d tmp

Raw output, compiler added the comments. Note the addition of the synthetic package private class and constructor.

public class WeirdInnerClassJavaWarning {
    {
    }

    public WeirdInnerClassJavaWarning() {
        super();
    }
    {
    }
    private final WeirdInnerClassJavaWarning$InnerClass anInstance;
    {
        this.anInstance = new WeirdInnerClassJavaWarning$InnerClass(null);
        this.anInstance.doSomething();
    }
}

class WeirdInnerClassJavaWarning$InnerClass {

    /*synthetic*/ WeirdInnerClassJavaWarning$InnerClass(WeirdInnerClassJavaWarning$1 x0) {
        this();
    }

    private WeirdInnerClassJavaWarning$InnerClass() {
        super();
    }

    public void doSomething() {
    }
}

/*synthetic*/ class WeirdInnerClassJavaWarning$1 {
}
于 2013-08-21T15:27:12.177 回答
0

You should be able to get rid of it by using the default scope instead of private or protected, i.e.

static class InnerClass ...

It's also worth noting that my placing your cursor on the line of code with the warning and pressing ctrl-1, Eclipse may be able to fix this automatically for you.

于 2009-05-28T14:19:49.647 回答