如何使用内联匿名类声明避免合成访问编译器警告,这意味着什么?

Chr*_*ght 5 java eclipse compiler-warnings java-synthetic-methods

我有以下代码:

public class SomeClass {
   //InterfaceUpdateListener is an interface
   private InterfaceUpdateListener listener = new InterfaceUpdateListener(){
        public void onUpdate() {
           SomeClass.this.someMethod();  //complier complains on this line of code
        }
   };

   private void someMethod() {
     //do something in here on an update event occuring
   }

   //other code to register the listener with another class...
}
Run Code Online (Sandbox Code Playgroud)

我在 Eclipse 中的编译器抱怨说

Access to enclosing method 'someMethod' from type SomeClass is emulated by a synthetic accessor method.

谁能准确解释一下

  1. 这是什么意思,
  2. 如果我保持原样(因为它只是一个警告),可能的后果可能意味着什么,以及
  3. 我该如何解决?

谢谢

JB *_*zet 4

我只是停用该规则(即使编译器不为此生成警告)。如果构造是合法的,并且编译器添加了额外的方法来支持它,那么它就必须这样做。

我怀疑这种合成方法会造成性能的显着损失。如果有必要,JIT 无论如何都必须内联它。

  • 综合方法在调试代码时可能会造成严重的麻烦。热代码替换不支持添加/删除方法,但是当您背后添加/删除合成方法时,对用户来说并不透明。 (2认同)