ToolOption.java revision 3769:9b74986367e3
1/*
2 * Copyright (c) 2012, 2016, 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.javadoc.main;
27
28import java.util.LinkedHashMap;
29import java.util.Map;
30import java.util.StringTokenizer;
31
32import com.sun.tools.javac.code.Flags;
33import com.sun.tools.javac.main.Option;
34import com.sun.tools.javac.main.Option.InvalidValueException;
35import com.sun.tools.javac.main.OptionHelper;
36import com.sun.tools.javac.util.ListBuffer;
37import com.sun.tools.javac.util.Options;
38
39
40/**
41 * javadoc tool options.
42 *
43 *  <p><b>This is NOT part of any supported API.
44 *  If you write code that depends on this, you do so at your own risk.
45 *  This code and its internal interfaces are subject to change or
46 *  deletion without notice.</b>
47 */
48@Deprecated
49public enum ToolOption {
50    // ----- options for underlying compiler -----
51
52    BOOTCLASSPATH("-bootclasspath", true) {
53        @Override
54        public void process(Helper helper, String arg) {
55            helper.setFileManagerOpt(Option.BOOT_CLASS_PATH, arg);
56        }
57    },
58
59    CLASSPATH("-classpath", true) {
60        @Override
61        public void process(Helper helper, String arg) {
62            helper.setFileManagerOpt(Option.CLASS_PATH, arg);
63        }
64    },
65
66    CP("-cp", true) {
67        @Override
68        public void process(Helper helper, String arg) {
69            helper.setFileManagerOpt(Option.CLASS_PATH, arg);
70        }
71    },
72
73    CLASS_PATH("--class-path", true) {
74        @Override
75        public void process(Helper helper, String arg) {
76            helper.setFileManagerOpt(Option.CLASS_PATH, arg);
77        }
78    },
79
80    EXTDIRS("-extdirs", true) {
81        @Override
82        public void process(Helper helper, String arg) {
83            helper.setFileManagerOpt(Option.EXTDIRS, arg);
84        }
85    },
86
87    SOURCEPATH("-sourcepath", true) {
88        @Override
89        public void process(Helper helper, String arg) {
90            helper.setFileManagerOpt(Option.SOURCE_PATH, arg);
91        }
92    },
93
94    SOURCE_PATH("--source-path", true) {
95        @Override
96        public void process(Helper helper, String arg) {
97            helper.setFileManagerOpt(Option.SOURCE_PATH, arg);
98        }
99    },
100
101    SYSCLASSPATH("-sysclasspath", true) {
102        @Override
103        public void process(Helper helper, String arg) {
104            helper.setFileManagerOpt(Option.BOOT_CLASS_PATH, arg);
105        }
106    },
107
108    MODULE_SOURCE_PATH("--module-source-path", true) {
109        @Override
110        public void process(Helper helper, String arg) {
111            helper.setFileManagerOpt(Option.MODULE_SOURCE_PATH, arg);
112        }
113    },
114
115    UPGRADE_MODULE_PATH("--upgrade-module-path", true) {
116        @Override
117        public void process(Helper helper, String arg) {
118            helper.setFileManagerOpt(Option.UPGRADE_MODULE_PATH, arg);
119        }
120    },
121
122    SYSTEM_("--system", true) {
123        @Override
124        public void process(Helper helper, String arg) {
125            helper.setFileManagerOpt(Option.SYSTEM, arg);
126        }
127    },
128
129    MODULE_PATH("--module-path", true) {
130        @Override
131        public void process(Helper helper, String arg) {
132            helper.setFileManagerOpt(Option.MODULE_PATH, arg);
133        }
134    },
135
136    P("-p", true) {
137        @Override
138        public void process(Helper helper, String arg) {
139            helper.setFileManagerOpt(Option.MODULE_PATH, arg);
140        }
141    },
142
143    ADD_MODULES("--add-modules", true) {
144        @Override
145        public void process(Helper helper, String arg) throws InvalidValueException {
146            Option.ADD_MODULES.process(helper.getOptionHelper(), opt, arg);
147        }
148    },
149
150    LIMIT_MODULES("--limit-modules", true) {
151        @Override
152        public void process(Helper helper, String arg) throws InvalidValueException {
153            Option.LIMIT_MODULES.process(helper.getOptionHelper(), opt, arg);
154        }
155    },
156
157    ENCODING("-encoding", true) {
158        @Override
159        public void process(Helper helper, String arg) {
160            helper.encoding = arg;
161            helper.setCompilerOpt(opt, arg);
162        }
163    },
164
165    RELEASE("--release", true) {
166        @Override
167        public void process(Helper helper, String arg) {
168            helper.setCompilerOpt(opt, arg);
169        }
170    },
171
172    SOURCE("-source", true) {
173        @Override
174        public void process(Helper helper, String arg) {
175            helper.setCompilerOpt(opt, arg);
176        }
177    },
178
179    XMAXERRS("-Xmaxerrs", true) {
180        @Override
181        public void process(Helper helper, String arg) {
182            helper.setCompilerOpt(opt, arg);
183        }
184    },
185
186    XMAXWARNS("-Xmaxwarns", true) {
187        @Override
188        public void process(Helper helper, String arg) {
189            helper.setCompilerOpt(opt, arg);
190        }
191    },
192
193    ADD_READS("--add-reads", true) {
194        @Override
195        public void process(Helper helper, String arg) throws InvalidValueException {
196            Option.ADD_READS.process(helper.getOptionHelper(), opt, arg);
197        }
198    },
199
200    ADD_EXPORTS("--add-exports", true) {
201        @Override
202        public void process(Helper helper, String arg) throws InvalidValueException {
203            Option.ADD_EXPORTS.process(helper.getOptionHelper(), opt, arg);
204        }
205    },
206
207    XMODULE("-Xmodule:", false) {
208        @Override
209        public void process(Helper helper, String arg) throws InvalidValueException {
210            Option.XMODULE.process(helper.getOptionHelper(), arg);
211        }
212    },
213
214    PATCH_MODULE("--patch-module", true) {
215        @Override
216        public void process(Helper helper, String arg) throws InvalidValueException {
217            Option.PATCH_MODULE.process(helper.getOptionHelper(), opt, arg);
218        }
219    },
220
221    // ----- doclet options -----
222
223    DOCLET("-doclet", true), // handled in setDocletInvoker
224
225    DOCLETPATH("-docletpath", true), // handled in setDocletInvoker
226
227    // ----- selection options -----
228
229    SUBPACKAGES("-subpackages", true) {
230        @Override
231        public void process(Helper helper, String arg) {
232            helper.addToList(helper.subPackages, arg);
233        }
234    },
235
236    EXCLUDE("-exclude", true) {
237        @Override
238        public void process(Helper helper, String arg) {
239            helper.addToList(helper.excludedPackages, arg);
240        }
241    },
242
243    // ----- filtering options -----
244
245    PACKAGE("-package") {
246        @Override
247        public void process(Helper helper) {
248            helper.setFilter(
249                    Flags.PUBLIC | Flags.PROTECTED | ModifierFilter.PACKAGE);
250        }
251    },
252
253    PRIVATE("-private") {
254        @Override
255        public void process(Helper helper) {
256            helper.setFilter(ModifierFilter.ALL_ACCESS);
257        }
258    },
259
260    PROTECTED("-protected") {
261        @Override
262        public void process(Helper helper) {
263            helper.setFilter(Flags.PUBLIC | Flags.PROTECTED);
264        }
265    },
266
267    PUBLIC("-public") {
268        @Override
269        public void process(Helper helper) {
270            helper.setFilter(Flags.PUBLIC);
271        }
272    },
273
274    // ----- output control options -----
275
276    PROMPT("-prompt") {
277        @Override
278        public void process(Helper helper) {
279            helper.compOpts.put("-prompt", "-prompt");
280            helper.promptOnError = true;
281        }
282    },
283
284    QUIET("-quiet") {
285        @Override
286        public void process(Helper helper) {
287            helper.quiet = true;
288        }
289    },
290
291    VERBOSE("-verbose") {
292        @Override
293        public void process(Helper helper) {
294            helper.compOpts.put("-verbose", "");
295        }
296    },
297
298    XWERROR("-Xwerror") {
299        @Override
300        public void process(Helper helper) {
301            helper.rejectWarnings = true;
302
303        }
304    },
305
306    // ----- other options -----
307
308    BREAKITERATOR("-breakiterator") {
309        @Override
310        public void process(Helper helper) {
311            helper.breakiterator = true;
312        }
313    },
314
315    LOCALE("-locale", true) {
316        @Override
317        public void process(Helper helper, String arg) {
318            helper.docLocale = arg;
319        }
320    },
321
322    OVERVIEW("-overview", true),
323
324    XCLASSES("-Xclasses") {
325        @Override
326        public void process(Helper helper) {
327            helper.docClasses = true;
328
329        }
330    },
331
332    // ----- help options -----
333
334    HELP("-help") {
335        @Override
336        public void process(Helper helper) {
337            helper.usage();
338        }
339    },
340
341    X("-X") {
342        @Override
343        public void process(Helper helper) {
344            helper.Xusage();
345        }
346    };
347
348    public final String opt;
349    public final boolean hasArg;
350
351    ToolOption(String opt) {
352        this(opt, false);
353    }
354
355    ToolOption(String opt, boolean hasArg) {
356        this.opt = opt;
357        this.hasArg = hasArg;
358    }
359
360    void process(Helper helper, String arg) throws Option.InvalidValueException { }
361
362    void process(Helper helper) { }
363
364    static ToolOption get(String name) {
365        for (ToolOption o: values()) {
366            if (name.equals(o.opt))
367                return o;
368        }
369        return null;
370    }
371
372    static abstract class Helper {
373        /** List of decoded options. */
374        final ListBuffer<String[]> options = new ListBuffer<>();
375
376        /** Selected packages, from -subpackages. */
377        final ListBuffer<String> subPackages = new ListBuffer<>();
378
379        /** Excluded packages, from -exclude. */
380        final ListBuffer<String> excludedPackages = new ListBuffer<>();
381
382        // File manager options
383        final Map<Option, String> fileManagerOpts = new LinkedHashMap<>();
384
385        /** javac options, set by various options. */
386        Options compOpts; // = Options.instance(context)
387
388        /* Encoding for javac, and files written? set by -encoding. */
389        String encoding = null;
390
391        /** Set by -breakiterator. */
392        boolean breakiterator = false;
393
394        /** Set by -quiet. */
395        boolean quiet = false;
396
397        /** Set by -Xclasses. */
398        boolean docClasses = false;
399
400        /** Set by -Xwerror. */
401        boolean rejectWarnings = false;
402
403        /** Set by -prompt. */
404        boolean promptOnError;
405
406        /** Set by -locale. */
407        String docLocale = "";
408
409        /** Set by -public, private, -protected, -package. */
410        ModifierFilter showAccess = null;
411
412        abstract void usage();
413        abstract void Xusage();
414
415        abstract void usageError(String msg, Object... args);
416        abstract OptionHelper getOptionHelper();
417
418        void addToList(ListBuffer<String> list, String str){
419            StringTokenizer st = new StringTokenizer(str, ":");
420            String current;
421            while(st.hasMoreTokens()){
422                current = st.nextToken();
423                list.append(current);
424            }
425        }
426
427        void setFilter(long filterBits) {
428            if (showAccess != null) {
429                usageError("main.incompatible.access.flags");
430            }
431            showAccess = new ModifierFilter(filterBits);
432        }
433
434        void setCompilerOpt(String opt, String arg) {
435            if (compOpts.get(opt) != null) {
436                usageError("main.option.already.seen", opt);
437            }
438            compOpts.put(opt, arg);
439        }
440
441        void setFileManagerOpt(Option opt, String arg) {
442            fileManagerOpts.put(opt, arg);
443        }
444    }
445}
446