/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * Test that module-info can be used to name the .class file * for a module declaration (i.e. ACC_MODULE, this_class == 0) * This is the primary test case for the bug as reported.
*/
@Test publicvoid testStandardModuleInfo(Path base) throws Exception {
Path src = base.resolve("src");
Path classes = Files.createDirectories(base.resolve("classes"));
tb.writeJavaFiles(src, "module m { }");
new JavacTask(tb)
.outdir(classes.toString())
.files(tb.findJavaFiles(src))
.run()
.writeAll();
/** * Test module-info can still be used to find a weirdly named * class equivalent to "class module-info { }" if that were legal in JLS. * Such a class file would arguably be legal in certain selected contexts.
*/
@Test publicvoid testLegacyModuleInfo(Path base) throws Exception {
Path src = base.resolve("src");
Path classes = Files.createDirectories(base.resolve("classes"));
tb.writeJavaFiles(src, "class module_info { }");
new JavacTask(tb)
.outdir(classes.toString())
.files(tb.findJavaFiles(src))
.run()
.writeAll();
byte[] bytes = Files.readAllBytes(classes.resolve("module_info.class")); byte[] searchBytes = "module_info".getBytes("UTF-8"); byte[] replaceBytes = "module-info".getBytes("UTF-8"); for (int i = 0; i < bytes.length - searchBytes.length; i++) { if (Arrays.equals(bytes, i, i + searchBytes.length,
searchBytes, 0, searchBytes.length)) {
System.arraycopy(replaceBytes, 0, bytes, i, replaceBytes.length);
}
}
Files.write(classes.resolve("module-info.class"), bytes);
List<String> log = new JavapTask(tb)
.classpath(classes.toString())
.options("-bootclasspath", "") // hide all system classes
.classes("module-info")
.run()
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> log = new JavapTask(tb)
.classpath(classes.toString())
.classes("Z")
.run()
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
checkOutput(log, true, "Warning:.*Z.class does not contain class Z");
}
/** * Test a class with unexpected contents. * This is the arguably the most common negative case.
*/
@Test publicvoid testWrongNameClass(Path base) throws Exception {
Path src = base.resolve("src");
Path classes = Files.createDirectories(base.resolve("classes"));
tb.writeJavaFiles(src, "class A { }");
new JavacTask(tb)
.outdir(classes.toString())
.files(tb.findJavaFiles(src))
.run()
.writeAll();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.