How to set code formatting for Java Anonymous Methods in Eclipse

I use Eclipse for Android development, and I already set up my code formatting style, but still have anonymous methods that I could not figure out how to format in Eclipse. Here's how Eclipse now formats anonymous methods:

// 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 already // paired, skip it, // because it 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); } } } }; 

Cm? This is very trashy. What is the correct option for formatting an anonymous method declaration so that it stays on the left side and does not fall under the = equal symbol?

+6
source share
3 answers

I believe that the parameter that causes this incorrect formatting is "Align fields in columns", if you disable it, the implementation of the class / interface should be indented from the beginning of the line instead of equal.

I opened an error in eclipse to either fix the default behavior or add another parameter to implement the class / interface.

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

+3
source

Work on a round for this problem is to slightly change the 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); } }; } 
+2
source

It seems that you have some kind of custom formatting settings. Go to the project properties / Java Code Style / Formatter / Enable Project Specific settings, and then select the built-in Java Conventions formatting profile.

0
source

Source: https://habr.com/ru/post/918783/


All Articles