Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6457284/T6457284.java
38813 views
/*1* Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 645728426* @summary Internationalize "unnamed package" when the term is used in diagnostics27* @author Peter von der Ah\u00e928*/2930import java.io.IOException;31import java.net.URI;32import javax.lang.model.element.Element;3334import com.sun.tools.javac.api.JavacTaskImpl;35import com.sun.tools.javac.util.Context;36import com.sun.tools.javac.util.List;37import com.sun.tools.javac.util.JavacMessages;3839import javax.tools.*;4041public class T6457284 {42static class MyFileObject extends SimpleJavaFileObject {43public MyFileObject() {44super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);45}46public CharSequence getCharContent(boolean ignoreEncodingErrors) {47return "class Test {}";48}49}50public static void main(String[] args) throws IOException {51JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();52JavacTaskImpl task = (JavacTaskImpl)compiler.getTask(null, null, null, null, null,53List.of(new MyFileObject()));54MyMessages.preRegister(task.getContext());55task.parse();56for (Element e : task.analyze()) {57if (!e.getEnclosingElement().toString().equals("compiler.misc.unnamed.package"))58throw new AssertionError(e.getEnclosingElement());59System.out.println("OK: " + e.getEnclosingElement());60return;61}62throw new AssertionError("No top-level classes!");63}6465static class MyMessages extends JavacMessages {66static void preRegister(Context context) {67context.put(messagesKey, new MyMessages());68}69MyMessages() {70super("com.sun.tools.javac.resources.compiler");71}72public String getLocalizedString(String key, Object... args) {73if (key.equals("compiler.misc.unnamed.package"))74return key;75else76return super.getLocalizedString(key, args);77}78}79}808182