T6457284.java revision 173:fdfed22db054
1/* 2 * Copyright 2006-2008 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24/* 25 * @test 26 * @bug 6457284 27 * @summary Internationalize "unnamed package" when the term is used in diagnostics 28 * @author Peter von der Ah\u00e9 29 */ 30 31import java.io.IOException; 32import java.net.URI; 33import javax.lang.model.element.Element; 34 35import com.sun.tools.javac.api.JavacTaskImpl; 36import com.sun.tools.javac.util.Context; 37import com.sun.tools.javac.util.List; 38import com.sun.tools.javac.util.JavacMessages; 39 40import javax.tools.*; 41 42public class T6457284 { 43 static class MyFileObject extends SimpleJavaFileObject { 44 public MyFileObject() { 45 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 46 } 47 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 48 return "class Test {}"; 49 } 50 } 51 public static void main(String[] args) throws IOException { 52 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 53 JavacTaskImpl task = (JavacTaskImpl)compiler.getTask(null, null, null, null, null, 54 List.of(new MyFileObject())); 55 MyMessages.preRegister(task.getContext()); 56 task.parse(); 57 for (Element e : task.analyze()) { 58 if (!e.getEnclosingElement().toString().equals("compiler.misc.unnamed.package")) 59 throw new AssertionError(e.getEnclosingElement()); 60 System.out.println("OK: " + e.getEnclosingElement()); 61 return; 62 } 63 throw new AssertionError("No top-level classes!"); 64 } 65 66 static class MyMessages extends JavacMessages { 67 static void preRegister(Context context) { 68 context.put(messagesKey, new MyMessages()); 69 } 70 MyMessages() { 71 super("com.sun.tools.javac.resources.compiler"); 72 } 73 public String getLocalizedString(String key, Object... args) { 74 if (key.equals("compiler.misc.unnamed.package")) 75 return key; 76 else 77 return super.getLocalizedString(key, args); 78 } 79 } 80} 81