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

changeset 416
c287d51c57da
parent 1
9a66ca7c79fa
child 554
9d9f26857129
     1.1 --- a/src/share/classes/com/sun/tools/javah/Util.java	Wed Sep 23 18:48:13 2009 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javah/Util.java	Wed Sep 23 19:15:04 2009 -0700
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright 2002-2004 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * Copyright 2002-2008 Sun Microsystems, Inc.  All Rights Reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -26,12 +26,15 @@
    1.11  
    1.12  package com.sun.tools.javah;
    1.13  
    1.14 -import java.io.File;
    1.15 -import java.io.FileInputStream;
    1.16 -import java.io.IOException;
    1.17 +import java.io.PrintWriter;
    1.18 +import java.text.MessageFormat;
    1.19 +import java.util.Locale;
    1.20  import java.util.ResourceBundle;
    1.21 -import java.text.MessageFormat;
    1.22  import java.util.MissingResourceException;
    1.23 +import javax.tools.Diagnostic;
    1.24 +import javax.tools.Diagnostic.Kind;
    1.25 +import javax.tools.DiagnosticListener;
    1.26 +import javax.tools.JavaFileObject;
    1.27  
    1.28  /**
    1.29   * Messages, verbose and error handling support.
    1.30 @@ -41,42 +44,70 @@
    1.31   *      bug   -- Bug has occurred in javah
    1.32   *      fatal -- We can't even find resources, so bail fast, don't localize
    1.33   *
    1.34 + * <p><b>This is NOT part of any API supported by Sun Microsystems.
    1.35 + * If you write code that depends on this, you do so at your own
    1.36 + * risk.  This code and its internal interfaces are subject to change
    1.37 + * or deletion without notice.</b></p>
    1.38   */
    1.39  public class Util {
    1.40 +    /** Exit is used to replace the use of System.exit in the original javah.
    1.41 +     */
    1.42 +    public static class Exit extends Error {
    1.43 +        private static final long serialVersionUID = 430820978114067221L;
    1.44 +        Exit(int exitValue) {
    1.45 +            this(exitValue, null);
    1.46 +        }
    1.47 +
    1.48 +        Exit(int exitValue, Throwable cause) {
    1.49 +            super(cause);
    1.50 +            this.exitValue = exitValue;
    1.51 +            this.cause = cause;
    1.52 +        }
    1.53 +
    1.54 +        Exit(Exit e) {
    1.55 +            this(e.exitValue, e.cause);
    1.56 +        }
    1.57 +
    1.58 +        public final int exitValue;
    1.59 +        public final Throwable cause;
    1.60 +    }
    1.61  
    1.62      /*
    1.63       * Help for verbosity.
    1.64       */
    1.65 -    public static boolean verbose = false;
    1.66 +    public boolean verbose = false;
    1.67  
    1.68 -    public static void log(String s) {
    1.69 -        System.out.println(s);
    1.70 +    public PrintWriter log;
    1.71 +    public DiagnosticListener<? super JavaFileObject> dl;
    1.72 +
    1.73 +    Util(PrintWriter log, DiagnosticListener<? super JavaFileObject> dl) {
    1.74 +        this.log = log;
    1.75 +        this.dl = dl;
    1.76 +    }
    1.77 +
    1.78 +    public void log(String s) {
    1.79 +        log.println(s);
    1.80      }
    1.81  
    1.82  
    1.83      /*
    1.84       * Help for loading localized messages.
    1.85       */
    1.86 -    private static ResourceBundle m;
    1.87 +    private ResourceBundle m;
    1.88  
    1.89 -    private static void initMessages() {
    1.90 +    private void initMessages() throws Exit {
    1.91          try {
    1.92 -            m=ResourceBundle.getBundle("com.sun.tools.javah.resources.l10n");
    1.93 +            m = ResourceBundle.getBundle("com.sun.tools.javah.resources.l10n");
    1.94          } catch (MissingResourceException mre) {
    1.95              fatal("Error loading resources.  Please file a bug report.", mre);
    1.96          }
    1.97      }
    1.98  
    1.99 -    public static String getText(String key) {
   1.100 -        return getText(key, null, null);
   1.101 -    }
   1.102 -
   1.103 -    private static String getText(String key, String a1, String a2){
   1.104 +    private String getText(String key, Object... args) throws Exit {
   1.105          if (m == null)
   1.106              initMessages();
   1.107          try {
   1.108 -            return MessageFormat.format(m.getString(key),
   1.109 -                                        new Object[] { a1, a2 });
   1.110 +            return MessageFormat.format(m.getString(key), args);
   1.111          } catch (MissingResourceException e) {
   1.112              fatal("Key " + key + " not found in resources.", e);
   1.113          }
   1.114 @@ -86,107 +117,74 @@
   1.115      /*
   1.116       * Usage message.
   1.117       */
   1.118 -    public static void usage(int exitValue) {
   1.119 -        if (exitValue == 0) {
   1.120 -            System.out.println(getText("usage"));
   1.121 -        } else {
   1.122 -            System.err.println(getText("usage"));
   1.123 -        }
   1.124 -        System.exit(exitValue);
   1.125 +    public void usage() throws Exit {
   1.126 +        log.println(getText("usage"));
   1.127      }
   1.128  
   1.129 -    public static void version() {
   1.130 -        System.out.println(getText("javah.version",
   1.131 +    public void version() throws Exit {
   1.132 +        log.println(getText("javah.version",
   1.133                                     System.getProperty("java.version"), null));
   1.134 -        System.exit(0);
   1.135      }
   1.136  
   1.137      /*
   1.138       * Failure modes.
   1.139       */
   1.140 -    public static void bug(String key) {
   1.141 +    public void bug(String key) throws Exit {
   1.142          bug(key, null);
   1.143      }
   1.144  
   1.145 -    public static void bug(String key, Exception e) {
   1.146 -        if (e != null)
   1.147 -            e.printStackTrace();
   1.148 -        System.err.println(getText(key));
   1.149 -        System.err.println(getText("bug.report"));
   1.150 -        System.exit(11);
   1.151 +    public void bug(String key, Exception e) throws Exit {
   1.152 +        dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key));
   1.153 +        dl.report(createDiagnostic(Diagnostic.Kind.NOTE, "bug.report"));
   1.154 +        throw new Exit(11, e);
   1.155      }
   1.156  
   1.157 -    public static void error(String key) {
   1.158 -        error(key, null);
   1.159 +    public void error(String key, Object... args) throws Exit {
   1.160 +        dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key, args));
   1.161 +        throw new Exit(15);
   1.162      }
   1.163  
   1.164 -    public static void error(String key, String a1) {
   1.165 -        error(key, a1, null);
   1.166 -    }
   1.167 -
   1.168 -    public static void error(String key, String a1, String a2) {
   1.169 -        error(key, a1, a2, false);
   1.170 -    }
   1.171 -
   1.172 -    public static void error(String key, String a1, String a2,
   1.173 -                             boolean showUsage) {
   1.174 -        System.err.println("Error: " + getText(key, a1, a2));
   1.175 -        if (showUsage)
   1.176 -            usage(15);
   1.177 -        System.exit(15);
   1.178 -    }
   1.179 -
   1.180 -
   1.181 -    private static void fatal(String msg) {
   1.182 +    private void fatal(String msg) throws Exit {
   1.183          fatal(msg, null);
   1.184      }
   1.185  
   1.186 -    private static void fatal(String msg, Exception e) {
   1.187 -        if (e != null) {
   1.188 -            e.printStackTrace();
   1.189 -        }
   1.190 -        System.err.println(msg);
   1.191 -        System.exit(10);
   1.192 +    private void fatal(String msg, Exception e) throws Exit {
   1.193 +        dl.report(createDiagnostic(Diagnostic.Kind.ERROR, "", msg));
   1.194 +        throw new Exit(10, e);
   1.195      }
   1.196  
   1.197 -    /*
   1.198 -     * Support for platform specific things in javah, such as pragma
   1.199 -     * directives, exported symbols etc.
   1.200 -     */
   1.201 -    static private ResourceBundle platform = null;
   1.202 -
   1.203 -    /*
   1.204 -     * Set when platform has been initialized.
   1.205 -     */
   1.206 -    static private boolean platformInit = false;
   1.207 -
   1.208 -    static String getPlatformString(String key) {
   1.209 -        if (!platformInit) {
   1.210 -            initPlatform();
   1.211 -            platformInit = true;
   1.212 -        }
   1.213 -        if (platform == null)
   1.214 -            return null;
   1.215 -        try {
   1.216 -            return platform.getString(key);
   1.217 -        } catch (MissingResourceException mre) {
   1.218 -            return null;
   1.219 -        }
   1.220 -    }
   1.221 -
   1.222 -    private static void initPlatform() {
   1.223 -        String os = System.getProperty("os.name");
   1.224 -        if (os.startsWith("Windows")) {
   1.225 -            os = "win32";
   1.226 -        } else if (os.indexOf("Linux") >= 0) {
   1.227 -            os = "Linux";
   1.228 -        }
   1.229 -        String arch = System.getProperty("os.arch");
   1.230 -        String resname = "com.sun.tools.javah.resources." + os + "_" + arch;
   1.231 -        try {
   1.232 -            platform=ResourceBundle.getBundle(resname);
   1.233 -        } catch (MissingResourceException mre) {
   1.234 -            // fatal("Error loading resources.  Please file a bug report.", mre);
   1.235 -        }
   1.236 +    private Diagnostic<JavaFileObject> createDiagnostic(
   1.237 +            final Diagnostic.Kind kind, final String code, final Object... args) {
   1.238 +        return new Diagnostic<JavaFileObject>() {
   1.239 +            public String getCode() {
   1.240 +                return code;
   1.241 +            }
   1.242 +            public long getColumnNumber() {
   1.243 +                return Diagnostic.NOPOS;
   1.244 +            }
   1.245 +            public long getEndPosition() {
   1.246 +                return Diagnostic.NOPOS;
   1.247 +            }
   1.248 +            public Kind getKind() {
   1.249 +                return kind;
   1.250 +            }
   1.251 +            public long getLineNumber() {
   1.252 +                return Diagnostic.NOPOS;
   1.253 +            }
   1.254 +            public String getMessage(Locale locale) {
   1.255 +                if (code.length() == 0)
   1.256 +                    return (String) args[0];
   1.257 +                return getText(code, args); // FIXME locale
   1.258 +            }
   1.259 +            public long getPosition() {
   1.260 +                return Diagnostic.NOPOS;
   1.261 +            }
   1.262 +            public JavaFileObject getSource() {
   1.263 +                return null;
   1.264 +            }
   1.265 +            public long getStartPosition() {
   1.266 +                return Diagnostic.NOPOS;
   1.267 +            }
   1.268 +        };
   1.269      }
   1.270  }

mercurial