OptionHelper.java revision 2571:10fc81ac75b4
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.nio.file.Files;
29import java.nio.file.Path;
30import java.nio.file.Paths;
31import java.util.Arrays;
32import java.util.List;
33
34import 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 */
40public abstract class OptionHelper {
41
42    /** Handle error */
43    public abstract void reportError(String msg);
44
45    /** Record a package exclusion pattern */
46    public abstract void exclude(String excl);
47
48    /** Record a package inclusion pattern */
49    public abstract void include(String incl);
50
51    /** Record a file exclusion */
52    public abstract void excludeFile(String exclFile);
53
54    /** Record a file inclusion */
55    public abstract void includeFile(String inclFile);
56
57    /** Record a root of sources to be compiled */
58    public abstract void sourceRoots(List<Path> path);
59
60    /** Record a suffix + transformer */
61    public abstract void addTransformer(String suffix, Transformer tr);
62
63    /** Record a sourcepath to be used */
64    public abstract void sourcepath(List<Path> path);
65
66    /** Record a modulepath to be used */
67    public abstract void modulepath(List<Path> path);
68
69    /** Record a classpath to be used */
70    public abstract void classpath(List<Path> path);
71
72    /** Record the number of cores */
73    public abstract void numCores(int parseInt);
74
75    /** Record desired log level */
76    public abstract void logLevel(String level);
77
78    /** Record path for reference source list */
79    public abstract void compareFoundSources(Path referenceList);
80
81    /** Record the fact that unidentified artifacts are permitted */
82    public abstract void permitUnidentifiedArtifacts();
83
84    /** Record the fact that sources in the default package are permitted */
85    public abstract void permitDefaultPackage();
86
87    /** Record server configuration parameters */
88    public abstract void serverConf(String serverConf);
89
90    /** Record server launch configuration parameters */
91    public abstract void startServerConf(String serverConf);
92
93    /** Record some arguments to be passed on to javac */
94    public abstract void javacArg(String... arg);
95
96    /** Sets the destination directory for the compilation */
97    public abstract void destDir(Path dir);
98
99    /** Sets the directory for generated sources */
100    public abstract void generatedSourcesDir(Path genSrcDir);
101
102    /** Sets the directory for generated headers */
103    public abstract void headerDir(Path dir);
104
105    /** Sets the implicit policy */
106    public abstract void implicit(String policy);
107
108
109    /**
110     * Traverses an array of arguments and performs the appropriate callbacks.
111     *
112     * @param args the arguments to traverse.
113     */
114    void traverse(String[] args) {
115
116        ArgumentIterator argIter = new ArgumentIterator(Arrays.asList(args));
117
118        nextArg:
119        while (argIter.hasNext()) {
120
121            String arg = argIter.next();
122
123            if (arg.startsWith("-")) {
124                for (Option opt : Option.values()) {
125                    if (opt.processCurrent(argIter, this))
126                        continue nextArg;
127                }
128
129                javacArg(arg);
130
131                // Does this javac argument take an argument? If so, don't
132                // let it pass on to sjavac as a source root directory.
133                for (com.sun.tools.javac.main.Option javacOpt : com.sun.tools.javac.main.Option.values()) {
134                    if (javacOpt.matches(arg)) {
135                        boolean takesArgument = javacOpt.hasArg();
136                        boolean separateToken = !arg.contains(":") && !arg.contains("=");
137                        if (takesArgument && separateToken)
138                            javacArg(argIter.next());
139                    }
140                }
141            } else {
142                sourceRoots(Arrays.asList(Paths.get(arg)));
143            }
144        }
145    }
146}
147