T6457284.java revision 135
4680N/A/*
4680N/A * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
4680N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4680N/A *
4680N/A * This code is free software; you can redistribute it and/or modify it
4680N/A * under the terms of the GNU General Public License version 2 only, as
4680N/A * published by the Free Software Foundation.
4680N/A *
4680N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4680N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4680N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4680N/A * version 2 for more details (a copy is included in the LICENSE file that
4680N/A * accompanied this code).
4680N/A *
4680N/A * You should have received a copy of the GNU General Public License version
4680N/A * 2 along with this work; if not, write to the Free Software Foundation,
4680N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4680N/A *
4680N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
4680N/A * CA 95054 USA or visit www.sun.com if you need additional information or
4680N/A * have any questions.
4680N/A */
4680N/A
4680N/A/*
4680N/A * @test
4680N/A * @bug 6457284
4680N/A * @summary Internationalize "unnamed package" when the term is used in diagnostics
4680N/A * @author Peter von der Ah\u00e9
4680N/A */
4680N/A
4680N/Aimport java.io.IOException;
4680N/Aimport java.net.URI;
4680N/Aimport javax.lang.model.element.Element;
4680N/A
4680N/Aimport com.sun.tools.javac.api.JavacTaskImpl;
4680N/Aimport com.sun.tools.javac.util.Context;
4680N/Aimport com.sun.tools.javac.util.List;
4680N/Aimport com.sun.tools.javac.util.JavacMessages;
4680N/A
4680N/Aimport javax.tools.*;
4680N/A
4680N/Apublic class T6457284 {
4680N/A static class MyFileObject extends SimpleJavaFileObject {
4680N/A public MyFileObject() {
4680N/A super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
4680N/A }
4680N/A public CharSequence getCharContent(boolean ignoreEncodingErrors) {
4680N/A return "class Test {}";
4680N/A }
4680N/A }
4680N/A public static void main(String[] args) throws IOException {
4680N/A JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
4680N/A JavacTaskImpl task = (JavacTaskImpl)compiler.getTask(null, null, null, null, null,
4680N/A List.of(new MyFileObject()));
4680N/A MyMessages.preRegister(task.getContext());
4680N/A task.parse();
4680N/A for (Element e : task.analyze()) {
4680N/A if (!e.getEnclosingElement().toString().equals("compiler.misc.unnamed.package"))
4680N/A throw new AssertionError(e.getEnclosingElement());
4680N/A System.out.println("OK: " + e.getEnclosingElement());
4680N/A return;
4680N/A }
4680N/A throw new AssertionError("No top-level classes!");
4680N/A }
4680N/A
4680N/A static class MyMessages extends JavacMessages {
4680N/A static void preRegister(Context context) {
4680N/A context.put(messagesKey, new MyMessages());
4680N/A }
4680N/A MyMessages() {
4680N/A super("com.sun.tools.javac.resources.compiler");
4680N/A }
4680N/A public String getLocalizedString(String key, Object... args) {
4680N/A if (key.equals("compiler.misc.unnamed.package"))
4680N/A return key;
4680N/A else
4680N/A return super.getLocalizedString(key, args);
4680N/A }
4680N/A }
4680N/A}
4680N/A