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

Wed, 05 Aug 2009 07:43:50 -0700

author
jjg
date
Wed, 05 Aug 2009 07:43:50 -0700
changeset 349
bc0b1f404c40
parent 1
9a66ca7c79fa
child 416
c287d51c57da
permissions
-rw-r--r--

6868553: 6867671 breaks some tests
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright 2002-2004 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    27 package com.sun.tools.javah;
    29 import java.io.File;
    30 import java.io.FileInputStream;
    31 import java.io.IOException;
    32 import java.util.ResourceBundle;
    33 import java.text.MessageFormat;
    34 import java.util.MissingResourceException;
    36 /**
    37  * Messages, verbose and error handling support.
    38  *
    39  * For errors, the failure modes are:
    40  *      error -- User did something wrong
    41  *      bug   -- Bug has occurred in javah
    42  *      fatal -- We can't even find resources, so bail fast, don't localize
    43  *
    44  */
    45 public class Util {
    47     /*
    48      * Help for verbosity.
    49      */
    50     public static boolean verbose = false;
    52     public static void log(String s) {
    53         System.out.println(s);
    54     }
    57     /*
    58      * Help for loading localized messages.
    59      */
    60     private static ResourceBundle m;
    62     private static void initMessages() {
    63         try {
    64             m=ResourceBundle.getBundle("com.sun.tools.javah.resources.l10n");
    65         } catch (MissingResourceException mre) {
    66             fatal("Error loading resources.  Please file a bug report.", mre);
    67         }
    68     }
    70     public static String getText(String key) {
    71         return getText(key, null, null);
    72     }
    74     private static String getText(String key, String a1, String a2){
    75         if (m == null)
    76             initMessages();
    77         try {
    78             return MessageFormat.format(m.getString(key),
    79                                         new Object[] { a1, a2 });
    80         } catch (MissingResourceException e) {
    81             fatal("Key " + key + " not found in resources.", e);
    82         }
    83         return null; /* dead code */
    84     }
    86     /*
    87      * Usage message.
    88      */
    89     public static void usage(int exitValue) {
    90         if (exitValue == 0) {
    91             System.out.println(getText("usage"));
    92         } else {
    93             System.err.println(getText("usage"));
    94         }
    95         System.exit(exitValue);
    96     }
    98     public static void version() {
    99         System.out.println(getText("javah.version",
   100                                    System.getProperty("java.version"), null));
   101         System.exit(0);
   102     }
   104     /*
   105      * Failure modes.
   106      */
   107     public static void bug(String key) {
   108         bug(key, null);
   109     }
   111     public static void bug(String key, Exception e) {
   112         if (e != null)
   113             e.printStackTrace();
   114         System.err.println(getText(key));
   115         System.err.println(getText("bug.report"));
   116         System.exit(11);
   117     }
   119     public static void error(String key) {
   120         error(key, null);
   121     }
   123     public static void error(String key, String a1) {
   124         error(key, a1, null);
   125     }
   127     public static void error(String key, String a1, String a2) {
   128         error(key, a1, a2, false);
   129     }
   131     public static void error(String key, String a1, String a2,
   132                              boolean showUsage) {
   133         System.err.println("Error: " + getText(key, a1, a2));
   134         if (showUsage)
   135             usage(15);
   136         System.exit(15);
   137     }
   140     private static void fatal(String msg) {
   141         fatal(msg, null);
   142     }
   144     private static void fatal(String msg, Exception e) {
   145         if (e != null) {
   146             e.printStackTrace();
   147         }
   148         System.err.println(msg);
   149         System.exit(10);
   150     }
   152     /*
   153      * Support for platform specific things in javah, such as pragma
   154      * directives, exported symbols etc.
   155      */
   156     static private ResourceBundle platform = null;
   158     /*
   159      * Set when platform has been initialized.
   160      */
   161     static private boolean platformInit = false;
   163     static String getPlatformString(String key) {
   164         if (!platformInit) {
   165             initPlatform();
   166             platformInit = true;
   167         }
   168         if (platform == null)
   169             return null;
   170         try {
   171             return platform.getString(key);
   172         } catch (MissingResourceException mre) {
   173             return null;
   174         }
   175     }
   177     private static void initPlatform() {
   178         String os = System.getProperty("os.name");
   179         if (os.startsWith("Windows")) {
   180             os = "win32";
   181         } else if (os.indexOf("Linux") >= 0) {
   182             os = "Linux";
   183         }
   184         String arch = System.getProperty("os.arch");
   185         String resname = "com.sun.tools.javah.resources." + os + "_" + arch;
   186         try {
   187             platform=ResourceBundle.getBundle(resname);
   188         } catch (MissingResourceException mre) {
   189             // fatal("Error loading resources.  Please file a bug report.", mre);
   190         }
   191     }
   192 }

mercurial