Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
1 /*
2 * Copyright (c) 2006, 2008, 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 */
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 */
31 import java.io.IOException;
32 import java.net.URI;
33 import javax.lang.model.element.Element;
35 import com.sun.tools.javac.api.JavacTaskImpl;
36 import com.sun.tools.javac.util.Context;
37 import com.sun.tools.javac.util.List;
38 import com.sun.tools.javac.util.JavacMessages;
40 import javax.tools.*;
42 public 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 }
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 }