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