1 /*
   2  * Copyright (c) 2009, 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.javah;
  27 
  28 import java.io.InputStream;
  29 import java.io.OutputStream;
  30 import java.io.Writer;
  31 import java.nio.charset.Charset;
  32 import java.util.Arrays;
  33 import java.util.EnumSet;
  34 import java.util.Locale;
  35 import java.util.Set;
  36 import javax.lang.model.SourceVersion;
  37 import javax.tools.DiagnosticListener;
  38 import javax.tools.JavaFileManager;
  39 import javax.tools.JavaFileObject;
  40 import javax.tools.StandardJavaFileManager;
  41 
  42 import com.sun.tools.javac.util.DefinedBy;
  43 import com.sun.tools.javac.util.DefinedBy.Api;
  44 
  45 /*
  46  * <p><b>This is NOT part of any supported API.
  47  * If you write code that depends on this, you do so at your own
  48  * risk.  This code and its internal interfaces are subject to change
  49  * or deletion without notice.</b></p>
  50  */
  51 public class JavahTool implements NativeHeaderTool {
  52 
  53     public NativeHeaderTask getTask(Writer out,
  54             JavaFileManager fileManager,
  55             DiagnosticListener<? super JavaFileObject> diagnosticListener,
  56             Iterable<String> options,
  57             Iterable<String> classes) {
  58         return new JavahTask(out, fileManager, diagnosticListener, options, classes);
  59     }
  60 
  61     public StandardJavaFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> diagnosticListener, Locale locale, Charset charset) {
  62         return JavahTask.getDefaultFileManager(diagnosticListener, null);
  63     }
  64 
  65     @DefinedBy(Api.COMPILER)
  66     public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
  67         JavahTask t = new JavahTask(
  68                 JavahTask.getPrintWriterForStream(out),
  69                 null,
  70                 null,
  71                 Arrays.asList(arguments),
  72                 null);
  73         return (t.run() ? 0 : 1);
  74     }
  75 
  76     @DefinedBy(Api.COMPILER)
  77     public Set<SourceVersion> getSourceVersions() {
  78         return EnumSet.allOf(SourceVersion.class);
  79     }
  80 
  81     @DefinedBy(Api.COMPILER)
  82     public int isSupportedOption(String option) {
  83         for (JavahTask.Option opt : JavahTask.recognizedOptions) {
  84             if (opt.matches(option))
  85                 return (opt.hasArg ? 1 : 0);
  86         }
  87         return -1;
  88     }
  89 }