papaiatis
papaiatis

Reputation: 4291

How to set Code Formatter for Java Anonymous Methods in Eclipse

I'm using Eclipse for Android development and I have already set up my code formatting style but still have the anonymous methods that I couldn't figure out how to format in Eclipse. This is how Eclipse formats anonymous methods now:

// 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);
                                                          }
                                                      }
                                                  }
                                              };

See? Its very crappy. What is the correct setting to format the anonymous method declaration to stay in the left side and don't go under the = equal character?

Upvotes: 6

Views: 868

Answers (3)

Java42
Java42

Reputation: 7706

The work-a-round for this issue is to slightly change your coding style. Cut and paste the following and run your formatter. Style #2 will look less crappy.

// **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);
        }
    };
}

Upvotes: 2

jt-gilkeson
jt-gilkeson

Reputation: 2721

I believe the setting that causes this bad formatting is "Align fields in columns", if you turn this off, the class/interface implementation should be indented from the start of the line instead of the equals.

I opened a bug at eclipse to either fix the default behavior or to add another setting for class/interface implementations.

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

Upvotes: 3

Eugene Kuleshov
Eugene Kuleshov

Reputation: 31795

It seem like you have some kind of custom formatter settings. Go to project properties / Java Code Style / Formatter / Enable Project Specific settings and then select "Java Conventions" built-in formatter profile.

Upvotes: 0

Related Questions