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