1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.sjavac.options;
  27 
  28 import java.io.IOException;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.util.Arrays;
  33 import java.util.List;
  34 
  35 import com.sun.tools.javac.main.CommandLine;
  36 import com.sun.tools.sjavac.Transformer;
  37 
  38 /**
  39  * This class is used to decode sjavac options.
  40  * See com.sun.tools.sjavac.options.Options for example usage.
  41  */
  42 public abstract class OptionHelper {
  43 
  44     /** Handle error */
  45     public abstract void reportError(String msg);
  46 
  47     /** Record a package exclusion pattern */
  48     public abstract void exclude(String excl);
  49 
  50     /** Record a package inclusion pattern */
  51     public abstract void include(String incl);
  52 
  53     /** Record a file exclusion */
  54     public abstract void excludeFile(String exclFile);
  55 
  56     /** Record a file inclusion */
  57     public abstract void includeFile(String inclFile);
  58 
  59     /** Record a root of sources to be compiled */
  60     public abstract void sourceRoots(List<Path> path);
  61 
  62     /** Record a suffix + transformer */
  63     public abstract void addTransformer(String suffix, Transformer tr);
  64 
  65     /** Record a sourcepath to be used */
  66     public abstract void sourcepath(List<Path> path);
  67 
  68     /** Record a modulepath to be used */
  69     public abstract void modulepath(List<Path> path);
  70 
  71     /** Record a classpath to be used */
  72     public abstract void classpath(List<Path> path);
  73 
  74     /** Record the number of cores */
  75     public abstract void numCores(int parseInt);
  76 
  77     /** Record desired log level */
  78     public abstract void logLevel(String level);
  79 
  80     /** Record path for reference source list */
  81     public abstract void compareFoundSources(Path referenceList);
  82 
  83     /** Record the fact that unidentified artifacts are permitted */
  84     public abstract void permitUnidentifiedArtifacts();
  85 
  86     /** Record the fact that sources in the default package are permitted */
  87     public abstract void permitDefaultPackage();
  88 
  89     /** Record server configuration parameters */
  90     public abstract void serverConf(String serverConf);
  91 
  92     /** Record server launch configuration parameters */
  93     public abstract void startServerConf(String serverConf);
  94 
  95     /** Record some arguments to be passed on to javac */
  96     public abstract void javacArg(String... arg);
  97 
  98     /** Sets the destination directory for the compilation */
  99     public abstract void destDir(Path dir);
 100 
 101     /** Sets the directory for generated sources */
 102     public abstract void generatedSourcesDir(Path genSrcDir);
 103 
 104     /** Sets the directory for generated headers */
 105     public abstract void headerDir(Path dir);
 106 
 107     /** Sets the implicit policy */
 108     public abstract void implicit(String policy);
 109 
 110 
 111     /**
 112      * Traverses an array of arguments and performs the appropriate callbacks.
 113      *
 114      * @param args the arguments to traverse.
 115      */
 116     void traverse(String[] args) {
 117         try {
 118             args = CommandLine.parse(args); // Detect @file and load it as a command line.
 119         } catch (java.io.IOException e) {
 120             throw new IllegalArgumentException("Problem reading @"+e.getMessage());
 121         }
 122         ArgumentIterator argIter = new ArgumentIterator(Arrays.asList(args));
 123 
 124         nextArg:
 125         while (argIter.hasNext()) {
 126 
 127             String arg = argIter.next();
 128 
 129             if (arg.startsWith("-")) {
 130                 for (Option opt : Option.values()) {
 131                     if (opt.processCurrent(argIter, this))
 132                         continue nextArg;
 133                 }
 134 
 135                 javacArg(arg);
 136 
 137                 // Does this javac argument take an argument? If so, don't
 138                 // let it pass on to sjavac as a source root directory.
 139                 for (com.sun.tools.javac.main.Option javacOpt : com.sun.tools.javac.main.Option.values()) {
 140                     if (javacOpt.matches(arg)) {
 141                         boolean takesArgument = javacOpt.hasArg();
 142                         boolean separateToken = !arg.contains(":") && !arg.contains("=");
 143                         if (takesArgument && separateToken)
 144                             javacArg(argIter.next());
 145                     }
 146                 }
 147             } else {
 148                 sourceRoots(Arrays.asList(Paths.get(arg)));
 149             }
 150         }
 151     }
 152 }