Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/sjavac/SJavac.java
32285 views
/*1* Copyright (c) 2013, 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*/2223import java.util.*;24import java.io.*;25import java.net.*;26import java.nio.file.*;27import java.nio.file.attribute.*;28import java.nio.charset.*;2930import com.sun.tools.sjavac.Main;3132public33class SJavac {3435public static void main(String... args) throws Exception {36SJavac s = new SJavac();37s.test();38}3940FileSystem defaultfs = FileSystems.getDefault();4142// Where to put generated sources that will43// test aspects of sjavac, ie JTWork/scratch/gensrc44Path gensrc;45// More gensrc dirs are used to test merging of serveral source roots.46Path gensrc2;47Path gensrc3;4849// Where to put compiled classes.50Path bin;51// Where to put c-header files.52Path headers;5354// The sjavac compiler.55Main main = new Main();5657// Remember the previous bin and headers state here.58Map<String,Long> previous_bin_state;59Map<String,Long> previous_headers_state;6061public void test() throws Exception {62gensrc = defaultfs.getPath("gensrc");63gensrc2 = defaultfs.getPath("gensrc2");64gensrc3 = defaultfs.getPath("gensrc3");65bin = defaultfs.getPath("bin");66headers = defaultfs.getPath("headers");6768Files.createDirectory(gensrc);69Files.createDirectory(gensrc2);70Files.createDirectory(gensrc3);71Files.createDirectory(bin);72Files.createDirectory(headers);7374initialCompile();75incrementalCompileNoChanges();76incrementalCompileDroppingClasses();77incrementalCompileWithChange();78incrementalCompileDropAllNatives();79incrementalCompileAddNative();80incrementalCompileChangeNative();81compileWithOverrideSource();82compileWithInvisibleSources();83compileCircularSources();84compileExcludingDependency();8586delete(gensrc);87delete(gensrc2);88delete(gensrc3);89delete(bin);90delete(headers);91}9293void initialCompile() throws Exception {94System.out.println("\nInitial compile of gensrc.");95System.out.println("----------------------------");96populate(gensrc,97"alfa/AINT.java",98"package alfa; public interface AINT { void aint(); }",99100"alfa/A.java",101"package alfa; public class A implements AINT { "+102"public final static int DEFINITION = 17; public void aint() { } }",103104"alfa/AA.java",105"package alfa;"+106"// A package private class, not contributing to the public api.\n"+107"class AA {"+108" // A properly nested static inner class.\n"+109" static class AAA { }\n"+110" // A properly nested inner class.\n"+111" class AAAA { }\n"+112" Runnable foo() {\n"+113" // A proper anonymous class.\n"+114" return new Runnable() { public void run() { } };\n"+115" }\n"+116" AAA aaa;\n"+117" AAAA aaaa;\n"+118" AAAAA aaaaa;\n"+119"}\n"+120"class AAAAA {\n"+121" // A bad auxiliary class, but no one is referencing it\n"+122" // from outside of this source file, therefore it is ok.\n"+123"}\n",124125"beta/BINT.java",126"package beta;public interface BINT { void foo(); }",127128"beta/B.java",129"package beta; import alfa.A; public class B {"+130"private int b() { return A.DEFINITION; } native void foo(); }");131132compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",133"--server:portfile=testserver,background=false", "--log=debug");134previous_bin_state = collectState(bin);135previous_headers_state = collectState(headers);136}137138void incrementalCompileNoChanges() throws Exception {139System.out.println("\nTesting that no change in sources implies no change in binaries.");140System.out.println("------------------------------------------------------------------");141compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",142"--server:portfile=testserver,background=false", "--log=debug");143Map<String,Long> new_bin_state = collectState(bin);144verifyEqual(new_bin_state, previous_bin_state);145Map<String,Long> new_headers_state = collectState(headers);146verifyEqual(previous_headers_state, new_headers_state);147}148149void incrementalCompileDroppingClasses() throws Exception {150System.out.println("\nTesting that deleting AA.java deletes all");151System.out.println("generated inner class as well as AA.class");152System.out.println("-----------------------------------------");153removeFrom(gensrc, "alfa/AA.java");154compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",155"--server:portfile=testserver,background=false", "--log=debug");156Map<String,Long> new_bin_state = collectState(bin);157verifyThatFilesHaveBeenRemoved(previous_bin_state, new_bin_state,158"bin/alfa/AA$1.class",159"bin/alfa/AA$AAAA.class",160"bin/alfa/AA$AAA.class",161"bin/alfa/AAAAA.class",162"bin/alfa/AA.class");163164previous_bin_state = new_bin_state;165Map<String,Long> new_headers_state = collectState(headers);166verifyEqual(previous_headers_state, new_headers_state);167}168169void incrementalCompileWithChange() throws Exception {170System.out.println("\nNow update the A.java file with a new timestamps and");171System.out.println("new final static definition. This should trigger a recompile,");172System.out.println("not only of alfa, but also beta.");173System.out.println("But check that the generated native header was not updated!");174System.out.println("Since we did not modify the native api of B.");175System.out.println("-------------------------------------------------------------");176177populate(gensrc,"alfa/A.java",178"package alfa; public class A implements AINT { "+179"public final static int DEFINITION = 18; public void aint() { } private void foo() { } }");180181compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",182"--server:portfile=testserver,background=false", "--log=debug");183Map<String,Long> new_bin_state = collectState(bin);184185verifyNewerFiles(previous_bin_state, new_bin_state,186"bin/alfa/A.class",187"bin/alfa/AINT.class",188"bin/beta/B.class",189"bin/beta/BINT.class",190"bin/javac_state");191previous_bin_state = new_bin_state;192193Map<String,Long> new_headers_state = collectState(headers);194verifyEqual(new_headers_state, previous_headers_state);195}196197void incrementalCompileDropAllNatives() throws Exception {198System.out.println("\nNow update the B.java file with one less native method,");199System.out.println("ie it has no longer any methods!");200System.out.println("Verify that beta_B.h is removed!");201System.out.println("---------------------------------------------------------");202203populate(gensrc,"beta/B.java",204"package beta; import alfa.A; public class B {"+205"private int b() { return A.DEFINITION; } }");206207compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",208"--server:portfile=testserver,background=false", "--log=debug");209Map<String,Long> new_bin_state = collectState(bin);210verifyNewerFiles(previous_bin_state, new_bin_state,211"bin/beta/B.class",212"bin/beta/BINT.class",213"bin/javac_state");214previous_bin_state = new_bin_state;215216Map<String,Long> new_headers_state = collectState(headers);217verifyThatFilesHaveBeenRemoved(previous_headers_state, new_headers_state,218"headers/beta_B.h");219previous_headers_state = new_headers_state;220}221222void incrementalCompileAddNative() throws Exception {223System.out.println("\nNow update the B.java file with a final static annotated with @Native.");224System.out.println("Verify that beta_B.h is added again!");225System.out.println("------------------------------------------------------------------------");226227populate(gensrc,"beta/B.java",228"package beta; import alfa.A; public class B {"+229"private int b() { return A.DEFINITION; } "+230"@java.lang.annotation.Native final static int alfa = 42; }");231232compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",233"--server:portfile=testserver,background=false", "--log=debug");234Map<String,Long> new_bin_state = collectState(bin);235verifyNewerFiles(previous_bin_state, new_bin_state,236"bin/beta/B.class",237"bin/beta/BINT.class",238"bin/javac_state");239previous_bin_state = new_bin_state;240241Map<String,Long> new_headers_state = collectState(headers);242verifyThatFilesHaveBeenAdded(previous_headers_state, new_headers_state,243"headers/beta_B.h");244previous_headers_state = new_headers_state;245}246247void incrementalCompileChangeNative() throws Exception {248System.out.println("\nNow update the B.java file with a new value for the final static"+249" annotated with @Native.");250System.out.println("Verify that beta_B.h is rewritten again!");251System.out.println("-------------------------------------------------------------------");252253populate(gensrc,"beta/B.java",254"package beta; import alfa.A; public class B {"+255"private int b() { return A.DEFINITION; } "+256"@java.lang.annotation.Native final static int alfa = 43; }");257258compile("gensrc", "-d", "bin", "-h", "headers", "-j", "1",259"--server:portfile=testserver,background=false", "--log=debug");260Map<String,Long> new_bin_state = collectState(bin);261verifyNewerFiles(previous_bin_state, new_bin_state,262"bin/beta/B.class",263"bin/beta/BINT.class",264"bin/javac_state");265previous_bin_state = new_bin_state;266267Map<String,Long> new_headers_state = collectState(headers);268verifyNewerFiles(previous_headers_state, new_headers_state,269"headers/beta_B.h");270previous_headers_state = new_headers_state;271}272273void compileWithOverrideSource() throws Exception {274System.out.println("\nNow verify that we can override sources to be compiled.");275System.out.println("Compile gensrc and gensrc2. However do not compile broken beta.B in gensrc,");276System.out.println("only compile ok beta.B in gensrc2.");277System.out.println("---------------------------------------------------------------------------");278279delete(gensrc);280delete(gensrc2);281delete(bin);282previous_bin_state = collectState(bin);283284populate(gensrc,"alfa/A.java",285"package alfa; import beta.B; import gamma.C; public class A { B b; C c; }",286"beta/B.java",287"package beta; public class B { broken",288"gamma/C.java",289"package gamma; public class C { }");290291populate(gensrc2,292"beta/B.java",293"package beta; public class B { }");294295compile("-x", "beta", "gensrc", "gensrc2", "-d", "bin", "-h", "headers", "-j", "1",296"--server:portfile=testserver,background=false");297Map<String,Long> new_bin_state = collectState(bin);298verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state,299"bin/alfa/A.class",300"bin/beta/B.class",301"bin/gamma/C.class",302"bin/javac_state");303304System.out.println("----- Compile with exluded beta went well!");305delete(bin);306compileExpectFailure("gensrc", "gensrc2", "-d", "bin", "-h", "headers", "-j", "1",307"--server:portfile=testserver,background=false");308309System.out.println("----- Compile without exluded beta failed, as expected! Good!");310delete(bin);311}312313void compileWithInvisibleSources() throws Exception {314System.out.println("\nNow verify that we can make sources invisible to linking (sourcepath).");315System.out.println("Compile gensrc and link against gensrc2 and gensrc3, however");316System.out.println("gensrc2 contains broken code in beta.B, thus we must exclude that package");317System.out.println("fortunately gensrc3 contains a proper beta.B.");318System.out.println("------------------------------------------------------------------------");319320// Start with a fresh gensrcs and bin.321delete(gensrc);322delete(gensrc2);323delete(gensrc3);324delete(bin);325previous_bin_state = collectState(bin);326327populate(gensrc,"alfa/A.java",328"package alfa; import beta.B; import gamma.C; public class A { B b; C c; }");329populate(gensrc2,"beta/B.java",330"package beta; public class B { broken",331"gamma/C.java",332"package gamma; public class C { }");333populate(gensrc3, "beta/B.java",334"package beta; public class B { }");335336compile("gensrc", "-x", "beta", "-sourcepath", "gensrc2",337"-sourcepath", "gensrc3", "-d", "bin", "-h", "headers", "-j", "1",338"--server:portfile=testserver,background=false");339340System.out.println("The first compile went well!");341Map<String,Long> new_bin_state = collectState(bin);342verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state,343"bin/alfa/A.class",344"bin/javac_state");345346System.out.println("----- Compile with exluded beta went well!");347delete(bin);348compileExpectFailure("gensrc", "-sourcepath", "gensrc2", "-sourcepath", "gensrc3",349"-d", "bin", "-h", "headers", "-j", "1",350"--server:portfile=testserver,background=false");351352System.out.println("----- Compile without exluded beta failed, as expected! Good!");353delete(bin);354}355356void compileCircularSources() throws Exception {357System.out.println("\nNow verify that circular sources split on multiple cores can be compiled.");358System.out.println("---------------------------------------------------------------------------");359360// Start with a fresh gensrcs and bin.361delete(gensrc);362delete(gensrc2);363delete(gensrc3);364delete(bin);365previous_bin_state = collectState(bin);366367populate(gensrc,"alfa/A.java",368"package alfa; public class A { beta.B b; }",369"beta/B.java",370"package beta; public class B { gamma.C c; }",371"gamma/C.java",372"package gamma; public class C { alfa.A a; }");373374compile("gensrc", "-d", "bin", "-h", "headers", "-j", "3",375"--server:portfile=testserver,background=false","--log=debug");376Map<String,Long> new_bin_state = collectState(bin);377verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state,378"bin/alfa/A.class",379"bin/beta/B.class",380"bin/gamma/C.class",381"bin/javac_state");382delete(bin);383}384385/**386* Tests compiling class A that depends on class B without compiling class B387* @throws Exception If test fails388*/389void compileExcludingDependency() throws Exception {390System.out.println("\nVerify that excluding classes from compilation but not from linking works.");391System.out.println("---------------------------------------------------------------------------");392393delete(gensrc);394delete(bin);395previous_bin_state = collectState(bin);396397populate(gensrc,398"alfa/A.java",399"package alfa; public class A { beta.B b; }",400"beta/B.java",401"package beta; public class B { }");402403compile("-x", "beta", "-src", "gensrc", "-x", "alfa", "-sourcepath", "gensrc",404"-d", "bin", "--server:portfile=testserver,background=false");405406Map<String,Long> new_bin_state = collectState(bin);407verifyThatFilesHaveBeenAdded(previous_bin_state, new_bin_state,408"bin/alfa/A.class",409"bin/javac_state");410}411412void removeFrom(Path dir, String... args) throws IOException {413for (String filename : args) {414Path p = dir.resolve(filename);415Files.delete(p);416}417}418419void populate(Path src, String... args) throws IOException {420if (!Files.exists(src)) {421Files.createDirectory(src);422}423String[] a = args;424for (int i = 0; i<a.length; i+=2) {425String filename = a[i];426String content = a[i+1];427Path p = src.resolve(filename);428Files.createDirectories(p.getParent());429PrintWriter out = new PrintWriter(Files.newBufferedWriter(p,430Charset.defaultCharset()));431out.println(content);432out.close();433}434}435436void delete(final Path root) throws IOException {437if (!Files.exists(root)) return;438Files.walkFileTree(root, new SimpleFileVisitor<Path>() {439@Override440public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException441{442Files.delete(file);443return FileVisitResult.CONTINUE;444}445446@Override447public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException448{449if (e == null) {450if (!dir.equals(root)) Files.delete(dir);451return FileVisitResult.CONTINUE;452} else {453// directory iteration failed454throw e;455}456}457});458}459460void compile(String... args) throws Exception {461int rc = main.go(args, System.out, System.err);462if (rc != 0) throw new Exception("Error during compile!");463464// Wait a second, to get around the (temporary) problem with465// second resolution in the Java file api. But do not do this466// on windows where the timestamps work.467long in_a_sec = System.currentTimeMillis()+1000;468while (in_a_sec > System.currentTimeMillis()) {469try {470Thread.sleep(1000);471} catch (InterruptedException e) {472}473}474}475476void compileExpectFailure(String... args) throws Exception {477int rc = main.go(args, System.out, System.err);478if (rc == 0) throw new Exception("Expected error during compile! Did not fail!");479}480481Map<String,Long> collectState(Path dir) throws IOException482{483final Map<String,Long> files = new HashMap<>();484Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {485@Override486public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)487throws IOException488{489files.put(file.toString(),new Long(Files.getLastModifiedTime(file).toMillis()));490return FileVisitResult.CONTINUE;491}492});493return files;494}495496void verifyThatFilesHaveBeenRemoved(Map<String,Long> from,497Map<String,Long> to,498String... args) throws Exception {499500Set<String> froms = from.keySet();501Set<String> tos = to.keySet();502503if (froms.equals(tos)) {504throw new Exception("Expected new state to have fewer files than previous state!");505}506507for (String t : tos) {508if (!froms.contains(t)) {509throw new Exception("Expected "+t+" to exist in previous state!");510}511}512513for (String f : args) {514f = f.replace("/", File.separator);515if (!froms.contains(f)) {516throw new Exception("Expected "+f+" to exist in previous state!");517}518if (tos.contains(f)) {519throw new Exception("Expected "+f+" to have been removed from the new state!");520}521}522523if (froms.size() - args.length != tos.size()) {524throw new Exception("There are more removed files than the expected list!");525}526}527528void verifyThatFilesHaveBeenAdded(Map<String,Long> from,529Map<String,Long> to,530String... args) throws Exception {531532Set<String> froms = from.keySet();533Set<String> tos = to.keySet();534535if (froms.equals(tos)) {536throw new Exception("Expected new state to have more files than previous state!");537}538539for (String t : froms) {540if (!tos.contains(t)) {541throw new Exception("Expected "+t+" to exist in new state!");542}543}544545for (String f : args) {546f = f.replace("/", File.separator);547if (!tos.contains(f)) {548throw new Exception("Expected "+f+" to have been added to new state!");549}550if (froms.contains(f)) {551throw new Exception("Expected "+f+" to not exist in previous state!");552}553}554555if (froms.size() + args.length != tos.size()) {556throw new Exception("There are more added files than the expected list!");557}558}559560void verifyNewerFiles(Map<String,Long> from,561Map<String,Long> to,562String... args) throws Exception {563if (!from.keySet().equals(to.keySet())) {564throw new Exception("Expected the set of files to be identical!");565}566Set<String> files = new HashSet<String>();567for (String s : args) {568files.add(s.replace("/", File.separator));569}570for (String fn : from.keySet()) {571long f = from.get(fn);572long t = to.get(fn);573if (files.contains(fn)) {574if (t <= f) {575throw new Exception("Expected "+fn+" to have a more recent timestamp!");576}577} else {578if (t != f) {579throw new Exception("Expected "+fn+" to have the same timestamp!");580}581}582}583}584585String print(Map<String,Long> m) {586StringBuilder b = new StringBuilder();587Set<String> keys = m.keySet();588for (String k : keys) {589b.append(k+" "+m.get(k)+"\n");590}591return b.toString();592}593594void verifyEqual(Map<String,Long> from, Map<String,Long> to) throws Exception {595if (!from.equals(to)) {596System.out.println("FROM---"+print(from));597System.out.println("TO-----"+print(to));598throw new Exception("The dir should not differ! But it does!");599}600}601}602603604