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