T6457284.java revision 2599:50b448c5be54
1/* 2 * Copyright (c) 2006, 2014, 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. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * 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.source.util.JavacTask; 36import com.sun.tools.javac.api.JavacTool; 37import com.sun.tools.javac.util.Context; 38import com.sun.tools.javac.util.List; 39import com.sun.tools.javac.util.JavacMessages; 40 41import javax.tools.*; 42 43public class T6457284 { 44 static class MyFileObject extends SimpleJavaFileObject { 45 public MyFileObject() { 46 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 47 } 48 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 49 return "class Test {}"; 50 } 51 } 52 public static void main(String[] args) throws IOException { 53 Context context = new Context(); 54 MyMessages.preRegister(context); 55 JavacTool tool = JavacTool.create(); 56 JavacTask task = tool.getTask(null, null, null, null, null, 57 List.of(new MyFileObject()), 58 context); 59 task.parse(); 60 for (Element e : task.analyze()) { 61 if (!e.getEnclosingElement().toString().equals("compiler.misc.unnamed.package")) 62 throw new AssertionError(e.getEnclosingElement()); 63 System.out.println("OK: " + e.getEnclosingElement()); 64 return; 65 } 66 throw new AssertionError("No top-level classes!"); 67 } 68 69 static class MyMessages extends JavacMessages { 70 static void preRegister(Context context) { 71 context.put(messagesKey, new MyMessages()); 72 } 73 MyMessages() { 74 super("com.sun.tools.javac.resources.compiler"); 75 } 76 public String getLocalizedString(String key, Object... args) { 77 if (key.equals("compiler.misc.unnamed.package")) 78 return key; 79 else 80 return super.getLocalizedString(key, args); 81 } 82 } 83} 84