VMCommandLine { @SuppressWarnings({"checkstyle:cyclomaticcomplexity", "npathcomplexity"}) public static void splitOptionsAndParameters(String... optionDefinitions) { if (analyzed) { return; } final List<String> params = new ArrayList<>(); final SortedMap<String, List<Object>> options = new TreeMap<>(); String opt; final Map<String, OptionType> defs = new TreeMap<>(); for (final String def : optionDefinitions) { if (def.endsWith("!")) { opt = def.substring(0, def.length() - 1); defs.put(opt, OptionType.FLAG); registerOptionValue(options, opt, Boolean.FALSE, OptionType.FLAG); } else if (def.endsWith("+")) { opt = def.substring(0, def.length() - 1); defs.put(opt, OptionType.AUTO_INCREMENTED); registerOptionValue(options, opt, Long.valueOf(0), OptionType.AUTO_INCREMENTED); } else if (def.endsWith("=b")) { opt = def.substring(0, def.length() - 2); defs.put(opt, OptionType.MANDATORY_BOOLEAN); } else if (def.endsWith(":b")) { opt = def.substring(0, def.length() - 2); defs.put(opt, OptionType.OPTIONAL_BOOLEAN); } else if (def.endsWith("=f")) { opt = def.substring(0, def.length() - 2); defs.put(opt, OptionType.MANDATORY_FLOAT); } else if (def.endsWith(":f")) { opt = def.substring(0, def.length() - 2); defs.put(opt, OptionType.OPTIONAL_FLOAT); } else if (def.endsWith("=i")) { opt = def.substring(0, def.length() - 2); defs.put(opt, OptionType.MANDATORY_INTEGER); } else if (def.endsWith(":i")) { opt = def.substring(0, def.length() - 2); defs.put(opt, OptionType.OPTIONAL_INTEGER); } else if (def.endsWith("=s")) { opt = def.substring(0, def.length() - 2); defs.put(opt, OptionType.MANDATORY_STRING); } else if (def.endsWith(":s")) { opt = def.substring(0, def.length() - 2); defs.put(opt, OptionType.OPTIONAL_STRING); } else { defs.put(def, OptionType.SIMPLE); } } int idx; String base; String nbase; String val; OptionType type; OptionType waitingValue = null; String valueOptionName = null; boolean allParameters = false; boolean success; for (final String param : commandLineParameters) { if (allParameters) { params.add(param); continue; } if (waitingValue != null && waitingValue.isMandatory()) { success = registerOptionValue(options, valueOptionName, param, waitingValue); waitingValue = null; valueOptionName = null; if (success) { continue; } } if ("--".equals(param)) { if (waitingValue != null) { registerOptionValue(options, valueOptionName, null, waitingValue); waitingValue = null; valueOptionName = null; } allParameters = true; continue; } else if ((File.separatorChar != '/') && (param.startsWith("/"))) { opt = param.substring(1); } else if (param.startsWith("--")) { opt = param.substring(2); } else if (param.startsWith("-")) { opt = param.substring(1); } else if (waitingValue != null) { success = registerOptionValue(options, valueOptionName, param, waitingValue); waitingValue = null; valueOptionName = null; if (!success) { params.add(param); } continue; } else { params.add(param); continue; } if (waitingValue != null) { success = registerOptionValue(options, valueOptionName, param, waitingValue); waitingValue = null; valueOptionName = null; if (success) { continue; } } idx = opt.indexOf('='); if (idx > 0) { base = opt.substring(0, idx); val = opt.substring(idx + 1); } else { base = opt; val = null; } nbase = null; type = defs.get(base); if (type == null && base.toLowerCase().startsWith("no")) { nbase = base.substring(2); type = defs.get(nbase); } if (type != null) { switch (type) { case FLAG: if (nbase == null) { registerOptionValue(options, base, Boolean.TRUE, type); } else { registerOptionValue(options, nbase, Boolean.FALSE, type); } break; case MANDATORY_FLOAT: case MANDATORY_BOOLEAN: case MANDATORY_INTEGER: case MANDATORY_STRING: case OPTIONAL_FLOAT: case OPTIONAL_BOOLEAN: case OPTIONAL_INTEGER: case OPTIONAL_STRING: if (val != null) { registerOptionValue(options, base, val, type); } else { waitingValue = type; valueOptionName = base; } break; default: registerOptionValue(options, base, val, type); } } else { registerOptionValue(options, base, val, OptionType.SIMPLE); } } if (waitingValue != null && waitingValue.isMandatory()) { throw new IllegalStateException(Locale.getString("E2", valueOptionName)); } commandLineParameters = new String[params.size()]; params.toArray(commandLineParameters); params.clear(); commandLineOptions = options; analyzed = true; } VMCommandLine(Class<?> classToLaunch, String... parameters);  VMCommandLine(String classToLaunch, String... parameters);  VMCommandLine(Class<?> classToLaunch, String[] optionDefinitions, String... parameters);  VMCommandLine(String classToLaunch, String[] optionDefinitions, String... parameters);  VMCommandLine(); @Pure static String getExecutableFilename(String name); @Pure static String getVMBinary(); @Inline(value = "VMCommandLine.launchVMWithClassPath(($1).getCanonicalName(), ($2), ($3))", imported = {VMCommandLine.class}, statementExpression = true) static Process launchVMWithClassPath(Class<?> classToLaunch, String classpath,
			String... additionalParams); @SuppressWarnings({"checkstyle:magicnumber"}) static Process launchVMWithClassPath(String classToLaunch, String classpath,
			String... additionalParams); @Inline(value = "VMCommandLine.launchVMWithClassPath(($1).getCanonicalName(), ($2), ($3))", imported = {VMCommandLine.class}, statementExpression = true) static Process launchVMWithClassPath(Class<?> classToLaunch, File[] classpath,
			String... additionalParams); static Process launchVMWithClassPath(String classToLaunch, File[] classpath,
			String... additionalParams); @SuppressWarnings("checkstyle:magicnumber") static Process launchVMWithJar(File jarFile, String... additionalParams); @Inline(value = "VMCommandLine.launchVM(($1).getCanonicalName(), ($2))", imported = {VMCommandLine.class}, statementExpression = true) static Process launchVM(Class<?> classToLaunch, String... additionalParams); @Inline(value = "VMCommandLine.launchVMWithClassPath(($1), System.getProperty(\"java.class.path\"), ($2))", imported = {VMCommandLine.class}, statementExpression = true) static Process launchVM(String classToLaunch, String... additionalParams); @Inline(value = "VMCommandLine.saveVMParameters((($1) != null) ? ($1).getCanonicalName() : null, ($2))", imported = {VMCommandLine.class}, statementExpression = true) static void saveVMParameters(Class<?> classToLaunch, String... parameters); static void saveVMParameters(String classToLaunch, String... parameters); @Inline(value = "VMCommandLine.saveVMParametersIfNotSet(($1).getCanonicalName(), ($2))", imported = {VMCommandLine.class}, statementExpression = true) static void saveVMParametersIfNotSet(Class<?> classToLaunch, String... parameters); static void saveVMParametersIfNotSet(String classToLaunch, String... parameters); static Process relaunchVM(); @Pure @SuppressWarnings({"checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity"}) static String[] getAllCommandLineParameters(); @Pure static String[] getCommandLineParameters(); static String shiftCommandLineParameters(); @Pure static Map<String, List<Object>> getCommandLineOptions(); @Pure static List<Object> getCommandLineOption(String name); @Pure static boolean hasCommandLineOption(String name); @SuppressWarnings({"checkstyle:cyclomaticcomplexity", "npathcomplexity"}) static void splitOptionsAndParameters(String... optionDefinitions); @Pure @SuppressWarnings("static-method") boolean hasOption(String optionLabel); @Pure @SuppressWarnings("static-method") Object getFirstOptionValue(String optionLabel); @Pure @SuppressWarnings("static-method") List<Object> getOptionValues(String optionLabel); @Pure @SuppressWarnings("static-method") String[] getParameters(); @SuppressWarnings("static-method") String shiftParameters(); @Pure @SuppressWarnings("static-method") int getParameterCount(); @Pure @SuppressWarnings("static-method") String getParameterAt(int index); @Pure @SuppressWarnings("static-method") boolean isParameterExists(int index);  }
