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.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.Arrays;
31 import java.util.List;
32 
33 import com.sun.tools.javac.main.CommandLine;
34 import com.sun.tools.sjavac.Transformer;
35 
36 /**
37  * This class is used to decode sjavac options.
38  * See com.sun.tools.sjavac.options.Options for example usage.
39  *
40  *  <p><b>This is NOT part of any supported API.
41  *  If you write code that depends on this, you do so at your own risk.
42  *  This code and its internal interfaces are subject to change or
43  *  deletion without notice.</b>
44  */
45 public abstract class OptionHelper {
46 
47     /** Handle error */
reportError(String msg)48     public abstract void reportError(String msg);
49 
50     /** Record a package exclusion pattern */
exclude(String excl)51     public abstract void exclude(String excl);
52 
53     /** Record a package inclusion pattern */
include(String incl)54     public abstract void include(String incl);
55 
56     /** Record a root of sources to be compiled */
sourceRoots(List<Path> path)57     public abstract void sourceRoots(List<Path> path);
58 
59     /** Record a suffix + transformer */
addTransformer(String suffix, Transformer tr)60     public abstract void addTransformer(String suffix, Transformer tr);
61 
62     /** Record a sourcepath to be used */
sourcepath(List<Path> path)63     public abstract void sourcepath(List<Path> path);
64 
65     /** Record a modulepath to be used */
modulepath(List<Path> path)66     public abstract void modulepath(List<Path> path);
67 
68     /** Record a classpath to be used */
classpath(List<Path> path)69     public abstract void classpath(List<Path> path);
70 
71     /** Record the number of cores */
numCores(int parseInt)72     public abstract void numCores(int parseInt);
73 
74     /** Record desired log level */
logLevel(String level)75     public abstract void logLevel(String level);
76 
77     /** Record path for reference source list */
compareFoundSources(Path referenceList)78     public abstract void compareFoundSources(Path referenceList);
79 
80     /** Record a single permitted artifact */
permitArtifact(String f)81     public abstract void permitArtifact(String f);
82 
83     /** Record the fact that unidentified artifacts are permitted */
permitUnidentifiedArtifacts()84     public abstract void permitUnidentifiedArtifacts();
85 
86     /** Record the fact that sources in the default package are permitted */
permitDefaultPackage()87     public abstract void permitDefaultPackage();
88 
89     /** Record server configuration parameters */
serverConf(String serverConf)90     public abstract void serverConf(String serverConf);
91 
92     /** Record server launch configuration parameters */
startServerConf(String serverConf)93     public abstract void startServerConf(String serverConf);
94 
95     /** Record some arguments to be passed on to javac */
javacArg(String... arg)96     public abstract void javacArg(String... arg);
97 
98     /** Sets the destination directory for the compilation */
destDir(Path dir)99     public abstract void destDir(Path dir);
100 
101     /** Sets the directory for generated sources */
generatedSourcesDir(Path genSrcDir)102     public abstract void generatedSourcesDir(Path genSrcDir);
103 
104     /** Sets the directory for generated headers */
headerDir(Path dir)105     public abstract void headerDir(Path dir);
106 
107     /** Sets the directory for state and log files generated by sjavac */
stateDir(Path dir)108     public abstract void stateDir(Path dir);
109 
110     /** Sets the implicit policy */
implicit(String policy)111     public abstract void implicit(String policy);
112 
113 
114     /**
115      * Traverses an array of arguments and performs the appropriate callbacks.
116      *
117      * @param args the arguments to traverse.
118      */
traverse(String[] args)119     void traverse(String[] args) {
120         try {
121             args = CommandLine.parse(args); // Detect @file and load it as a command line.
122         } catch (java.io.IOException e) {
123             throw new IllegalArgumentException("Problem reading @"+e.getMessage());
124         }
125         ArgumentIterator argIter = new ArgumentIterator(Arrays.asList(args));
126 
127         nextArg:
128         while (argIter.hasNext()) {
129 
130             String arg = argIter.next();
131 
132             if (arg.startsWith("-")) {
133                 for (Option opt : Option.values()) {
134                     if (opt.processCurrent(argIter, this))
135                         continue nextArg;
136                 }
137 
138                 javacArg(arg);
139 
140                 // Does this javac argument take an argument? If so, don't
141                 // let it pass on to sjavac as a source root directory.
142                 for (com.sun.tools.javac.main.Option javacOpt : com.sun.tools.javac.main.Option.values()) {
143                     if (javacOpt.matches(arg)) {
144                         boolean takesArgument = javacOpt.hasArg();
145                         boolean separateToken = !arg.contains(":") && !arg.contains("=");
146                         if (takesArgument && separateToken)
147                             javacArg(argIter.next());
148                     }
149                 }
150             } else {
151                 sourceRoots(Arrays.asList(Paths.get(arg)));
152             }
153         }
154     }
155 
unescapeCmdArg(String arg)156     public static String unescapeCmdArg(String arg) {
157         return arg.replaceAll("%20", " ")
158                   .replaceAll("%2C", ",");
159     }
160 }
161