/* * Copyright (c) 2014, 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. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * 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 * @bug 8056258 8048609 * @summary Ensures that the DependencyCollector covers various cases. * @library /tools/lib * @build Wrapper ToolBox * @run main Wrapper DependencyCollection */ import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.tools.StandardJavaFileManager; import com.sun.tools.javac.api.JavacTaskImpl; import com.sun.tools.javac.api.JavacTool; import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Symbol.ClassSymbol; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.Assert; import com.sun.tools.javac.util.Context; import com.sun.tools.sjavac.comp.SmartFileManager; import com.sun.tools.sjavac.comp.dependencies.DependencyCollector; public class DependencyCollection { public static void main(String[] args) throws IOException { Path src = Paths.get(ToolBox.testSrc, "test-input", "src"); Context context = new Context(); JavacTool javac = JavacTool.create(); try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) { SmartFileManager smartFileManager = new SmartFileManager(fm); smartFileManager.setSymbolFileEnabled(false); List srcList = Arrays.asList(src.resolve("pkg/Test.java").toFile()); JavacTaskImpl task = (JavacTaskImpl) javac.getTask( new PrintWriter(System.out), smartFileManager, null, Arrays.asList("-d", "classes", "-sourcepath", src.toAbsolutePath().toString()), null, fm.getJavaFileObjectsFromFiles(srcList), context); DependencyCollector dc = new DependencyCollector(context); task.addTaskListener(dc); task.doCall(); // Find Tset.java compilation unit JCCompilationUnit testCU = null; for (JCCompilationUnit cu : dc.collectedDependencies.keySet()) { if (cu.getSourceFile().getName().endsWith("/Test.java")) testCU = cu; } Assert.check(testCU != null, "Could not find Test.java compilation unit."); Set foundDependencies = dc.collectedDependencies.get(testCU).get("pkg.Test"); // Print dependencies foundDependencies.stream() .sorted(Comparator.comparing(DependencyCollection::extractNumber)) .forEach(p -> System.out.println(" " + p)); // Check result Set found = foundDependencies.stream() .map(DependencyCollection::extractNumber) .collect(Collectors.toSet()); found.remove(-1); // Dependencies with no number (java.lang etc) Set expected = new HashSet<>(); for (int i = 2; i <= 32; i++) { if (i == 15) // Case 15 correspond to the type of a continue; // throw-away return value. expected.add(i); } Set missing = new HashSet<>(expected); missing.removeAll(found); if (missing.size() > 0) { System.out.println("Missing dependencies:"); missing.forEach(i -> System.out.println(" Dependency " + i)); } Set unexpected = new HashSet<>(found); unexpected.removeAll(expected); if (unexpected.size() > 0) { System.out.println("Unexpected dependencies found:"); unexpected.forEach(i -> System.out.println(" Dependency " + i)); } if (missing.size() > 0 || unexpected.size() > 0) throw new AssertionError("Missing and/or unexpected dependencies found."); System.out.println("Test passed"); } } public static int extractNumber(Symbol p) { Matcher m = Pattern.compile("\\d+").matcher(p.name.toString()); if (!m.find()) return -1; return Integer.parseInt(m.group()); } }