From e073b0d18afd95d5b638607f43b852c46785202c Mon Sep 17 00:00:00 2001 From: Remko Popma Date: Wed, 8 Dec 2021 16:13:34 +0900 Subject: [PATCH] [BUILD] Java 5 compatibility: work around Java 5 compiler limitations --- src/test/java/picocli/CommandLineTest.java | 24 ++++++------- src/test/java/picocli/CommandMethodTest.java | 5 +-- src/test/java/picocli/Demo.java | 6 ++-- src/test/java/picocli/ExecuteLegacyTest.java | 6 +++- src/test/java/picocli/HelpSubCommandTest.java | 4 +-- src/test/java/picocli/HelpTest.java | 8 ++--- src/test/java/picocli/MixinTest.java | 2 +- .../java/picocli/OptionMethodSpecTest.java | 2 +- src/test/java/picocli/SubcommandTests.java | 34 +++++++++---------- src/test/java/picocli/TypeConversionTest.java | 4 +-- 10 files changed, 50 insertions(+), 45 deletions(-) diff --git a/src/test/java/picocli/CommandLineTest.java b/src/test/java/picocli/CommandLineTest.java index edac2d3cc..51d9db0cf 100644 --- a/src/test/java/picocli/CommandLineTest.java +++ b/src/test/java/picocli/CommandLineTest.java @@ -1828,8 +1828,8 @@ public void testParseSubCommands() { List parsed = commandLine.parse("--git-dir=/home/rpopma/picocli status -sbuno".split(" ")); assertEquals("command count", 2, parsed.size()); - assertEquals(Demo.Git.class, parsed.get(0).getCommand().getClass()); - assertEquals(Demo.GitStatus.class, parsed.get(1).getCommand().getClass()); + assertEquals(Demo.Git.class, ((Object) parsed.get(0).getCommand()).getClass()); + assertEquals(Demo.GitStatus.class, ((Object) parsed.get(1).getCommand()).getClass()); Demo.Git git = (Demo.Git) parsed.get(0).getCommand(); assertEquals(new File("/home/rpopma/picocli"), git.gitDir); @@ -1851,8 +1851,8 @@ public void testCommandListReturnsRegisteredCommands() { Map commandMap = commandLine.getSubcommands(); assertEquals(2, commandMap.size()); - assertTrue("cmd1", commandMap.get("cmd1").getCommand() instanceof Command1); - assertTrue("cmd2", commandMap.get("cmd2").getCommand() instanceof Command2); + assertTrue("cmd1", ((Object) commandMap.get("cmd1").getCommand()) instanceof Command1); + assertTrue("cmd2", ((Object) commandMap.get("cmd2").getCommand()) instanceof Command2); } @Test @@ -1866,8 +1866,8 @@ public void testCommandListReturnsCaseInsensitiveRegisteredCommands() { Map commandMap = commandLine.getSubcommands(); assertEquals(2, commandMap.size()); - assertTrue("cmd1", commandMap.get("CMD1").getCommand() instanceof Command1); - assertTrue("cmd2", commandMap.get("CMD2").getCommand() instanceof Command2); + assertTrue("cmd1", ((Object) commandMap.get("CMD1").getCommand()) instanceof Command1); + assertTrue("cmd2", ((Object) commandMap.get("CMD2").getCommand()) instanceof Command2); } @Test(expected = InitializationException.class) @@ -2614,7 +2614,7 @@ class Sub2 { addSubcommand("sub2", new Sub2()). parseArgs("sub1 -x abc".split(" ")); } catch (ParameterException ex) { - assertTrue(ex.getCommandLine().getCommand() instanceof Top); + assertTrue(((Object) ex.getCommandLine().getCommand()) instanceof Top); } try { new CommandLine(new Top()). @@ -2622,7 +2622,7 @@ class Sub2 { addSubcommand("sub2", new Sub2()). parseArgs("-o OPT sub1 -wrong ABC".split(" ")); } catch (ParameterException ex) { - assertTrue(ex.getCommandLine().getCommand() instanceof Sub1); + assertTrue(((Object) ex.getCommandLine().getCommand()) instanceof Sub1); } try { new CommandLine(new Top()). @@ -2630,7 +2630,7 @@ class Sub2 { addSubcommand("sub2", new Sub2()). parseArgs("-o OPT sub2 -wrong ABC".split(" ")); } catch (ParameterException ex) { - assertTrue(ex.getCommandLine().getCommand() instanceof Sub2); + assertTrue(((Object) ex.getCommandLine().getCommand()) instanceof Sub2); } List parsed = new CommandLine(new Top()). addSubcommand("sub1", new Sub1()). @@ -2655,17 +2655,17 @@ class Top { try { new CommandLine(new Top()).parseArgs("sub207A -x abc".split(" ")); } catch (ParameterException ex) { - assertTrue(ex.getCommandLine().getCommand() instanceof Top); + assertTrue(((Object) ex.getCommandLine().getCommand()) instanceof Top); } try { new CommandLine(new Top()).parseArgs("-o OPT sub207A -wrong ABC".split(" ")); } catch (ParameterException ex) { - assertTrue(ex.getCommandLine().getCommand() instanceof Sub207A); + assertTrue(((Object) ex.getCommandLine().getCommand()) instanceof Sub207A); } try { new CommandLine(new Top()).parseArgs("-o OPT sub207B -wrong ABC".split(" ")); } catch (ParameterException ex) { - assertTrue(ex.getCommandLine().getCommand() instanceof Sub207B); + assertTrue(((Object) ex.getCommandLine().getCommand()) instanceof Sub207B); } List parsed = new CommandLine(new Top()). parse("-o OPT sub207A -x ABC".split(" ")); diff --git a/src/test/java/picocli/CommandMethodTest.java b/src/test/java/picocli/CommandMethodTest.java index f3eaa16e0..60548e1cc 100644 --- a/src/test/java/picocli/CommandMethodTest.java +++ b/src/test/java/picocli/CommandMethodTest.java @@ -36,6 +36,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -100,11 +101,11 @@ public void testAnnotateMethod_noArg() throws Exception { Method m = CommandLine.getCommandMethods(MethodApp.class, "run0").get(0); CommandLine cmd1 = new CommandLine(m); assertEquals("run-0", cmd1.getCommandName()); - assertEquals(Arrays.asList(), cmd1.getCommandSpec().args()); + assertEquals(Collections.emptyList(), cmd1.getCommandSpec().args()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); cmd1.parseWithHandler(((IParseResultHandler) null), new PrintStream(baos), new String[]{"--y"}); - assertEquals(Arrays.asList("--y"), cmd1.getUnmatchedArguments()); + assertEquals(Collections.singletonList("--y"), cmd1.getUnmatchedArguments()); // test execute Object ret = CommandLine.invoke(m.getName(), MethodApp.class, new PrintStream(new ByteArrayOutputStream())); diff --git a/src/test/java/picocli/Demo.java b/src/test/java/picocli/Demo.java index db5f16603..189ad64ed 100644 --- a/src/test/java/picocli/Demo.java +++ b/src/test/java/picocli/Demo.java @@ -55,7 +55,7 @@ //descriptionHeading = "@|bold %nDescription|@:%n", description = { "", - "Demonstrates picocli subcommands parsing and usage help.", }, + "Demonstrates picocli subcommands parsing and usage help." }, versionProvider = Demo.ManifestVersionProvider.class, optionListHeading = "@|bold %nOptions|@:%n", footer = { @@ -430,8 +430,8 @@ public static void testParseSubCommands() { List parsed = commandLine.parse(args); assert parsed.size() == 2 : "found 2 commands"; - assert parsed.get(0).getCommand().getClass() == Git.class; - assert parsed.get(1).getCommand().getClass() == GitStatus.class; + assert ((Object) parsed.get(0).getCommand()).getClass() == Git.class; + assert ((Object) parsed.get(1).getCommand()).getClass() == GitStatus.class; Git git = (Git) parsed.get(0).getCommand(); assert git.gitDir.equals(new File("/home/rpopma/picocli")); diff --git a/src/test/java/picocli/ExecuteLegacyTest.java b/src/test/java/picocli/ExecuteLegacyTest.java index 2bbace30f..b29b19398 100644 --- a/src/test/java/picocli/ExecuteLegacyTest.java +++ b/src/test/java/picocli/ExecuteLegacyTest.java @@ -412,6 +412,7 @@ public void run() { "
"), systemErrRule.getLog()); } + @SuppressWarnings("deprecation") private DefaultExceptionHandler> defaultExceptionHandler() { return new DefaultExceptionHandler>(); } @@ -431,6 +432,7 @@ public void run() { assertEquals(25, handler.exitCode); } + @SuppressWarnings("deprecation") static class CustomExceptionHandler extends DefaultExceptionHandler { int exitCode; @@ -454,6 +456,8 @@ public void run() { "Usage:
%n"), systemErrRule.getLog()); assertEquals(25, handler.exitCode); } + + @SuppressWarnings("deprecation") static class CustomNoThrowExceptionHandler extends DefaultExceptionHandler { int exitCode; ExecutionException caught; @@ -591,7 +595,7 @@ public void testCall1DefaultExceptionHandlerRethrows() { CommandLine.call(new MyCallable(), "-x abc"); fail("Expected exception"); } catch (ExecutionException ex) { - String cmd = ex.getCommandLine().getCommand().toString(); + String cmd = ((Object) ex.getCommandLine().getCommand()).toString(); String msg = "Error while calling command (" + cmd + "): java.lang.IllegalStateException: this is a test"; assertEquals(msg, ex.getMessage()); } diff --git a/src/test/java/picocli/HelpSubCommandTest.java b/src/test/java/picocli/HelpSubCommandTest.java index 45e70fa45..667887b4e 100644 --- a/src/test/java/picocli/HelpSubCommandTest.java +++ b/src/test/java/picocli/HelpSubCommandTest.java @@ -111,7 +111,7 @@ public void testCommandAliasAnnotationUsageHelp() { new Help.ColorScheme.Builder(CommandLine.Help.defaultColorScheme(Help.Ansi.ON)) .commands(Help.Ansi.Style.underline) .build()); // add underline - + String expected = Help.Ansi.ON.new Text(String.format("" + "Usage: @|bold,underline top|@ [COMMAND]%n" + "top level command%n" + @@ -139,8 +139,8 @@ public void testCommandAliasAnnotationSubcommandUsageHelp() { assertEquals(expected, baos.toString()); } - @SuppressWarnings("deprecation") @Command(name = "customHelp", helpCommand = true) + @SuppressWarnings("deprecation") static class LegacyCustomHelpCommand implements IHelpCommandInitializable, Runnable { private CommandLine helpCommandLine; private Help.Ansi ansi; diff --git a/src/test/java/picocli/HelpTest.java b/src/test/java/picocli/HelpTest.java index a70d7ae2e..0e62bb5bc 100644 --- a/src/test/java/picocli/HelpTest.java +++ b/src/test/java/picocli/HelpTest.java @@ -1771,7 +1771,7 @@ public void testDefaultLayout_addsEachRowToTable() { final int[] count = {0}; TextTable tt = TextTable.forDefaultColumns(Help.Ansi.OFF, UsageMessageSpec.DEFAULT_USAGE_WIDTH); tt = new TextTable(Help.Ansi.OFF, tt.columns()) { - @Override public void addRowValues(Text[] columnValues) { + @Override public void addRowValues(Text... columnValues) { assertArrayEquals(values[count[0]], columnValues); count[0]++; } @@ -2035,7 +2035,7 @@ public void testCustomSynopsis() { @Command(customSynopsis = { " --number=NUMBER --other-option=", " --more=OTHER --and-other-option=", - " --number=NUMBER --and-other-option=", + " --number=NUMBER --and-other-option=" }) class App {@Option(names = "--ignored") boolean ignored;} Help help = new Help(new App(), Help.Ansi.OFF); @@ -3064,8 +3064,8 @@ static class Top { public void test244SubcommandsNotParsed() { List list = new CommandLine(new Top()).parse("-h", "sub"); assertEquals(2, list.size()); - assertTrue(list.get(0).getCommand() instanceof Top); - assertTrue(list.get(1).getCommand() instanceof Sub); + assertTrue(((Object) list.get(0).getCommand()) instanceof Top); + assertTrue(((Object) list.get(1).getCommand()) instanceof Sub); assertTrue(((Top) list.get(0).getCommand()).isUsageHelpRequested); } diff --git a/src/test/java/picocli/MixinTest.java b/src/test/java/picocli/MixinTest.java index 3b0c7a799..677f6f4ac 100644 --- a/src/test/java/picocli/MixinTest.java +++ b/src/test/java/picocli/MixinTest.java @@ -772,7 +772,7 @@ class Receiver { assertEquals(1, commandSpec.subcommands().size()); CommandLine subcommandLine = commandSpec.subcommands().get("mixinsub"); assertSame(subcommandLine, commandLine.getSubcommands().get("mixinsub")); - assertTrue(subcommandLine.getCommand() instanceof MixedInSubCommand); + assertTrue(((Object) subcommandLine.getCommand()) instanceof MixedInSubCommand); } @Test diff --git a/src/test/java/picocli/OptionMethodSpecTest.java b/src/test/java/picocli/OptionMethodSpecTest.java index d3011b991..1865bb9fb 100644 --- a/src/test/java/picocli/OptionMethodSpecTest.java +++ b/src/test/java/picocli/OptionMethodSpecTest.java @@ -77,7 +77,7 @@ interface PrimitivesWithDefault { @Test public void testInterfaceIsInstantiated() { CommandLine cmd = new CommandLine(Primitives.class); - assertTrue(cmd.getCommand() instanceof Primitives); + assertTrue(((Object) cmd.getCommand()) instanceof Primitives); } @Test diff --git a/src/test/java/picocli/SubcommandTests.java b/src/test/java/picocli/SubcommandTests.java index 0ecd33770..85710b9d3 100644 --- a/src/test/java/picocli/SubcommandTests.java +++ b/src/test/java/picocli/SubcommandTests.java @@ -343,8 +343,8 @@ public void testCommandListReturnsOnlyCommandsRegisteredOnInstance() { Map commandMap = commandLine.getSubcommands(); assertEquals(2, commandMap.size()); - assertTrue("cmd1", commandMap.get("cmd1").getCommand() instanceof ChildCommand1); - assertTrue("cmd2", commandMap.get("cmd2").getCommand() instanceof ChildCommand2); + assertTrue("cmd1", ((Object) commandMap.get("cmd1").getCommand()) instanceof ChildCommand1); + assertTrue("cmd2", ((Object) commandMap.get("cmd2").getCommand()) instanceof ChildCommand2); } @Test @@ -354,18 +354,18 @@ public void testCommandListReturnsAliases() { Map commandMap = commandLine.getSubcommands(); assertEquals(6, commandMap.size()); assertEquals(setOf("cmd1", "cmd1alias1", "cmd1alias2", "cmd2", "cmd2alias1", "cmd2alias2"), commandMap.keySet()); - assertTrue("cmd1", commandMap.get("cmd1").getCommand() instanceof ChildCommand1); + assertTrue("cmd1", ((Object) commandMap.get("cmd1").getCommand()) instanceof ChildCommand1); assertSame(commandMap.get("cmd1"), commandMap.get("cmd1alias1")); assertSame(commandMap.get("cmd1"), commandMap.get("cmd1alias2")); - assertTrue("cmd2", commandMap.get("cmd2").getCommand() instanceof ChildCommand2); + assertTrue("cmd2", ((Object) commandMap.get("cmd2").getCommand()) instanceof ChildCommand2); assertSame(commandMap.get("cmd2"), commandMap.get("cmd2alias1")); assertSame(commandMap.get("cmd2"), commandMap.get("cmd2alias2")); CommandLine cmd2 = commandMap.get("cmd2"); Map subMap = cmd2.getSubcommands(); - assertTrue("cmd2", subMap.get("sub21").getCommand() instanceof GrandChild2Command1); + assertTrue("cmd2", ((Object) subMap.get("sub21").getCommand()) instanceof GrandChild2Command1); assertSame(subMap.get("sub21"), subMap.get("sub21alias1")); assertSame(subMap.get("sub21"), subMap.get("sub21alias2")); } @@ -627,11 +627,11 @@ public void testDeclarativelyAddSubcommands() { assertEquals(1, main.getSubcommands().size()); CommandLine sub1 = main.getSubcommands().get("sub1"); - assertEquals(Sub1_testDeclarativelyAddSubcommands.class, sub1.getCommand().getClass()); + assertEquals(Sub1_testDeclarativelyAddSubcommands.class, ((Object) sub1.getCommand()).getClass()); assertEquals(1, sub1.getSubcommands().size()); CommandLine subsub1 = sub1.getSubcommands().get("subsub1"); - assertEquals(SubSub1_testDeclarativelyAddSubcommands.class, subsub1.getCommand().getClass()); + assertEquals(SubSub1_testDeclarativelyAddSubcommands.class, ((Object) subsub1.getCommand()).getClass()); } @Test public void testGetParentForDeclarativelyAddedSubcommands() { @@ -640,12 +640,12 @@ public void testGetParentForDeclarativelyAddedSubcommands() { CommandLine sub1 = main.getSubcommands().get("sub1"); assertSame(main, sub1.getParent()); - assertEquals(Sub1_testDeclarativelyAddSubcommands.class, sub1.getCommand().getClass()); + assertEquals(Sub1_testDeclarativelyAddSubcommands.class, ((Object) sub1.getCommand()).getClass()); assertEquals(1, sub1.getSubcommands().size()); CommandLine subsub1 = sub1.getSubcommands().get("subsub1"); assertSame(sub1, subsub1.getParent()); - assertEquals(SubSub1_testDeclarativelyAddSubcommands.class, subsub1.getCommand().getClass()); + assertEquals(SubSub1_testDeclarativelyAddSubcommands.class, ((Object) subsub1.getCommand()).getClass()); } @Test public void testGetParentForProgrammaticallyAddedSubcommands() { @@ -668,7 +668,7 @@ class Top {} public void testDeclarativelyAddSubcommandsSucceedsWithDefaultConstructorForDefaultFactory() { @Command(subcommands = {SubSub1_testDeclarativelyAddSubcommands.class}) class MainCommand {} CommandLine cmdLine = new CommandLine(new MainCommand()); - assertEquals(SubSub1_testDeclarativelyAddSubcommands.class.getName(), cmdLine.getSubcommands().get("subsub1").getCommand().getClass().getName()); + assertEquals(SubSub1_testDeclarativelyAddSubcommands.class.getName(), ((Object) cmdLine.getSubcommands().get("subsub1").getCommand()).getClass().getName()); } @Test public void testDeclarativelyAddSubcommandsFailsWithoutNoArgConstructor() { @@ -691,7 +691,7 @@ public void testDeclarativelyAddSubcommandsSucceedsWithDefaultConstructor() { @Command(name = "sub1") class ABCD {} @Command(subcommands = {ABCD.class}) class MainCommand {} CommandLine cmdLine = new CommandLine(new MainCommand(), new InnerClassFactory(this)); - assertEquals("picocli.SubcommandTests$1ABCD", cmdLine.getSubcommands().get("sub1").getCommand().getClass().getName()); + assertEquals("picocli.SubcommandTests$1ABCD", ((Object) cmdLine.getSubcommands().get("sub1").getCommand()).getClass().getName()); } @Test public void testDeclarativelyAddSubcommandsFailsWithoutAnnotation() { @@ -746,7 +746,7 @@ class TopLevel { public boolean equals(Object o) {return getClass().equals(o.get commandLine.registerConverter(CustomType.class, new CustomType(null)); List parsed = commandLine.parse("main", "cmd1", "sub12", "-e", "TXT"); assertEquals(4, parsed.size()); - assertEquals(TopLevel.class, parsed.get(0).getCommand().getClass()); + assertEquals(TopLevel.class, ((Object) parsed.get(0).getCommand()).getClass()); assertFalse(((MainCommand) parsed.get(1).getCommand()).a); assertFalse(((ChildCommand1) parsed.get(2).getCommand()).b); assertEquals("TXT", ((GrandChild1Command2) parsed.get(3).getCommand()).e.val); @@ -2273,7 +2273,7 @@ public void testSetHelpSectionKeys_AfterSubcommandsAdded() { class TopLevel {} CommandLine commandLine = new CommandLine(new TopLevel()); commandLine.addSubcommand("main", createNestedCommand()); - + final List DEFAULT_LIST = Arrays.asList("headerHeading", "header", "synopsisHeading", "synopsis", "descriptionHeading", "description", "parameterListHeading", "atFileParameterList", "parameterList", "optionListHeading", "optionList", "endOfOptionsList", "commandListHeading", "commandList", "exitCodeListHeading", "exitCodeList", "footerHeading", "footer"); @@ -2383,7 +2383,7 @@ public void run() { public void testMandatorySubcommand625() { int exitCode = new CommandLine(new MandatorySubcommand625.Top()).execute(); assertEquals(CommandLine.ExitCode.USAGE, exitCode); - + String expected = String.format("" + "Missing required subcommand%n" + "Usage: top COMMAND%n" + @@ -2518,7 +2518,7 @@ public Foo() { } } - + @Command(name = "playpico", description = "play picocli", mixinStandardHelpOptions = true, subcommands = Foo.class ) @@ -2526,7 +2526,7 @@ static class Launcher implements Runnable { @Spec CommandSpec spec; @Option(names = "-x", defaultValue = "123", description = "X; default=${DEFAULT-VALUE}") int x; - + public static void main(String[] args) { new CommandLine(new Launcher()) .execute(args); @@ -2548,7 +2548,7 @@ public void testPostponeInstantiation_Issue690() { .setOut(new PrintWriter(out)) .execute(); assertEquals("", err.toString()); - + assertEquals(String.format("" + "Usage: playpico [-hV] [-x=] [COMMAND]%n" + "play picocli%n" + diff --git a/src/test/java/picocli/TypeConversionTest.java b/src/test/java/picocli/TypeConversionTest.java index a86e84e84..a61c30a00 100644 --- a/src/test/java/picocli/TypeConversionTest.java +++ b/src/test/java/picocli/TypeConversionTest.java @@ -601,7 +601,7 @@ class GlobConverter implements ITypeConverter { String[] args = {"a*glob*pattern"}; List parsed = commandLine.parse(args); assertEquals("not empty", 1, parsed.size()); - assertTrue(parsed.get(0).getCommand() instanceof App); + assertTrue(((Object) parsed.get(0).getCommand()) instanceof App); App app = (App) parsed.get(0).getCommand(); assertEquals(args[0], app.globField.glob); } @@ -625,7 +625,7 @@ class App { String[] args = {"a*glob*pattern"}; List parsed = commandLine.parse(args); assertEquals("not empty", 1, parsed.size()); - assertTrue(parsed.get(0).getCommand() instanceof App); + assertTrue(((Object) parsed.get(0).getCommand()) instanceof App); App app = (App) parsed.get(0).getCommand(); assertEquals(args[0], app.globField.glob); }