/* * Copyright (c) 2014, 2022, 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.
*/
/* * If not explicitly specified the latest source and latest target * values are the defaults. If explicitly specified, the target value * has to be greater than or equal to the source value.
*/ publicclass Versions {
// Verify that a -source value less than a -target value is // accepted and that the resulting class files are dependent // on the target setting alone.
SourceTarget[] sourceTargets = SourceTarget.values(); for (int i = 0; i < sourceTargets.length; i++) {
SourceTarget st = sourceTargets[i];
String classFileVer = st.classFileVer();
String target = st.target(); boolean dotOne = st.dotOne();
check_source_target(dotOne, List.of(classFileVer, target, target)); for (int j = i - 1; j >= 0; j--) {
String source = sourceTargets[j].target();
check_source_target(dotOne, List.of(classFileVer, source, target));
}
}
// Verify acceptance of different combinations of -source N, // -target M; N <= M for (int i = 0; i < sourceTargets.length; i++) {
SourceTarget st = sourceTargets[i];
if (i == sourceTargets.length - 1) { // Can use -target without -source setting only for // most recent target since the most recent source is // the default.
st.checksrc(this, List.of("-target " + st.target()));
if (!st.classFileVer().equals(LATEST_MAJOR_VERSION)) { thrownew RuntimeException(st + "does not have class file version" +
LATEST_MAJOR_VERSION);
}
}
}
// Verify that -source N -target (N-1) is rejected for (int i = 1 /* Skip zeroth value */; i < sourceTargets.length; i++) {
fail(List.of("-source " + sourceTargets[i].target(), "-target " + sourceTargets[i-1].target(), "Base.java"));
}
// add in args conforming to List requrements of JavaCompiler for (String onearg : args) {
String[] fields = onearg.split(" "); for (String onefield : fields) {
jcargs.add(onefield);
}
}
boolean creturn = compile("Base.java", jcargs); if (!creturn) { // compilation errors note and return.. assume no class file
System.err.println("check: Compilation Failed");
System.err.println("\t classVersion:\t" + major);
System.err.println("\t arguments:\t" + jcargs);
failedCases++;
protectedvoid checksrc17(List<String> args) {
printargs("checksrc17", args);
expectedPass(args, List.of("New7.java", "New8.java", "New10.java", "New11.java", "New14.java", "New15.java", "New16.java", "New17.java")); // Add expectedFail after new language features added in a later release.
}
protectedvoid checksrc18(List<String> args) {
printargs("checksrc18", args);
expectedPass(args, List.of("New7.java", "New8.java", "New10.java", "New11.java", "New14.java", "New15.java", "New16.java", "New17.java")); // Add expectedFail after new language features added in a later release.
}
protectedvoid checksrc19(List<String> args) {
printargs("checksrc19", args);
expectedPass(args, List.of("New7.java", "New8.java", "New10.java", "New11.java", "New14.java", "New15.java", "New16.java", "New17.java")); // Add expectedFail after new language features added in a later release.
}
protectedvoid checksrc20(List<String> args) {
printargs("checksrc20", args);
expectedPass(args, List.of("New7.java", "New8.java", "New10.java", "New11.java", "New14.java", "New15.java", "New16.java", "New17.java")); // Add expectedFail after new language features added in a later release.
}
protectedvoid expected(List<String> args, List<String> fileNames,
Consumer<List<String>> passOrFail) {
ArrayList<String> fullArguments = new ArrayList<>(args); // Issue compile with each filename in turn. for(String fileName : fileNames) {
fullArguments.add(fileName);
passOrFail.accept(fullArguments);
fullArguments.remove(fullArguments.size() - 1);
}
}
List<String> jcargs = new ArrayList<>();
jcargs.add("-Xlint:-options");
// add in args conforming to List requrements of JavaCompiler for (String onearg : args) {
String[] fields = onearg.split(" "); for (String onefield : fields) {
jcargs.add(onefield);
}
}
// empty list is error if (jcargs.isEmpty()) {
System.err.println("error: test error in pass() - No arguments");
System.err.println("\t arguments:\t" + jcargs);
failedCases++; return;
}
// the last argument is the filename *.java
String filename = jcargs.get(jcargs.size() - 1);
jcargs.remove(jcargs.size() - 1);
boolean creturn = compile(filename, jcargs); // expect a compilation failure, failure if otherwise if (!creturn) {
System.err.println("pass: Compilation erroneously failed");
System.err.println("\t arguments:\t" + jcargs);
System.err.println("\t file :\t" + filename);
failedCases++;
List<String> jcargs = new ArrayList<>();
jcargs.add("-Xlint:-options");
// add in args conforming to List requrements of JavaCompiler for (String onearg : args) {
String[] fields = onearg.split(" "); for (String onefield : fields) {
jcargs.add(onefield);
}
}
// empty list is error if (jcargs.isEmpty()) {
System.err.println("error: test error in fail()- No arguments");
System.err.println("\t arguments:\t" + jcargs);
failedCases++; return;
}
// the last argument is the filename *.java
String filename = jcargs.get(jcargs.size() - 1);
jcargs.remove(jcargs.size() - 1);
protectedvoid genSourceFiles() throws IOException{ /* Create a file that executes with all supported versions. */
writeSourceFile("Base.java","public class Base { }\n");
/* * Create a file with a new feature in 7, not in 6 : "<>"
*/
writeSourceFile("New7.java", """ import java.util.List; import java.util.ArrayList; class New7 { List<String> s = new ArrayList<>(); } """
);
/* * Create a file with a new feature in 8, not in 7 : lambda
*/
writeSourceFile("New8.java", """ publicclass New8 { void m() { newThread(() -> { });
}
} """
);
/* * Create a file with a new feature in 10, not in 9 : var
*/
writeSourceFile("New10.java", """ publicclass New10 { void m() { var tmp = newThread(() -> { });
}
} """
);
/* * Create a file with a new feature in 11, not in 10 : var for lambda parameters
*/
writeSourceFile("New11.java", """ publicclass New11 { static java.util.function.Function<String,String> f = (var x) -> x.substring(0); void m(String name) { var tmp = newThread(() -> { }, f.apply(name));
}
} """
);
/* * Create a file with a new feature in 14, not in 13 : switch expressions
*/
writeSourceFile("New14.java", """ publicclass New14 { static { int i = 5;
System.out.println( switch(i) { case 0 -> false; default -> true;
}
);
}
} """
);
/* * Create a file with a new feature in 15, not in 14 : text blocks
*/
writeSourceFile("New15.java", """ publicclass New15 { publicstaticfinal String s =
\"\"\"
Hello, World.
\"\"\"
;
} """
);
/* * Create a file with a new feature in 16, not in 15 : records
*/
writeSourceFile("New16.java", """ publicclass New16 { public record Record(double rpm) { publicstaticfinal Record LONG_PLAY = new Record(100.0/3.0);
}
} """
);
/* * Create a file with a new feature in 17, not in 16 : sealed classes
*/
writeSourceFile("New17.java", """ publicclass New17 { publicstatic sealed class Seal {}
publicstaticfinalclass Pinniped extends Seal {} publicstaticfinalclass TaperedThread extends Seal {} publicstaticfinalclass Wax extends Seal {}
} """
);
}
protectedboolean checkClassFileVersion
(String filename,String classVersionNumber) {
ByteBuffer bb = ByteBuffer.allocate(1024); try (FileChannel fc = new FileInputStream(filename).getChannel()) {
bb.clear(); if (fc.read(bb) < 0) thrownew IOException("Could not read from file : " + filename);
bb.flip(); int minor = bb.getShort(4); int major = bb.getShort(6);
String fileVersion = major + "." + minor; if (fileVersion.equals(classVersionNumber)) { returntrue;
} else {
System.err.println("checkClassFileVersion : Failed");
System.err.println("\tclassfile version mismatch");
System.err.println("\texpected : " + classVersionNumber);
System.err.println("\tfound : " + fileVersion); returnfalse;
}
} catch (IOException e) {
System.err.println("checkClassFileVersion : Failed");
System.err.println("\terror :\t" + e.getMessage());
System.err.println("\tfile:\tfilename");
} returnfalse;
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.3 Sekunden
(vorverarbeitet)
¤
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 und die Messung sind noch experimentell.