ModifierOrderer { static JavaInput reorderModifiers(String text) throws FormatterException { return reorderModifiers( new JavaInput(text), ImmutableList.of(Range.closedOpen(0, text.length()))); }    }
@Test public void simple() throws FormatterException { assertThat(ModifierOrderer.reorderModifiers("static abstract class InnerClass {}").getText()) .isEqualTo("abstract static class InnerClass {}"); }
@Test public void comment() throws FormatterException { assertThat(ModifierOrderer.reorderModifiers("staticabstractpublic").getText()) .isEqualTo("publicabstractstatic"); }
@Test public void everything() throws FormatterException { assertThat( ModifierOrderer.reorderModifiers( "strictfp native synchronized volatile transient final static abstract" + " private protected public") .getText()) .isEqualTo( "public protected private abstract static final transient volatile synchronized" + " native strictfp"); }
@Test public void everythingIncludingDefault() throws FormatterException { assertThat( ModifierOrderer.reorderModifiers( "strictfp native synchronized volatile transient final static default abstract" + " private protected public") .getText()) .isEqualTo( "public protected private abstract default static final transient volatile synchronized" + " native strictfp"); }
@Test public void subRange() throws FormatterException { String[] lines = { "class Test {", " static public int a;", " static public int b;", "}", }; String input = Joiner.on('\n').join(lines); String substring = "static public int a"; int start = input.indexOf(substring); int end = start + substring.length(); String output = ModifierOrderer.reorderModifiers( new JavaInput(input), Arrays.asList(Range.closedOpen(start, end))) .getText(); assertThat(output).contains("public static int a;"); assertThat(output).contains("static public int b;"); }
@Test public void whitespace() throws FormatterException { String[] lines = { "class Test {", " static", " public int a;", "}", }; String input = Joiner.on('\n').join(lines); String substring = "static public int a"; int start = input.indexOf(substring); int end = start + substring.length(); String output = ModifierOrderer.reorderModifiers( new JavaInput(input), Arrays.asList(Range.closedOpen(start, end))) .getText(); assertThat(output).contains("public\n static int a;"); }