langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java
changeset 41938 8e66bf10fcec
parent 41637 7b24b4c32ee6
child 41992 720257f5307d
equal deleted inserted replaced
41937:1313399705e9 41938:8e66bf10fcec
    41 import java.util.Locale;
    41 import java.util.Locale;
    42 import java.util.Map;
    42 import java.util.Map;
    43 import java.util.ServiceLoader;
    43 import java.util.ServiceLoader;
    44 import java.util.Set;
    44 import java.util.Set;
    45 import java.util.TreeSet;
    45 import java.util.TreeSet;
       
    46 import java.util.regex.Pattern;
    46 import java.util.stream.Collectors;
    47 import java.util.stream.Collectors;
    47 import java.util.stream.StreamSupport;
    48 import java.util.stream.StreamSupport;
    48 
    49 
    49 import javax.lang.model.SourceVersion;
    50 import javax.lang.model.SourceVersion;
    50 
    51 
   554     },
   555     },
   555 
   556 
   556     ADD_EXPORTS("--add-exports", "opt.arg.addExports", "opt.addExports", EXTENDED, BASIC) {
   557     ADD_EXPORTS("--add-exports", "opt.arg.addExports", "opt.addExports", EXTENDED, BASIC) {
   557         @Override
   558         @Override
   558         public boolean process(OptionHelper helper, String option, String arg) {
   559         public boolean process(OptionHelper helper, String option, String arg) {
   559             String prev = helper.get(ADD_EXPORTS);
   560             if (arg.isEmpty()) {
   560             helper.put(ADD_EXPORTS.primaryName, (prev == null) ? arg : prev + '\0' + arg);
   561                 helper.error("err.no.value.for.option", option);
   561             return false;
   562                 return true;
       
   563             } else if (getPattern().matcher(arg).matches()) {
       
   564                 String prev = helper.get(ADD_EXPORTS);
       
   565                 helper.put(ADD_EXPORTS.primaryName, (prev == null) ? arg : prev + '\0' + arg);
       
   566                 return false;
       
   567             } else {
       
   568                 helper.error("err.bad.value.for.option", option, arg);
       
   569                 return true;
       
   570             }
       
   571         }
       
   572 
       
   573         @Override
       
   574         public Pattern getPattern() {
       
   575             return Pattern.compile("([^/]+)/([^=]+)=(,*[^,].*)");
   562         }
   576         }
   563     },
   577     },
   564 
   578 
   565     ADD_READS("--add-reads", "opt.arg.addReads", "opt.addReads", EXTENDED, BASIC) {
   579     ADD_READS("--add-reads", "opt.arg.addReads", "opt.addReads", EXTENDED, BASIC) {
   566         @Override
   580         @Override
   567         public boolean process(OptionHelper helper, String option, String arg) {
   581         public boolean process(OptionHelper helper, String option, String arg) {
   568             String prev = helper.get(ADD_READS);
   582             if (arg.isEmpty()) {
   569             helper.put(ADD_READS.primaryName, (prev == null) ? arg : prev + '\0' + arg);
   583                 helper.error("err.no.value.for.option", option);
   570             return false;
   584                 return true;
       
   585             } else if (getPattern().matcher(arg).matches()) {
       
   586                 String prev = helper.get(ADD_READS);
       
   587                 helper.put(ADD_READS.primaryName, (prev == null) ? arg : prev + '\0' + arg);
       
   588                 return false;
       
   589             } else {
       
   590                 helper.error("err.bad.value.for.option", option, arg);
       
   591                 return true;
       
   592             }
       
   593         }
       
   594 
       
   595         @Override
       
   596         public Pattern getPattern() {
       
   597             return Pattern.compile("([^=]+)=(,*[^,].*)");
   571         }
   598         }
   572     },
   599     },
   573 
   600 
   574     XMODULE("-Xmodule:", "opt.arg.module", "opt.module", EXTENDED, BASIC) {
   601     XMODULE("-Xmodule:", "opt.arg.module", "opt.module", EXTENDED, BASIC) {
   575         @Override
   602         @Override
   576         public boolean process(OptionHelper helper, String option, String arg) {
   603         public boolean process(OptionHelper helper, String option, String arg) {
   577             String prev = helper.get(XMODULE);
   604             String prev = helper.get(XMODULE);
   578             if (prev != null) {
   605             if (prev != null) {
   579                 helper.error("err.option.too.many", XMODULE.primaryName);
   606                 helper.error("err.option.too.many", XMODULE.primaryName);
       
   607                 return true;
   580             }
   608             }
   581             helper.put(XMODULE.primaryName, arg);
   609             helper.put(XMODULE.primaryName, arg);
   582             return false;
   610             return false;
   583         }
   611         }
   584     },
   612     },
   585 
   613 
   586     MODULE("--module -m", "opt.arg.m", "opt.m", STANDARD, BASIC),
   614     MODULE("--module -m", "opt.arg.m", "opt.m", STANDARD, BASIC),
   587 
   615 
   588     ADD_MODULES("--add-modules", "opt.arg.addmods", "opt.addmods", STANDARD, BASIC),
   616     ADD_MODULES("--add-modules", "opt.arg.addmods", "opt.addmods", STANDARD, BASIC) {
   589 
   617         @Override
   590     LIMIT_MODULES("--limit-modules", "opt.arg.limitmods", "opt.limitmods", STANDARD, BASIC),
   618         public boolean process(OptionHelper helper, String option, String arg) {
       
   619             if (arg.isEmpty()) {
       
   620                 helper.error("err.no.value.for.option", option);
       
   621                 return true;
       
   622             } else if (getPattern().matcher(arg).matches()) {
       
   623                 String prev = helper.get(ADD_MODULES);
       
   624                 // since the individual values are simple names, we can simply join the
       
   625                 // values of multiple --add-modules options with ','
       
   626                 helper.put(ADD_MODULES.primaryName, (prev == null) ? arg : prev + ',' + arg);
       
   627                 return false;
       
   628             } else {
       
   629                 helper.error("err.bad.value.for.option", option, arg);
       
   630                 return true;
       
   631             }
       
   632         }
       
   633 
       
   634         @Override
       
   635         public Pattern getPattern() {
       
   636             return Pattern.compile(",*[^,].*");
       
   637         }
       
   638     },
       
   639 
       
   640     LIMIT_MODULES("--limit-modules", "opt.arg.limitmods", "opt.limitmods", STANDARD, BASIC) {
       
   641         @Override
       
   642         public boolean process(OptionHelper helper, String option, String arg) {
       
   643             if (arg.isEmpty()) {
       
   644                 helper.error("err.no.value.for.option", option);
       
   645                 return true;
       
   646             } else if (getPattern().matcher(arg).matches()) {
       
   647                 helper.put(LIMIT_MODULES.primaryName, arg); // last one wins
       
   648                 return false;
       
   649             } else {
       
   650                 helper.error("err.bad.value.for.option", option, arg);
       
   651                 return true;
       
   652             }
       
   653         }
       
   654 
       
   655         @Override
       
   656         public Pattern getPattern() {
       
   657             return Pattern.compile(",*[^,].*");
       
   658         }
       
   659     },
   591 
   660 
   592     // This option exists only for the purpose of documenting itself.
   661     // This option exists only for the purpose of documenting itself.
   593     // It's actually implemented by the CommandLine class.
   662     // It's actually implemented by the CommandLine class.
   594     AT("@", "opt.arg.file", "opt.AT", STANDARD, INFO, ArgKind.ADJACENT) {
   663     AT("@", "opt.arg.file", "opt.AT", STANDARD, INFO, ArgKind.ADJACENT) {
   595         @Override
   664         @Override
   961      * @return true if the operation was successful, and false otherwise
  1030      * @return true if the operation was successful, and false otherwise
   962      * @implNote The return value is the opposite of that used by {@link #process}.
  1031      * @implNote The return value is the opposite of that used by {@link #process}.
   963      */
  1032      */
   964     public boolean handleOption(OptionHelper helper, String arg, Iterator<String> rest) {
  1033     public boolean handleOption(OptionHelper helper, String arg, Iterator<String> rest) {
   965         if (hasArg()) {
  1034         if (hasArg()) {
       
  1035             String option;
   966             String operand;
  1036             String operand;
   967             int sep = findSeparator(arg);
  1037             int sep = findSeparator(arg);
   968             if (getArgKind() == Option.ArgKind.ADJACENT) {
  1038             if (getArgKind() == Option.ArgKind.ADJACENT) {
       
  1039                 option = primaryName; // aliases not supported
   969                 operand = arg.substring(primaryName.length());
  1040                 operand = arg.substring(primaryName.length());
   970             } else if (sep > 0) {
  1041             } else if (sep > 0) {
       
  1042                 option = arg.substring(0, sep);
   971                 operand = arg.substring(sep + 1);
  1043                 operand = arg.substring(sep + 1);
   972             } else {
  1044             } else {
   973                 if (!rest.hasNext()) {
  1045                 if (!rest.hasNext()) {
   974                     helper.error("err.req.arg", arg);
  1046                     helper.error("err.req.arg", arg);
   975                     return false;
  1047                     return false;
   976                 }
  1048                 }
       
  1049                 option = arg;
   977                 operand = rest.next();
  1050                 operand = rest.next();
   978             }
  1051             }
   979             return !process(helper, arg, operand);
  1052             return !process(helper, option, operand);
   980         } else {
  1053         } else {
   981             return !process(helper, arg);
  1054             return !process(helper, arg);
   982         }
  1055         }
   983     }
  1056     }
   984 
  1057 
  1028         }
  1101         }
  1029         helper.put(primaryName, arg);
  1102         helper.put(primaryName, arg);
  1030         if (group == OptionGroup.FILEMANAGER)
  1103         if (group == OptionGroup.FILEMANAGER)
  1031             helper.handleFileManagerOption(this, arg);
  1104             helper.handleFileManagerOption(this, arg);
  1032         return false;
  1105         return false;
       
  1106     }
       
  1107 
       
  1108     /**
       
  1109      * Returns a pattern to analyze the value for an option.
       
  1110      * @return the pattern
       
  1111      * @throws UnsupportedOperationException if an option does not provide a pattern.
       
  1112      */
       
  1113     public Pattern getPattern() {
       
  1114         throw new UnsupportedOperationException();
  1033     }
  1115     }
  1034 
  1116 
  1035     /**
  1117     /**
  1036      * Scans a word to find the first separator character, either colon or equals.
  1118      * Scans a word to find the first separator character, either colon or equals.
  1037      * @param word the word to be scanned
  1119      * @param word the word to be scanned