Path: blob/master/test/langtools/tools/javac/6457284/T6457284.java
41149 views
/*1* Copyright (c) 2006, 2015, 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* @modules jdk.compiler/com.sun.tools.javac.api29* jdk.compiler/com.sun.tools.javac.util30*/3132import java.io.IOException;33import java.net.URI;34import javax.lang.model.element.Element;3536import com.sun.source.util.JavacTask;37import com.sun.tools.javac.api.JavacTool;38import com.sun.tools.javac.util.Context;39import com.sun.tools.javac.util.List;40import com.sun.tools.javac.util.JavacMessages;4142import javax.tools.*;4344public class T6457284 {45static class MyFileObject extends SimpleJavaFileObject {46public MyFileObject() {47super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);48}49public CharSequence getCharContent(boolean ignoreEncodingErrors) {50return "class Test {}";51}52}53public static void main(String[] args) throws IOException {54Context context = new Context();55MyMessages.preRegister(context);56JavacTool tool = JavacTool.create();57JavacTask task = tool.getTask(null, null, null, null, null,58List.of(new MyFileObject()),59context);60task.parse();61for (Element e : task.analyze()) {62if (!e.getEnclosingElement().toString().equals("compiler.misc.unnamed.package"))63throw new AssertionError(e.getEnclosingElement());64System.out.println("OK: " + e.getEnclosingElement());65return;66}67throw new AssertionError("No top-level classes!");68}6970static class MyMessages extends JavacMessages {71static void preRegister(Context context) {72context.put(messagesKey, new MyMessages());73}74MyMessages() {75super("com.sun.tools.javac.resources.compiler");76}77public String getLocalizedString(String key, Object... args) {78if (key.equals("compiler.misc.unnamed.package"))79return key;80else81return super.getLocalizedString(key, args);82}83}84}858687