|
Java example source code file (JavahTask.java)
The JavahTask.java Java example source code/* * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.tools.javah; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.Set; import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.Messager; import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.SupportedAnnotationTypes; import javax.lang.model.SourceVersion; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; import javax.lang.model.type.ArrayType; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeVisitor; import javax.lang.model.util.ElementFilter; import javax.lang.model.util.SimpleTypeVisitor8; import javax.lang.model.util.Types; import javax.tools.Diagnostic; import javax.tools.DiagnosticListener; import javax.tools.JavaCompiler; import javax.tools.JavaCompiler.CompilationTask; import javax.tools.JavaFileManager; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.StandardLocation; import javax.tools.ToolProvider; import static javax.tools.Diagnostic.Kind.*; import com.sun.tools.javac.code.Symbol.CompletionFailure; import com.sun.tools.javac.main.CommandLine; /** * Javah generates support files for native methods. * Parse commandline options and invokes javadoc to execute those commands. * * <p>This is NOT part of any supported API. * If you write code that depends on this, you do so at your own * risk. This code and its internal interfaces are subject to change * or deletion without notice.</b> * * @author Sucheta Dambalkar * @author Jonathan Gibbons */ public class JavahTask implements NativeHeaderTool.NativeHeaderTask { public class BadArgs extends Exception { private static final long serialVersionUID = 1479361270874789045L; BadArgs(String key, Object... args) { super(JavahTask.this.getMessage(key, args)); this.key = key; this.args = args; } BadArgs showUsage(boolean b) { showUsage = b; return this; } final String key; final Object[] args; boolean showUsage; } static abstract class Option { Option(boolean hasArg, String... aliases) { this.hasArg = hasArg; this.aliases = aliases; } boolean isHidden() { return false; } boolean matches(String opt) { for (String a: aliases) { if (a.equals(opt)) return true; } return false; } boolean ignoreRest() { return false; } abstract void process(JavahTask task, String opt, String arg) throws BadArgs; final boolean hasArg; final String[] aliases; } static abstract class HiddenOption extends Option { HiddenOption(boolean hasArg, String... aliases) { super(hasArg, aliases); } @Override boolean isHidden() { return true; } } static final Option[] recognizedOptions = { new Option(true, "-o") { void process(JavahTask task, String opt, String arg) { task.ofile = new File(arg); } }, new Option(true, "-d") { void process(JavahTask task, String opt, String arg) { task.odir = new File(arg); } }, new HiddenOption(true, "-td") { void process(JavahTask task, String opt, String arg) { // ignored; for backwards compatibility } }, new HiddenOption(false, "-stubs") { void process(JavahTask task, String opt, String arg) { // ignored; for backwards compatibility } }, new Option(false, "-v", "-verbose") { void process(JavahTask task, String opt, String arg) { task.verbose = true; } }, new Option(false, "-h", "-help", "--help", "-?") { void process(JavahTask task, String opt, String arg) { task.help = true; } }, new HiddenOption(false, "-trace") { void process(JavahTask task, String opt, String arg) { task.trace = true; } }, new Option(false, "-version") { void process(JavahTask task, String opt, String arg) { task.version = true; } }, new HiddenOption(false, "-fullversion") { void process(JavahTask task, String opt, String arg) { task.fullVersion = true; } }, new Option(false, "-jni") { void process(JavahTask task, String opt, String arg) { task.jni = true; } }, new Option(false, "-force") { void process(JavahTask task, String opt, String arg) { task.force = true; } }, new HiddenOption(false, "-Xnew") { void process(JavahTask task, String opt, String arg) { // we're already using the new javah } }, new HiddenOption(false, "-llni", "-Xllni") { void process(JavahTask task, String opt, String arg) { task.llni = true; } }, new HiddenOption(false, "-llnidouble") { void process(JavahTask task, String opt, String arg) { task.llni = true; task.doubleAlign = true; } }, new HiddenOption(false) { boolean matches(String opt) { return opt.startsWith("-XD"); } void process(JavahTask task, String opt, String arg) { task.javac_extras.add(opt); } }, }; JavahTask() { } JavahTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes) { this(); this.log = getPrintWriterForWriter(out); this.fileManager = fileManager; this.diagnosticListener = diagnosticListener; try { handleOptions(options, false); } catch (BadArgs e) { throw new IllegalArgumentException(e.getMessage()); } this.classes = new ArrayList<String>(); if (classes != null) { for (String classname: classes) { classname.getClass(); // null-check this.classes.add(classname); } } } public void setLocale(Locale locale) { if (locale == null) locale = Locale.getDefault(); task_locale = locale; } public void setLog(PrintWriter log) { this.log = log; } public void setLog(OutputStream s) { setLog(getPrintWriterForStream(s)); } static PrintWriter getPrintWriterForStream(OutputStream s) { return new PrintWriter(s, true); } static PrintWriter getPrintWriterForWriter(Writer w) { if (w == null) return getPrintWriterForStream(null); else if (w instanceof PrintWriter) return (PrintWriter) w; else return new PrintWriter(w, true); } public void setDiagnosticListener(DiagnosticListener<? super JavaFileObject> dl) { diagnosticListener = dl; } public void setDiagnosticListener(OutputStream s) { setDiagnosticListener(getDiagnosticListenerForStream(s)); } private DiagnosticListener<JavaFileObject> getDiagnosticListenerForStream(OutputStream s) { return getDiagnosticListenerForWriter(getPrintWriterForStream(s)); } private DiagnosticListener<JavaFileObject> getDiagnosticListenerForWriter(Writer w) { final PrintWriter pw = getPrintWriterForWriter(w); return new DiagnosticListener<JavaFileObject> () { public void report(Diagnostic<? extends JavaFileObject> diagnostic) { if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { pw.print(getMessage("err.prefix")); pw.print(" "); } pw.println(diagnostic.getMessage(null)); } }; } int run(String[] args) { try { handleOptions(args); boolean ok = run(); return ok ? 0 : 1; } catch (BadArgs e) { diagnosticListener.report(createDiagnostic(e.key, e.args)); return 1; } catch (InternalError e) { diagnosticListener.report(createDiagnostic("err.internal.error", e.getMessage())); return 1; } catch (Util.Exit e) { return e.exitValue; } finally { log.flush(); } } public void handleOptions(String[] args) throws BadArgs { handleOptions(Arrays.asList(args), true); } private void handleOptions(Iterable<String> args, boolean allowClasses) throws BadArgs { if (log == null) { log = getPrintWriterForStream(System.out); if (diagnosticListener == null) diagnosticListener = getDiagnosticListenerForStream(System.err); } else { if (diagnosticListener == null) diagnosticListener = getDiagnosticListenerForWriter(log); } if (fileManager == null) fileManager = getDefaultFileManager(diagnosticListener, log); Iterator<String> iter = expandAtArgs(args).iterator(); noArgs = !iter.hasNext(); while (iter.hasNext()) { String arg = iter.next(); if (arg.startsWith("-")) handleOption(arg, iter); else if (allowClasses) { if (classes == null) classes = new ArrayList<String>(); classes.add(arg); while (iter.hasNext()) classes.add(iter.next()); } else throw new BadArgs("err.unknown.option", arg).showUsage(true); } if ((classes == null || classes.size() == 0) && !(noArgs || help || version || fullVersion)) { throw new BadArgs("err.no.classes.specified"); } if (jni && llni) throw new BadArgs("jni.llni.mixed"); if (odir != null && ofile != null) throw new BadArgs("dir.file.mixed"); } private void handleOption(String name, Iterator<String> rest) throws BadArgs { for (Option o: recognizedOptions) { if (o.matches(name)) { if (o.hasArg) { if (rest.hasNext()) o.process(this, name, rest.next()); else throw new BadArgs("err.missing.arg", name).showUsage(true); } else o.process(this, name, null); if (o.ignoreRest()) { while (rest.hasNext()) rest.next(); } return; } } if (fileManager.handleOption(name, rest)) return; throw new BadArgs("err.unknown.option", name).showUsage(true); } private Iterable<String> expandAtArgs(Iterable Other Java examples (source code examples)Here is a short list of links related to this Java JavahTask.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.