Community
Participate
Working Groups
Version: Luna Release (4.4.0) Build id: 20140612-0600 For the following method: @SuppressWarnings("unchecked") public <T> ClasspathScanner addSubclassMatcher(final Class<T> superclass, final ClassMatchProcessor<T> classMatchProcessor) { classMatchers.add(() -> { ClassInfo superclassInfo = classNameToClassInfo.get(superclass.getName()); boolean foundMatches = false; if (superclassInfo != null) { for (ClassInfo subclassInfo : superclassInfo.allSubclasses) { try { Class<? extends T> klass = (Class<? extends T>) Class.forName(subclassInfo.name); classMatchProcessor.processMatch(klass); foundMatches = true; } catch (ClassNotFoundException | NoClassDefFoundError e) { throw new RuntimeException(e); } } } if (!foundMatches) { // TODO: Log.i("No classes found with superclass " + superclass.getName()); } }); return this; } If I insert a line comment inside the try block, the indentation gets screwed up from that point on: @SuppressWarnings("unchecked") public <T> ClasspathScanner addSubclassMatcher(final Class<T> superclass, final ClassMatchProcessor<T> classMatchProcessor) { classMatchers.add(() -> { ClassInfo superclassInfo = classNameToClassInfo.get(superclass.getName()); boolean foundMatches = false; if (superclassInfo != null) { for (ClassInfo subclassInfo : superclassInfo.allSubclasses) { try { // Only now do we call the classloader (Class.forName()), it is slow Class<? extends T> klass = (Class<? extends T>) Class.forName(subclassInfo.name); classMatchProcessor.processMatch(klass); foundMatches = true; } catch (ClassNotFoundException | NoClassDefFoundError e) { throw new RuntimeException(e); } } } if (!foundMatches) { // TODO: Log.i("No classes found with superclass " + superclass.getName()); } }) ; return this; }
Created attachment 245529 [details] The same formatting examples, but without the line wrapping
Turns out it is the lambda that is causing the formatting issue with line comments. If you insert a line comment below the "() -> {" line, everything indents. if you insert it after the "if (superclassInfo != null) {" line, nothing happens. If you insert it after the "for" or "try", everything outdents.
Manoj, please investigate and see how critical it is.
Here are two standalone examples of this problem. Both are exactly as formatted by eclipse with default settings. The only difference between the first case and the second is that the second case has an additional comment after the "try {" line. Indentation is wrong in both cases, but the first case has a lot more wrong with it. public class Test { @FunctionalInterface public static interface Z { public void z(String a, String b, String c); } private static void y(String s, Z object) { } public static void main(String[] args) { // comment y("hello", (a, b, c) -> { // comment try { if (a == null) { // comment throw new IllegalArgumentException(); } else if (a.equals("test")) { System.out.println("a equals \"test\""); } } catch (Exception e) { e.printStackTrace(); } // comment } ); // comment } } public class Test { @FunctionalInterface public static interface Z { public void z(String a, String b, String c); } private static void y(String s, Z object) { } public static void main(String[] args) { // comment y("hello", (a, b, c) -> { // comment try { // comment if (a == null) { // comment throw new IllegalArgumentException(); } else if (a.equals("test")) { System.out.println("a equals \"test\""); } } catch (Exception e) { e.printStackTrace(); } // comment }); // comment } }
Created attachment 253185 [details] Formatter configuration which exposes the indentation issue Lambda methods with line comments expose the bug. Setting the default indentation to 1 functions as a workaround. Attached is the formatter profile I use that exposes this bug.
This problem no longer occurs after the formatter redesign. *** This bug has been marked as a duplicate of bug 303519 ***
Verified using Eclipse Version: Neon (4.6) Build id: I20160425-1300