Skip to content

Commit

Permalink
[BUILD] Java 5 compatibility: work around Java 5 compiler limitations
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Dec 8, 2021
1 parent 907a604 commit e073b0d
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 45 deletions.
24 changes: 12 additions & 12 deletions src/test/java/picocli/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1828,8 +1828,8 @@ public void testParseSubCommands() {
List<CommandLine> 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);
Expand All @@ -1851,8 +1851,8 @@ public void testCommandListReturnsRegisteredCommands() {

Map<String, CommandLine> 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
Expand All @@ -1866,8 +1866,8 @@ public void testCommandListReturnsCaseInsensitiveRegisteredCommands() {

Map<String, CommandLine> 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)
Expand Down Expand Up @@ -2614,23 +2614,23 @@ 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()).
addSubcommand("sub1", new Sub1()).
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()).
addSubcommand("sub1", new Sub1()).
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<CommandLine> parsed = new CommandLine(new Top()).
addSubcommand("sub1", new Sub1()).
Expand All @@ -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<CommandLine> parsed = new CommandLine(new Top()).
parse("-o OPT sub207A -x ABC".split(" "));
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/picocli/CommandMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/picocli/Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -430,8 +430,8 @@ public static void testParseSubCommands() {
List<CommandLine> 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"));
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/picocli/ExecuteLegacyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ public void run() {
"<main command>"), systemErrRule.getLog());
}

@SuppressWarnings("deprecation")
private DefaultExceptionHandler<List<Object>> defaultExceptionHandler() {
return new DefaultExceptionHandler<List<Object>>();
}
Expand All @@ -431,6 +432,7 @@ public void run() {
assertEquals(25, handler.exitCode);
}

@SuppressWarnings("deprecation")
static class CustomExceptionHandler<R> extends DefaultExceptionHandler<R> {
int exitCode;

Expand All @@ -454,6 +456,8 @@ public void run() {
"Usage: <main class>%n"), systemErrRule.getLog());
assertEquals(25, handler.exitCode);
}

@SuppressWarnings("deprecation")
static class CustomNoThrowExceptionHandler<R> extends DefaultExceptionHandler<R> {
int exitCode;
ExecutionException caught;
Expand Down Expand Up @@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/picocli/HelpSubCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/picocli/HelpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]++;
}
Expand Down Expand Up @@ -2035,7 +2035,7 @@ public void testCustomSynopsis() {
@Command(customSynopsis = {
"<the-app> --number=NUMBER --other-option=<aargh>",
" --more=OTHER --and-other-option=<aargh>",
"<the-app> --number=NUMBER --and-other-option=<aargh>",
"<the-app> --number=NUMBER --and-other-option=<aargh>"
})
class App {@Option(names = "--ignored") boolean ignored;}
Help help = new Help(new App(), Help.Ansi.OFF);
Expand Down Expand Up @@ -3064,8 +3064,8 @@ static class Top {
public void test244SubcommandsNotParsed() {
List<CommandLine> 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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/picocli/MixinTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/picocli/OptionMethodSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 17 additions & 17 deletions src/test/java/picocli/SubcommandTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ public void testCommandListReturnsOnlyCommandsRegisteredOnInstance() {

Map<String, CommandLine> 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
Expand All @@ -354,18 +354,18 @@ public void testCommandListReturnsAliases() {
Map<String, CommandLine> 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<String, CommandLine> 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"));
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -746,7 +746,7 @@ class TopLevel { public boolean equals(Object o) {return getClass().equals(o.get
commandLine.registerConverter(CustomType.class, new CustomType(null));
List<CommandLine> 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);
Expand Down Expand Up @@ -2273,7 +2273,7 @@ public void testSetHelpSectionKeys_AfterSubcommandsAdded() {
class TopLevel {}
CommandLine commandLine = new CommandLine(new TopLevel());
commandLine.addSubcommand("main", createNestedCommand());

final List<String> DEFAULT_LIST = Arrays.asList("headerHeading", "header", "synopsisHeading", "synopsis",
"descriptionHeading", "description", "parameterListHeading", "atFileParameterList", "parameterList", "optionListHeading",
"optionList", "endOfOptionsList", "commandListHeading", "commandList", "exitCodeListHeading", "exitCodeList", "footerHeading", "footer");
Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -2518,15 +2518,15 @@ public Foo() {
}

}

@Command(name = "playpico",
description = "play picocli", mixinStandardHelpOptions = true,
subcommands = Foo.class )
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);
Expand All @@ -2548,7 +2548,7 @@ public void testPostponeInstantiation_Issue690() {
.setOut(new PrintWriter(out))
.execute();
assertEquals("", err.toString());

assertEquals(String.format("" +
"Usage: playpico [-hV] [-x=<x>] [COMMAND]%n" +
"play picocli%n" +
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/picocli/TypeConversionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ class GlobConverter implements ITypeConverter<Glob> {
String[] args = {"a*glob*pattern"};
List<CommandLine> 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);
}
Expand All @@ -625,7 +625,7 @@ class App {
String[] args = {"a*glob*pattern"};
List<CommandLine> 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);
}
Expand Down

0 comments on commit e073b0d

Please sign in to comment.