src/share/classes/com/sun/tools/javah/Util.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 581
f2fdd52e4e87
child 1648
a03c4a86ea2b
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2002, 2008, 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  */
    27 package com.sun.tools.javah;
    29 import java.io.PrintWriter;
    30 import java.text.MessageFormat;
    31 import java.util.Locale;
    32 import java.util.ResourceBundle;
    33 import java.util.MissingResourceException;
    34 import javax.tools.Diagnostic;
    35 import javax.tools.Diagnostic.Kind;
    36 import javax.tools.DiagnosticListener;
    37 import javax.tools.JavaFileObject;
    39 /**
    40  * Messages, verbose and error handling support.
    41  *
    42  * For errors, the failure modes are:
    43  *      error -- User did something wrong
    44  *      bug   -- Bug has occurred in javah
    45  *      fatal -- We can't even find resources, so bail fast, don't localize
    46  *
    47  * <p><b>This is NOT part of any supported API.
    48  * If you write code that depends on this, you do so at your own
    49  * risk.  This code and its internal interfaces are subject to change
    50  * or deletion without notice.</b></p>
    51  */
    52 public class Util {
    53     /** Exit is used to replace the use of System.exit in the original javah.
    54      */
    55     public static class Exit extends Error {
    56         private static final long serialVersionUID = 430820978114067221L;
    57         Exit(int exitValue) {
    58             this(exitValue, null);
    59         }
    61         Exit(int exitValue, Throwable cause) {
    62             super(cause);
    63             this.exitValue = exitValue;
    64             this.cause = cause;
    65         }
    67         Exit(Exit e) {
    68             this(e.exitValue, e.cause);
    69         }
    71         public final int exitValue;
    72         public final Throwable cause;
    73     }
    75     /*
    76      * Help for verbosity.
    77      */
    78     public boolean verbose = false;
    80     public PrintWriter log;
    81     public DiagnosticListener<? super JavaFileObject> dl;
    83     Util(PrintWriter log, DiagnosticListener<? super JavaFileObject> dl) {
    84         this.log = log;
    85         this.dl = dl;
    86     }
    88     public void log(String s) {
    89         log.println(s);
    90     }
    93     /*
    94      * Help for loading localized messages.
    95      */
    96     private ResourceBundle m;
    98     private void initMessages() throws Exit {
    99         try {
   100             m = ResourceBundle.getBundle("com.sun.tools.javah.resources.l10n");
   101         } catch (MissingResourceException mre) {
   102             fatal("Error loading resources.  Please file a bug report.", mre);
   103         }
   104     }
   106     private String getText(String key, Object... args) throws Exit {
   107         if (m == null)
   108             initMessages();
   109         try {
   110             return MessageFormat.format(m.getString(key), args);
   111         } catch (MissingResourceException e) {
   112             fatal("Key " + key + " not found in resources.", e);
   113         }
   114         return null; /* dead code */
   115     }
   117     /*
   118      * Usage message.
   119      */
   120     public void usage() throws Exit {
   121         log.println(getText("usage"));
   122     }
   124     public void version() throws Exit {
   125         log.println(getText("javah.version",
   126                                    System.getProperty("java.version"), null));
   127     }
   129     /*
   130      * Failure modes.
   131      */
   132     public void bug(String key) throws Exit {
   133         bug(key, null);
   134     }
   136     public void bug(String key, Exception e) throws Exit {
   137         dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key));
   138         dl.report(createDiagnostic(Diagnostic.Kind.NOTE, "bug.report"));
   139         throw new Exit(11, e);
   140     }
   142     public void error(String key, Object... args) throws Exit {
   143         dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key, args));
   144         throw new Exit(15);
   145     }
   147     private void fatal(String msg) throws Exit {
   148         fatal(msg, null);
   149     }
   151     private void fatal(String msg, Exception e) throws Exit {
   152         dl.report(createDiagnostic(Diagnostic.Kind.ERROR, "", msg));
   153         throw new Exit(10, e);
   154     }
   156     private Diagnostic<JavaFileObject> createDiagnostic(
   157             final Diagnostic.Kind kind, final String code, final Object... args) {
   158         return new Diagnostic<JavaFileObject>() {
   159             public String getCode() {
   160                 return code;
   161             }
   162             public long getColumnNumber() {
   163                 return Diagnostic.NOPOS;
   164             }
   165             public long getEndPosition() {
   166                 return Diagnostic.NOPOS;
   167             }
   168             public Kind getKind() {
   169                 return kind;
   170             }
   171             public long getLineNumber() {
   172                 return Diagnostic.NOPOS;
   173             }
   174             public String getMessage(Locale locale) {
   175                 if (code.length() == 0)
   176                     return (String) args[0];
   177                 return getText(code, args); // FIXME locale
   178             }
   179             public long getPosition() {
   180                 return Diagnostic.NOPOS;
   181             }
   182             public JavaFileObject getSource() {
   183                 return null;
   184             }
   185             public long getStartPosition() {
   186                 return Diagnostic.NOPOS;
   187             }
   188         };
   189     }
   190 }

mercurial