OptionHelper.java revision 3769:9b74986367e3
1/*
2 * Copyright (c) 2006, 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.javac.main;
27
28import java.nio.file.Path;
29
30import com.sun.tools.javac.util.JCDiagnostic;
31import com.sun.tools.javac.util.Log;
32import com.sun.tools.javac.util.Log.PrefixKind;
33
34/**
35 * Helper object to be used by {@link Option#process}, providing access to
36 * the compilation environment.
37 *
38 * <p><b>This is NOT part of any supported API.
39 * If you write code that depends on this, you do so at your own
40 * risk.  This code and its internal interfaces are subject to change
41 * or deletion without notice.</b></p>
42 */
43
44public abstract class OptionHelper {
45
46    /**
47     * Get the current value of an option.
48     * @param option the option
49     * @return the value of the option
50     */
51    public abstract String get(Option option);
52
53    /**
54     * Set the value of an option.
55     * @param name the primary name of the option
56     * @param value the value for the option
57     */
58    public abstract void put(String name, String value);
59
60    /**
61     * Remove any prior value for an option.
62     * @param name the primary name of the option
63     */
64    public abstract void remove(String name);
65
66    /**
67     * Handle a file manager option.
68     * @param option the option
69     * @param value the value for the option
70     * @return true if the option was handled successfully, and false otherwise
71     */
72    public abstract boolean handleFileManagerOption(Option option, String value);
73
74    /**
75     * Get access to the Log for the compilation.
76     * @return the log
77     */
78    public abstract Log getLog();
79
80    /**
81     * Get the name of the tool, such as "javac", to be used in info like -help.
82     * @return the name of the tool
83     */
84    public abstract String getOwnName();
85
86    /**
87     * Returns a new InvalidValueException, with a localized detail message.
88     * @param key the resource key for the message
89     * @param args the arguments, if any, for the resource string
90     * @return the InvalidValueException
91     */
92    Option.InvalidValueException newInvalidValueException(String key, Object... args) {
93        return new Option.InvalidValueException(getLog().localize(PrefixKind.JAVAC, key, args));
94    }
95
96    /** Record a file to be compiled. */
97    abstract void addFile(Path p);
98
99    /** Record the name of a class for annotation processing. */
100    abstract void addClassName(String s);
101
102    /** An implementation of OptionHelper that mostly throws exceptions. */
103    public static class GrumpyHelper extends OptionHelper {
104        private final Log log;
105
106        public GrumpyHelper(Log log) {
107            this.log = log;
108        }
109
110        @Override
111        public Log getLog() {
112            return log;
113        }
114
115        @Override
116        public String getOwnName() {
117            throw new IllegalStateException();
118        }
119
120        @Override
121        public String get(Option option) {
122            throw new IllegalArgumentException();
123        }
124
125        @Override
126        public void put(String name, String value) {
127            throw new IllegalArgumentException();
128        }
129
130        @Override
131        public void remove(String name) {
132            throw new IllegalArgumentException();
133        }
134
135        @Override
136        public boolean handleFileManagerOption(Option option, String value) {
137            throw new IllegalArgumentException();
138        }
139
140        @Override
141        public void addFile(Path p) {
142            throw new IllegalArgumentException(p.toString());
143        }
144
145        @Override
146        public void addClassName(String s) {
147            throw new IllegalArgumentException(s);
148        }
149    }
150
151}
152