langtools/test/tools/sjavac/DependencyCollection.java
author roland
Thu, 08 Jan 2015 11:00:38 +0100
changeset 28486 b0df113b962e
parent 27384 51aa4299ae7b
child 30730 d3ce7619db2c
permissions -rw-r--r--
8027626: assert(Opcode() != Op_If || outcnt() == 2) failed: bad if #1 Summary: IGVN encounters IfNode with single projection when optimizing dying subgraph Reviewed-by: kvn
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
/*
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    31
 * @build Wrapper ToolBox
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    32
 * @run main Wrapper DependencyCollection
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    33
 */
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    34
27384
51aa4299ae7b 8062740: Since changeset 2686:030080f03e4f test/tools/sjavac/DependencyCollection.java does no longer compile
alundblad
parents: 27319
diff changeset
    35
import java.io.IOException;
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    36
import java.io.PrintWriter;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    37
import java.nio.file.Path;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    38
import java.nio.file.Paths;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    39
import java.util.Arrays;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    40
import java.util.Comparator;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    41
import java.util.HashSet;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    42
import java.util.Set;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    43
import java.util.regex.Matcher;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    44
import java.util.regex.Pattern;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    45
import java.util.stream.Collectors;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    46
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    47
import javax.tools.JavaCompiler;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    48
import javax.tools.JavaFileObject;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    49
import javax.tools.StandardJavaFileManager;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    50
import javax.tools.ToolProvider;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    51
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    52
import com.sun.tools.javac.api.JavacTaskImpl;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    53
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
    54
import com.sun.tools.sjavac.comp.SmartFileManager;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    55
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
    56
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    57
public class DependencyCollection {
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    58
27384
51aa4299ae7b 8062740: Since changeset 2686:030080f03e4f test/tools/sjavac/DependencyCollection.java does no longer compile
alundblad
parents: 27319
diff changeset
    59
    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
    60
        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
    61
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    62
        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    63
        try (StandardJavaFileManager fileManager = javac.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    64
            SmartFileManager smartFileManager = new SmartFileManager(fileManager);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    65
            smartFileManager.setSymbolFileEnabled(false);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    66
            Iterable<? extends JavaFileObject> fileObjects =
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    67
                    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
    68
            JavacTaskImpl task = (JavacTaskImpl) javac.getTask(new PrintWriter(System.out),
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    69
                                                               smartFileManager,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    70
                                                               null,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    71
                                                               Arrays.asList("-d", "classes",
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    72
                                                                             "-sourcepath", src.toAbsolutePath().toString()),
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    73
                                                               null,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    74
                                                               fileObjects);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    75
            DependencyCollector depsCollector = new DependencyCollector();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    76
            task.addTaskListener(depsCollector);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    77
            task.doCall();
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    78
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    79
            // Find pkg symbol
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    80
            PackageSymbol pkg = findPkgSymbolWithName(depsCollector.getSourcePackages(), "pkg");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    81
            Set<PackageSymbol> foundDependencies = depsCollector.getDependenciesForPkg(pkg);
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    82
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    83
            // Print dependencies
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    84
            System.out.println("Found dependencies:");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    85
            foundDependencies.stream()
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    86
                             .sorted(Comparator.comparing(DependencyCollection::extractNumber))
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    87
                             .forEach(p -> System.out.println("    " + p));
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    88
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    89
            // Check result
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    90
            Set<Integer> found = foundDependencies.stream()
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    91
                                                  .map(DependencyCollection::extractNumber)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    92
                                                  .collect(Collectors.toSet());
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    93
            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
    94
            Set<Integer> expected = new HashSet<>();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    95
            for (int i = 2; i <= 30; i++) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    96
                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
    97
                expected.add(i);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
    98
            }
26991
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
    99
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   100
            Set<Integer> missing = new HashSet<>(expected);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   101
            missing.removeAll(found);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   102
            if (missing.size() > 0) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   103
                System.out.println("Missing dependencies:");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   104
                missing.forEach(i -> System.out.println("    Dependency " + 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> unexpected = new HashSet<>(found);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   108
            unexpected.removeAll(expected);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   109
            if (unexpected.size() > 0) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   110
                System.out.println("Unexpected dependencies found:");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   111
                unexpected.forEach(i -> System.out.println("    Dependency " + i));
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   112
            }
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   113
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   114
            if (missing.size() > 0 || unexpected.size() > 0)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 26991
diff changeset
   115
                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
   116
        }
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   117
    }
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   118
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   119
    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
   120
        for (PackageSymbol ps : syms)
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   121
            if (ps.fullname.toString().equals("pkg"))
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   122
                return ps;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   123
        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
   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
    public static int extractNumber(PackageSymbol p) {
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   127
        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
   128
        if (!m.find())
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   129
            return -1;
88d998b3bb4b 8056258: Analysis of public API does not take super classes into account
alundblad
parents:
diff changeset
   130
        return Integer.parseInt(m.group());
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
}