@Test public void splitOptionsAndParameters() { VMCommandLine.saveVMParameters(VMCommandLineTest.class, commandLine); VMCommandLine.splitOptionsAndParameters(optionDefinitions); Map<String,List<Object>> options = VMCommandLine.getCommandLineOptions(); String[] parameters = VMCommandLine.getCommandLineParameters(); List<Object> values; assertNotNull(options); assertEquals(5, options.size()); assertTrue(options.containsKey("D")); values = options.get("D"); assertNotNull(values); assertEquals(1, values.size()); assertEquals(true, values.get(0)); assertTrue(options.containsKey("v")); values = options.get("v"); assertNotNull(values); assertEquals(1, values.size()); assertEquals((long)3, values.get(0)); assertTrue(options.containsKey("F")); values = options.get("F"); assertNotNull(values); assertEquals(2, values.size()); assertEquals(0., values.get(0)); assertEquals(123., values.get(1)); assertTrue(options.containsKey("b")); values = options.get("b"); assertNotNull(values); assertEquals(1, values.size()); assertEquals(false, values.get(0)); assertTrue(options.containsKey("S")); values = options.get("S"); assertNotNull(values); assertEquals(1, values.size()); assertEquals("-b", values.get(0)); values = options.get("nob"); assertNull(values); assertNotNull(parameters); assertEquals(4, parameters.length); assertEquals("clean", parameters[0]); assertEquals("package", parameters[1]); assertEquals("installters", parameters[2]); assertEquals("-v", parameters[3]); }