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 
  26 package com.sun.tools.javac.main;
  27 
  28 import java.nio.file.Path;
  29 
  30 import com.sun.tools.javac.util.JCDiagnostic;
  31 import com.sun.tools.javac.util.Log;
  32 import 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 
  44 public abstract class OptionHelper {
  45 
  46     /** Get the current value of an option. */
  47     public abstract String get(Option option);
  48 
  49     /** Set the value of an option. */
  50     public abstract void put(String name, String value);
  51 
  52     /** Remove any prior value for an option. */
  53     public abstract void remove(String name);
  54 
  55     /** Handle a file manager option. */
  56     public abstract boolean handleFileManagerOption(Option option, String value);
  57 
  58     /** Get access to the Log for the compilation. */
  59     public abstract Log getLog();
  60 
  61     /** Get the name of the tool, such as "javac", to be used in info like -help. */
  62     public abstract String getOwnName();
  63 
  64     /** Report an error. */
  65     abstract void error(String key, Object... args);
  66 
  67     /** Report an error. */
  68     abstract void error(JCDiagnostic.Error error);
  69 
  70     /** Record a file to be compiled. */
  71     abstract void addFile(Path p);
  72 
  73     /** Record the name of a class for annotation processing. */
  74     abstract void addClassName(String s);
  75 
  76     /** An implementation of OptionHelper that mostly throws exceptions. */
  77     public static class GrumpyHelper extends OptionHelper {
  78         private final Log log;
  79 
  80         public GrumpyHelper(Log log) {
  81             this.log = log;
  82         }
  83 
  84         @Override
  85         public Log getLog() {
  86             return log;
  87         }
  88 
  89         @Override
  90         public String getOwnName() {
  91             throw new IllegalStateException();
  92         }
  93 
  94         @Override
  95         public String get(Option option) {
  96             throw new IllegalArgumentException();
  97         }
  98 
  99         @Override
 100         public void put(String name, String value) {
 101             throw new IllegalArgumentException();
 102         }
 103 
 104         @Override
 105         public void remove(String name) {
 106             throw new IllegalArgumentException();
 107         }
 108 
 109         @Override
 110         public boolean handleFileManagerOption(Option option, String value) {
 111             throw new IllegalArgumentException();
 112         }
 113 
 114         @Override
 115         void error(String key, Object... args) {
 116             throw new IllegalArgumentException(log.localize(PrefixKind.JAVAC, key, args));
 117         }
 118 
 119         @Override
 120         void error(JCDiagnostic.Error error) {
 121             throw new IllegalArgumentException(log.localize(error));
 122         }
 123 
 124         @Override
 125         public void addFile(Path p) {
 126             throw new IllegalArgumentException(p.toString());
 127         }
 128 
 129         @Override
 130         public void addClassName(String s) {
 131             throw new IllegalArgumentException(s);
 132         }
 133     }
 134 
 135 }