langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java
changeset 41637 7b24b4c32ee6
parent 41252 058d83c9b1c7
child 41938 8e66bf10fcec
equal deleted inserted replaced
41636:086a3c7a6b56 41637:7b24b4c32ee6
    35 import java.util.Collections;
    35 import java.util.Collections;
    36 import java.util.Comparator;
    36 import java.util.Comparator;
    37 import java.util.EnumSet;
    37 import java.util.EnumSet;
    38 import java.util.Iterator;
    38 import java.util.Iterator;
    39 import java.util.LinkedHashMap;
    39 import java.util.LinkedHashMap;
       
    40 import java.util.LinkedHashSet;
    40 import java.util.Locale;
    41 import java.util.Locale;
    41 import java.util.Map;
    42 import java.util.Map;
    42 import java.util.ServiceLoader;
    43 import java.util.ServiceLoader;
    43 import java.util.Set;
    44 import java.util.Set;
    44 import java.util.TreeSet;
    45 import java.util.TreeSet;
   109             log.printRawLines(WriterKind.STDOUT,
   110             log.printRawLines(WriterKind.STDOUT,
   110                               String.format(LINT_KEY_FORMAT,
   111                               String.format(LINT_KEY_FORMAT,
   111                                             "all",
   112                                             "all",
   112                                             log.localize(PrefixKind.JAVAC, "opt.Xlint.all")));
   113                                             log.localize(PrefixKind.JAVAC, "opt.Xlint.all")));
   113             for (LintCategory lc : LintCategory.values()) {
   114             for (LintCategory lc : LintCategory.values()) {
   114                 if (lc.hidden) continue;
       
   115                 log.printRawLines(WriterKind.STDOUT,
   115                 log.printRawLines(WriterKind.STDOUT,
   116                                   String.format(LINT_KEY_FORMAT,
   116                                   String.format(LINT_KEY_FORMAT,
   117                                                 lc.option,
   117                                                 lc.option,
   118                                                 log.localize(PrefixKind.JAVAC,
   118                                                 log.localize(PrefixKind.JAVAC,
   119                                                              "opt.Xlint.desc." + lc.option)));
   119                                                              "opt.Xlint.desc." + lc.option)));
   799     private final ArgKind argKind;
   799     private final ArgKind argKind;
   800 
   800 
   801     /** The kind of choices for this option, if any. */
   801     /** The kind of choices for this option, if any. */
   802     private final ChoiceKind choiceKind;
   802     private final ChoiceKind choiceKind;
   803 
   803 
   804     /** The choices for this option, if any, and whether or not the choices are hidden. */
   804     /** The choices for this option, if any. */
   805     private final Map<String,Boolean> choices;
   805     private final Set<String> choices;
   806 
   806 
   807     /**
   807     /**
   808      * Looks up the first option matching the given argument in the full set of options.
   808      * Looks up the first option matching the given argument in the full set of options.
   809      * @param arg the argument to be matches
   809      * @param arg the argument to be matches
   810      * @return the first option that matches, or null if none.
   810      * @return the first option that matches, or null if none.
   813         return lookup(arg, EnumSet.allOf(Option.class));
   813         return lookup(arg, EnumSet.allOf(Option.class));
   814     }
   814     }
   815 
   815 
   816     /**
   816     /**
   817      * Looks up the first option matching the given argument within a set of options.
   817      * Looks up the first option matching the given argument within a set of options.
   818      * @param arg the argument to be matches
   818      * @param arg the argument to be matched
       
   819      * @param options the set of possible options
   819      * @return the first option that matches, or null if none.
   820      * @return the first option that matches, or null if none.
   820      */
   821      */
   821     public static Option lookup(String arg, Set<Option> options) {
   822     public static Option lookup(String arg, Set<Option> options) {
   822         for (Option option: options) {
   823         for (Option option: options) {
   823             if (option.matches(arg))
   824             if (option.matches(arg))
   865             OptionKind kind, OptionGroup group, ArgKind ak) {
   866             OptionKind kind, OptionGroup group, ArgKind ak) {
   866         this(text, argsNameKey, descrKey, kind, group, null, null, ak);
   867         this(text, argsNameKey, descrKey, kind, group, null, null, ak);
   867     }
   868     }
   868 
   869 
   869     Option(String text, String argsNameKey, String descrKey, OptionKind kind, OptionGroup group,
   870     Option(String text, String argsNameKey, String descrKey, OptionKind kind, OptionGroup group,
   870             ChoiceKind choiceKind, Map<String,Boolean> choices) {
   871             ChoiceKind choiceKind, Set<String> choices) {
   871         this(text, argsNameKey, descrKey, kind, group, choiceKind, choices, ArgKind.REQUIRED);
   872         this(text, argsNameKey, descrKey, kind, group, choiceKind, choices, ArgKind.REQUIRED);
   872     }
   873     }
   873 
   874 
   874     Option(String text, String descrKey,
   875     Option(String text, String descrKey,
   875             OptionKind kind, OptionGroup group,
   876             OptionKind kind, OptionGroup group,
   876             ChoiceKind choiceKind, String... choices) {
   877             ChoiceKind choiceKind, String... choices) {
   877         this(text, null, descrKey, kind, group, choiceKind,
   878         this(text, null, descrKey, kind, group, choiceKind,
   878                 createChoices(choices), ArgKind.REQUIRED);
   879                 new LinkedHashSet<>(Arrays.asList(choices)), ArgKind.REQUIRED);
   879     }
   880     }
   880     // where
       
   881         private static Map<String,Boolean> createChoices(String... choices) {
       
   882             Map<String,Boolean> map = new LinkedHashMap<>();
       
   883             for (String c: choices)
       
   884                 map.put(c, false);
       
   885             return map;
       
   886         }
       
   887 
   881 
   888     private Option(String text, String argsNameKey, String descrKey,
   882     private Option(String text, String argsNameKey, String descrKey,
   889             OptionKind kind, OptionGroup group,
   883             OptionKind kind, OptionGroup group,
   890             ChoiceKind choiceKind, Map<String,Boolean> choices,
   884             ChoiceKind choiceKind, Set<String> choices,
   891             ArgKind argKind) {
   885             ArgKind argKind) {
   892         this.names = text.trim().split("\\s+");
   886         this.names = text.trim().split("\\s+");
   893         Assert.check(names.length >= 1);
   887         Assert.check(names.length >= 1);
   894         this.primaryName = names[0];
   888         this.primaryName = names[0];
   895         this.argsNameKey = argsNameKey;
   889         this.argsNameKey = argsNameKey;
   941             return false;
   935             return false;
   942 
   936 
   943         if (choices != null) {
   937         if (choices != null) {
   944             String arg = option.substring(name.length());
   938             String arg = option.substring(name.length());
   945             if (choiceKind == ChoiceKind.ONEOF)
   939             if (choiceKind == ChoiceKind.ONEOF)
   946                 return choices.keySet().contains(arg);
   940                 return choices.contains(arg);
   947             else {
   941             else {
   948                 for (String a: arg.split(",+")) {
   942                 for (String a: arg.split(",+")) {
   949                     if (!choices.keySet().contains(a))
   943                     if (!choices.contains(a))
   950                         return false;
   944                         return false;
   951                 }
   945                 }
   952             }
   946             }
   953         }
   947         }
   954 
   948 
  1014      */
  1008      */
  1015     public boolean process(OptionHelper helper, String option, String arg) {
  1009     public boolean process(OptionHelper helper, String option, String arg) {
  1016         if (choices != null) {
  1010         if (choices != null) {
  1017             if (choiceKind == ChoiceKind.ONEOF) {
  1011             if (choiceKind == ChoiceKind.ONEOF) {
  1018                 // some clients like to see just one of option+choice set
  1012                 // some clients like to see just one of option+choice set
  1019                 for (String s: choices.keySet())
  1013                 for (String s : choices)
  1020                     helper.remove(primaryName + s);
  1014                     helper.remove(primaryName + s);
  1021                 String opt = primaryName + arg;
  1015                 String opt = primaryName + arg;
  1022                 helper.put(opt, opt);
  1016                 helper.put(opt, opt);
  1023                 // some clients like to see option (without trailing ":")
  1017                 // some clients like to see option (without trailing ":")
  1024                 // set to arg
  1018                 // set to arg
  1111         StringBuilder sb = new StringBuilder();
  1105         StringBuilder sb = new StringBuilder();
  1112         sb.append(name);
  1106         sb.append(name);
  1113         if (argsNameKey == null) {
  1107         if (argsNameKey == null) {
  1114             if (choices != null) {
  1108             if (choices != null) {
  1115                 String sep = "{";
  1109                 String sep = "{";
  1116                 for (Map.Entry<String,Boolean> e: choices.entrySet()) {
  1110                 for (String choice : choices) {
  1117                     if (!e.getValue()) {
  1111                     sb.append(sep);
  1118                         sb.append(sep);
  1112                     sb.append(choices);
  1119                         sb.append(e.getKey());
  1113                     sep = ",";
  1120                         sep = ",";
       
  1121                     }
       
  1122                 }
  1114                 }
  1123                 sb.append("}");
  1115                 sb.append("}");
  1124             }
  1116             }
  1125         } else {
  1117         } else {
  1126             if (!name.matches(".*[=:]$") && argKind != ArgKind.ADJACENT)
  1118             if (!name.matches(".*[=:]$") && argKind != ArgKind.ADJACENT)
  1161                     ? PkgInfo.LEGACY
  1153                     ? PkgInfo.LEGACY
  1162                     : PkgInfo.valueOf(StringUtils.toUpperCase(v)));
  1154                     : PkgInfo.valueOf(StringUtils.toUpperCase(v)));
  1163         }
  1155         }
  1164     }
  1156     }
  1165 
  1157 
  1166     private static Map<String,Boolean> getXLintChoices() {
  1158     private static Set<String> getXLintChoices() {
  1167         Map<String,Boolean> choices = new LinkedHashMap<>();
  1159         Set<String> choices = new LinkedHashSet<>();
  1168         choices.put("all", false);
  1160         choices.add("all");
  1169         for (Lint.LintCategory c : Lint.LintCategory.values())
  1161         for (Lint.LintCategory c : Lint.LintCategory.values()) {
  1170             choices.put(c.option, c.hidden);
  1162             choices.add(c.option);
  1171         for (Lint.LintCategory c : Lint.LintCategory.values())
  1163             choices.add("-" + c.option);
  1172             choices.put("-" + c.option, c.hidden);
  1164         }
  1173         choices.put("none", false);
  1165         choices.add("none");
  1174         return choices;
  1166         return choices;
  1175     }
  1167     }
  1176 
  1168 
  1177     /**
  1169     /**
  1178      * Returns the set of options supported by the command line tool.
  1170      * Returns the set of options supported by the command line tool.