【问题标题】:How to set Code Formatter for Java Anonymous Methods in Eclipse如何在 Eclipse 中为 Java 匿名方法设置代码格式化程序
【发布时间】:2012-06-22 17:42:30
【问题描述】:

我正在使用 Eclipse 进行 Android 开发,并且我已经设置了我的代码格式化样式,但仍然有一些我无法弄清楚如何在 Eclipse 中格式化的匿名方法。这就是 Eclipse 现在格式化匿名方法的方式:

// The BroadcastReceiver that listens for discovered devices and
    // changes the title when discovery is finished
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
                                                  @Override
                                                  public void onReceive(Context context, Intent intent) {
                                                      String action = intent.getAction();
                                                      Utils.Log.i("BLUETOOTH: " + action);
                                                      if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                                                          // Get the
                                                          // BluetoothDevice
                                                          // object from the
                                                          // Intent
                                                          BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                                                          // If it's already
                                                          // paired, skip it,
                                                          // because it's been
                                                          // listed already
                                                          if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                                                              if (mNewDevicesArrayAdapter.getCount() == 0) {
                                                                  mNewDevicesArrayAdapter.add(device);
                                                              }
                                                              btDevicesUpdateList.add(device);
                                                          }
                                                      }
                                                      else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                                                          mNewDevicesArrayAdapter.setItems(btDevicesUpdateList);
                                                          mNewDevicesArrayAdapter.notifyDataSetChanged();
                                                          btDevicesUpdateList.clear();
                                                          mBtAdapter.startDiscovery();
                                                      }
                                                      else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                                                          if (mBtAdapter.getState() == BluetoothAdapter.STATE_ON) {
                                                              switchToView(viewBluetoothOn);
                                                              firstTimeDiscover();
                                                          }
                                                          else if (mBtAdapter.getState() == BluetoothAdapter.STATE_OFF) {
                                                              switchToView(viewBluetoothOff);
                                                          }
                                                      }
                                                  }
                                              };

看到了吗?它非常糟糕。将匿名方法声明的格式设置为保留在左侧并且不在= 等号字符下方的正确设置是什么?

【问题讨论】:

  • 请发布您的 .settings/org.eclipse.jdt.core.prefs 的内容
  • 我花了整整五分钟清理那个糟糕的格式,才意识到问题是关于所说的糟糕的格式。 ****头桌****
  • @EugeneKuleshov 这是项目特定的设置。
  • 不确定是什么问题?您没有项目特定的格式化程序设置?然后从格式编辑器中导出全局的。
  • 代码格式与项目设置无关。它是一个内置的 Eclipse 功能。问题是我找不到格式化 Anonymus 方法的设置。

标签: java eclipse coding-style


【解决方案1】:

我认为导致这种错误格式的设置是“对齐列中的字段”,如果你关闭它,类/接口实现应该从行首缩进而不是等号。

我在 Eclipse 中打开了一个错误,以修复默认行为或为类/接口实现添加另一个设置。

https://bugs.eclipse.org/bugs/show_bug.cgi?id=385901

【讨论】:

    【解决方案2】:

    解决这个问题的方法是稍微改变你的编码风格。 剪切并粘贴以下内容并运行格式化程序。样式 #2 看起来 不那么糟糕。

    // **Style #1** - Formatter handles poorly
    JDialog jDialog1 = new JDialog(new JFrame()) {
        public void setBackground(final Color c) {
           super.setBackground(c);
        }
    };
    
    // **Style #2** - Formatter handles well.
    JDialog jDialog2;
    {
        jDialog2 = new JDialog(new JFrame()) {
            public void setBackground(final Color c) {
                super.setBackground(c);
            }
        };
    }
    

    【讨论】:

    • 有效! Eclipse 应该按照@RedLenses 的建议修复格式化程序,但目前这是一个很好的修复。
    【解决方案3】:

    您似乎有某种自定义格式化程序设置。转到项目属性/Java 代码样式/格式化程序/启用项目特定设置,然后选择“Java 约定”内置格式化程序配置文件。

    【讨论】:

    • 是的,我有。正如我在问题中已经提到的那样,我已经设置了一些自定义格式设置,但我不知道如何处理 Anonymus 方法的格式...
    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 2012-11-18
    • 1970-01-01
    • 2017-02-16
    相关资源
    最近更新 更多