1 /* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8054717 27 * @summary Make sure changes of public API on classpath triggers recompilation 28 * @library /tools/lib 29 * @modules jdk.compiler/com.sun.tools.javac.api 30 * jdk.compiler/com.sun.tools.javac.main 31 * jdk.compiler/com.sun.tools.sjavac 32 * @build Wrapper toolbox.ToolBox toolbox.Assert 33 * @run main Wrapper ClasspathDependencies 34 */ 35 36 37 import java.io.IOException; 38 import java.nio.file.FileVisitResult; 39 import java.nio.file.Files; 40 import java.nio.file.Path; 41 import java.nio.file.Paths; 42 import java.nio.file.SimpleFileVisitor; 43 import java.nio.file.attribute.BasicFileAttributes; 44 import java.nio.file.attribute.FileTime; 45 46 import static toolbox.Assert.check; 47 48 public class ClasspathDependencies extends SjavacBase { 49 50 public static void main(String... args) throws Exception { 51 52 Path root = Paths.get(ClasspathDependencies.class.getSimpleName() + "Test"); 53 54 delete(root); 55 56 Path src = root.resolve("src"); 57 Path classes = root.resolve("classes"); 58 Path srcDep = root.resolve("srcDep"); 59 Path classesDep = root.resolve("classesDep"); 60 61 //////////////////////////////////////////////////////////////////////// 62 headline("Create a test dependency, Dep.class, and put it in the classpath dir"); 63 String depCode = "package dep; public class Dep { public void m1() {} }"; 64 toolbox.writeFile(srcDep.resolve("dep/Dep.java"), depCode); 65 int rc = compile("-d", classesDep, "--state-dir=" + classesDep, srcDep); 66 check(rc == 0, "Compilation failed unexpectedly"); 67 68 //////////////////////////////////////////////////////////////////////// 69 headline("Compile and link against the Dep.class"); 70 toolbox.writeFile(src.resolve("pkg/C.java"), 71 "package pkg;" + 72 "import dep.Dep;" + 73 "public class C { Dep dep; public void m() { new Dep().m1(); } }"); 74 rc = compile("-d", classes, "--state-dir=" + classes, src, "-cp", classesDep); 75 check(rc == 0, "Compilation failed unexpectedly"); 76 FileTime modTime1 = Files.getLastModifiedTime(classes.resolve("pkg/C.class")); 77 78 //////////////////////////////////////////////////////////////////////// 79 headline("Update dependency (without changing the public api)"); 80 Thread.sleep(2000); 81 depCode = depCode.replaceAll("}$", "private void m2() {} }"); 82 toolbox.writeFile(srcDep.resolve("dep/Dep.java"), depCode); 83 rc = compile("-d", classesDep, "--state-dir=" + classesDep, srcDep); 84 check(rc == 0, "Compilation failed unexpectedly"); 85 86 //////////////////////////////////////////////////////////////////////// 87 headline("Make sure that this does not trigger recompilation of C.java"); 88 rc = compile("-d", classes, "--state-dir=" + classes, src, "-cp", classesDep); 89 check(rc == 0, "Compilation failed unexpectedly"); 90 FileTime modTime2 = Files.getLastModifiedTime(classes.resolve("pkg/C.class")); 91 check(modTime1.equals(modTime2), "Recompilation erroneously triggered"); 92 93 //////////////////////////////////////////////////////////////////////// 94 headline("Update public API of dependency"); 95 Thread.sleep(2000); 96 depCode = depCode.replace("m1()", "m1(String... arg)"); 97 toolbox.writeFile(srcDep.resolve("dep/Dep.java"), depCode); 98 rc = compile("-d", classesDep, "--state-dir=" + classesDep, srcDep); 99 check(rc == 0, "Compilation failed unexpectedly"); 100 101 //////////////////////////////////////////////////////////////////////// 102 headline("Make sure that recompilation of C.java is triggered"); 103 rc = compile("-d", classes, "--state-dir=" + classes, src, "-cp", classesDep); 104 check(rc == 0, "Compilation failed unexpectedly"); 105 FileTime modTime3 = Files.getLastModifiedTime(classes.resolve("pkg/C.class")); 106 check(modTime2.compareTo(modTime3) < 0, "Recompilation not triggered"); 107 } 108 109 static void headline(String str) { 110 System.out.println(); 111 System.out.println(str); 112 System.out.println(str.replaceAll(".", "-")); 113 } 114 115 static void delete(Path root) throws IOException { 116 if (!Files.exists(root)) 117 return; 118 Files.walkFileTree(root, new SimpleFileVisitor<Path>() { 119 @Override 120 public FileVisitResult visitFile(Path f, BasicFileAttributes a) 121 throws IOException { 122 Files.delete(f); 123 return FileVisitResult.CONTINUE; 124 } 125 126 @Override 127 public FileVisitResult postVisitDirectory(Path dir, IOException e) 128 throws IOException { 129 if (e != null) 130 throw e; 131 if (!dir.equals(root)) 132 Files.delete(dir); 133 return FileVisitResult.CONTINUE; 134 } 135 }); 136 } 137 }