langtools/test/tools/sjavac/DependencyCollection.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 27384 51aa4299ae7b
permissions -rw-r--r--
8076543: Add @modules as needed to the langtools tests Reviewed-by: jjg, shurailine
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27384
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
     4
 *
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    10
 *
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    15
 * accompanied this code).
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    16
 *
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    20
 *
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    23
 * questions.
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    24
 */
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    25
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    26
/*
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    27
 * @test
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    28
 * @bug 8056258 8048609
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    29
 * @summary Ensures that the DependencyCollector covers various cases.
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    30
 * @library /tools/lib
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27384
diff changeset
    31
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27384
diff changeset
    32
 *          jdk.compiler/com.sun.tools.javac.code
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27384
diff changeset
    33
 *          jdk.compiler/com.sun.tools.javac.file
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27384
diff changeset
    34
 *          jdk.compiler/com.sun.tools.javac.main
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27384
diff changeset
    35
 *          jdk.compiler/com.sun.tools.javac.util
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27384
diff changeset
    36
 *          jdk.compiler/com.sun.tools.sjavac.comp
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27384
diff changeset
    37
 *          jdk.compiler/com.sun.tools.sjavac.comp.dependencies
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    38
 * @build Wrapper ToolBox
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    39
 * @run main Wrapper DependencyCollection
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    40
 */
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    41
27384
51aa4299ae7b 8062740: Since changeset 2686:030080f03e4f test/tools/sjavac/DependencyCollection.java does no longer compile
alundblad
parents: 27319
diff changeset
    42
import java.io.IOException;
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    43
import java.io.PrintWriter;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    44
import java.nio.file.Path;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    45
import java.nio.file.Paths;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    46
import java.util.Arrays;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    47
import java.util.Comparator;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    48
import java.util.HashSet;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    49
import java.util.Set;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    50
import java.util.regex.Matcher;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    51
import java.util.regex.Pattern;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    52
import java.util.stream.Collectors;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    53
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    54
import javax.tools.JavaCompiler;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    55
import javax.tools.JavaFileObject;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    56
import javax.tools.StandardJavaFileManager;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    57
import javax.tools.ToolProvider;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    58
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    59
import com.sun.tools.javac.api.JavacTaskImpl;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    60
import com.sun.tools.javac.code.Symbol.PackageSymbol;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    61
import com.sun.tools.sjavac.comp.SmartFileManager;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    62
import com.sun.tools.sjavac.comp.dependencies.DependencyCollector;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    63
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    64
public class DependencyCollection {
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    65
27384
51aa4299ae7b 8062740: Since changeset 2686:030080f03e4f test/tools/sjavac/DependencyCollection.java does no longer compile
alundblad
parents: 27319
diff changeset
    66
    public static void main(String[] args) throws IOException {
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    67
        Path src = Paths.get(ToolBox.testSrc, "test-input", "src");
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    68
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    69
        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    70
        try (StandardJavaFileManager fileManager = javac.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    71
            SmartFileManager smartFileManager = new SmartFileManager(fileManager);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    72
            smartFileManager.setSymbolFileEnabled(false);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    73
            Iterable<? extends JavaFileObject> fileObjects =
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    74
                    fileManager.getJavaFileObjectsFromFiles(Arrays.asList(src.resolve("pkg/Test.java").toFile()));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    75
            JavacTaskImpl task = (JavacTaskImpl) javac.getTask(new PrintWriter(System.out),
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    76
                                                               smartFileManager,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    77
                                                               null,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    78
                                                               Arrays.asList("-d", "classes",
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    79
                                                                             "-sourcepath", src.toAbsolutePath().toString()),
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    80
                                                               null,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    81
                                                               fileObjects);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    82
            DependencyCollector depsCollector = new DependencyCollector();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    83
            task.addTaskListener(depsCollector);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    84
            task.doCall();
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    85
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    86
            // Find pkg symbol
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    87
            PackageSymbol pkg = findPkgSymbolWithName(depsCollector.getSourcePackages(), "pkg");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    88
            Set<PackageSymbol> foundDependencies = depsCollector.getDependenciesForPkg(pkg);
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    89
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    90
            // Print dependencies
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    91
            System.out.println("Found dependencies:");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    92
            foundDependencies.stream()
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    93
                             .sorted(Comparator.comparing(DependencyCollection::extractNumber))
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    94
                             .forEach(p -> System.out.println("    " + p));
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    95
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    96
            // Check result
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    97
            Set<Integer> found = foundDependencies.stream()
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    98
                                                  .map(DependencyCollection::extractNumber)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    99
                                                  .collect(Collectors.toSet());
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   100
            found.remove(-1); // Dependencies with no number (java.lang etc)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   101
            Set<Integer> expected = new HashSet<>();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   102
            for (int i = 2; i <= 30; i++) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   103
                if (i == 15) continue;  // Case 15 correspond to the type of a throw-away return value.
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   104
                expected.add(i);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   105
            }
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   106
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   107
            Set<Integer> missing = new HashSet<>(expected);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   108
            missing.removeAll(found);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   109
            if (missing.size() > 0) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   110
                System.out.println("Missing dependencies:");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   111
                missing.forEach(i -> System.out.println("    Dependency " + i));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   112
            }
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   113
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   114
            Set<Integer> unexpected = new HashSet<>(found);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   115
            unexpected.removeAll(expected);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   116
            if (unexpected.size() > 0) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   117
                System.out.println("Unexpected dependencies found:");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   118
                unexpected.forEach(i -> System.out.println("    Dependency " + i));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   119
            }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   120
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   121
            if (missing.size() > 0 || unexpected.size() > 0)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   122
                throw new AssertionError("Missing and/or unexpected dependencies found.");
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   123
        }
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   124
    }
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   125
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   126
    private static PackageSymbol findPkgSymbolWithName(Set<PackageSymbol> syms, String name) {
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   127
        for (PackageSymbol ps : syms)
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   128
            if (ps.fullname.toString().equals("pkg"))
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   129
                return ps;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   130
        throw new AssertionError("Could not find package named \"pkg\".");
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   131
    }
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   132
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   133
    public static int extractNumber(PackageSymbol p) {
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   134
        Matcher m = Pattern.compile("\\d+").matcher(p.fullname.toString());
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   135
        if (!m.find())
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   136
            return -1;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   137
        return Integer.parseInt(m.group());
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   138
    }
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   139
}