langtools/test/tools/javac/options/xprefer/XPreferTest.java
author akulyakh
Thu, 21 May 2015 11:41:04 -0700
changeset 30730 d3ce7619db2c
parent 27319 030080f03e4f
child 36526 3b41f1c69604
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:
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
     4
 *
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
     7
 * published by the Free Software Foundation.
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
     8
 *
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    13
 * accompanied this code).
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    14
 *
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    18
 *
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    21
 * questions.
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    22
 */
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    23
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    24
/*
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    25
 * @test
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    26
 * @summary Tests which path is used to represent an implicit type given
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    27
 * various xprefer arguments and multiple .class / .java files involved.
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    28
 * @bug 8028196
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27319
diff changeset
    29
 * @modules jdk.compiler
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    30
 */
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    31
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    32
import java.io.File;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    33
import java.io.FileWriter;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    34
import java.io.IOException;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    35
import java.io.PrintWriter;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    36
import java.io.StringWriter;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    37
import java.util.ArrayList;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    38
import java.util.Arrays;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    39
import java.util.Collection;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    40
import java.util.Iterator;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    41
import java.util.List;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    42
import java.util.ListIterator;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    43
import java.util.NoSuchElementException;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    44
import java.util.Scanner;
25005
645284cd7d1d 8042830: A recently added Xprefer test fails on Windows
alundblad
parents: 24395
diff changeset
    45
import java.util.regex.Pattern;
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    46
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    47
import javax.tools.JavaCompiler;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    48
import javax.tools.JavaCompiler.CompilationTask;
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    49
import javax.tools.StandardJavaFileManager;
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    50
import javax.tools.ToolProvider;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    51
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    52
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    53
public class XPreferTest {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    54
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    55
    enum Dir {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    56
        SOURCE_PATH("src"),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    57
        CLASS_PATH("cp"),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    58
        BOOT_PATH("boot");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    59
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    60
        File file;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    61
        Dir(String dir) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    62
            this.file = new File(dir);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    63
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    64
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    65
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    66
    enum ImplicitOption {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    67
        XPREFER_SOURCE("-Xprefer:source"),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    68
        XPREFER_NEWER("-Xprefer:newer"),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    69
        XXUSERPATHSFIRST("-XXuserPathsFirst");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    70
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    71
        final String optionString;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    72
        private ImplicitOption(String optionString) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    73
            this.optionString = optionString;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    74
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    75
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    76
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    77
    final static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    78
    final static StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    79
    final static File OUTPUT_DIR = new File("out");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    80
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    81
    public static void main(String... args) throws Exception {
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    82
        try {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    83
            // Initialize test-directories
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    84
            OUTPUT_DIR.mkdir();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    85
            for (Dir dir : Dir.values())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    86
                dir.file.mkdir();
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    87
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    88
            int testCaseCounter = 0;
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    89
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    90
            for (List<Dir> dirSubset : SubseqIter.subseqsOf(Dir.values())) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    91
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    92
                if (dirSubset.isEmpty())
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    93
                    continue;
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    94
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    95
                for (ImplicitOption policy : ImplicitOption.values()) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    96
                    for (List<Dir> dirOrder : PermutationIterator.permutationsOf(dirSubset)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    97
                        new TestCase(dirOrder, policy, testCaseCounter++).run();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
    98
                    }
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    99
                }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   100
            }
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
   101
        } finally {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
   102
            fm.close();
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   103
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   104
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   105
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   106
    static class TestCase {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   107
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   108
        String classId;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   109
        List<Dir> dirs;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   110
        ImplicitOption option;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   111
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   112
        public TestCase(List<Dir> dirs, ImplicitOption option, int testCaseNum) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   113
            this.dirs = dirs;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   114
            this.option = option;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   115
            this.classId = "XPreferTestImplicit" + testCaseNum;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   116
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   117
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   118
        void run() throws Exception {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   119
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   120
            System.out.println("Test:");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   121
            System.out.println("    Class id: " + classId);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   122
            System.out.println("    Dirs:     " + dirs);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   123
            System.out.println("    Option:   " + option);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   124
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   125
            createTestFiles();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   126
            String compileOutput = compile();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   127
            Dir actual = getChosenOrigin(compileOutput);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   128
            Dir expected = getExpectedOrigin();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   129
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   130
            System.out.println("    Expected: " + expected);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   131
            System.out.println("    Actual:   " + actual);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   132
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   133
            if (actual != expected) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   134
                throw new RuntimeException(String.format(
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   135
                        "Expected javac to choose %s but %s was chosen",
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   136
                        expected == null ? "<none>" : expected.name(),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   137
                        actual   == null ? "<none>" : actual.name()));
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   138
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   139
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   140
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   141
        Dir getExpectedOrigin() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   142
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   143
            Dir newest = dirs.get(0);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   144
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   145
            switch (option) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   146
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   147
            case XPREFER_NEWER:
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   148
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   149
                Dir cls = dirs.contains(Dir.BOOT_PATH) ? Dir.BOOT_PATH
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   150
                        : dirs.contains(Dir.CLASS_PATH) ? Dir.CLASS_PATH
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   151
                        : null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   152
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   153
                Dir src = dirs.contains(Dir.SOURCE_PATH) ? Dir.SOURCE_PATH
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   154
                        : null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   155
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   156
                for (Dir dir : dirs)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   157
                    if (dir == cls || dir == src)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   158
                        return dir;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   159
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   160
                return null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   161
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   162
            case XPREFER_SOURCE:
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   163
                return dirs.contains(Dir.SOURCE_PATH) ? Dir.SOURCE_PATH
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   164
                     : dirs.contains(Dir.BOOT_PATH) ? Dir.BOOT_PATH
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   165
                     : dirs.contains(Dir.CLASS_PATH) ? Dir.CLASS_PATH
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   166
                     : null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   167
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   168
            case XXUSERPATHSFIRST:
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   169
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   170
                for (Dir dir : dirs)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   171
                    if (dir == Dir.SOURCE_PATH || dir == Dir.CLASS_PATH)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   172
                        return dir;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   173
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   174
                // Neither SOURCE_PATH nor CLASS_PATH among dirs. Safty check:
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   175
                if (newest != Dir.BOOT_PATH)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   176
                    throw new AssertionError("Expected to find BOOT_PATH");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   177
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   178
                return Dir.BOOT_PATH;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   179
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   180
            default:
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   181
                throw new RuntimeException("Unhandled policy case.");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   182
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   183
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   184
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   185
        Dir getChosenOrigin(String compilerOutput) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   186
            Scanner s = new Scanner(compilerOutput);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   187
            while (s.hasNextLine()) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   188
                String line = s.nextLine();
25005
645284cd7d1d 8042830: A recently added Xprefer test fails on Windows
alundblad
parents: 24395
diff changeset
   189
                if (line.matches("\\[loading .*\\]")) {
645284cd7d1d 8042830: A recently added Xprefer test fails on Windows
alundblad
parents: 24395
diff changeset
   190
                    for (Dir dir : Dir.values()) {
645284cd7d1d 8042830: A recently added Xprefer test fails on Windows
alundblad
parents: 24395
diff changeset
   191
                        // On Windows all paths are printed with '/' except
645284cd7d1d 8042830: A recently added Xprefer test fails on Windows
alundblad
parents: 24395
diff changeset
   192
                        // paths inside zip-files, which are printed with '\'.
645284cd7d1d 8042830: A recently added Xprefer test fails on Windows
alundblad
parents: 24395
diff changeset
   193
                        // For this reason we accept both '/' and '\' below.
645284cd7d1d 8042830: A recently added Xprefer test fails on Windows
alundblad
parents: 24395
diff changeset
   194
                        String regex = dir.file.getName() + "[\\\\/]" + classId;
645284cd7d1d 8042830: A recently added Xprefer test fails on Windows
alundblad
parents: 24395
diff changeset
   195
                        if (Pattern.compile(regex).matcher(line).find())
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   196
                            return dir;
25005
645284cd7d1d 8042830: A recently added Xprefer test fails on Windows
alundblad
parents: 24395
diff changeset
   197
                    }
645284cd7d1d 8042830: A recently added Xprefer test fails on Windows
alundblad
parents: 24395
diff changeset
   198
                }
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   199
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   200
            return null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   201
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   202
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   203
        String compile() throws IOException {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   204
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   205
            // Create a class that references classId
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   206
            File explicit = new File("ExplicitClass.java");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   207
            FileWriter filewriter = new FileWriter(explicit);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   208
            filewriter.append("class ExplicitClass { " + classId + " implicit; }");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   209
            filewriter.close();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   210
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   211
            StringWriter sw = new StringWriter();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   212
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   213
            com.sun.tools.javac.Main.compile(new String[] {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   214
                    "-verbose",
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   215
                    option.optionString,
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   216
                    "-sourcepath", Dir.SOURCE_PATH.file.getPath(),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   217
                    "-classpath", Dir.CLASS_PATH.file.getPath(),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   218
                    "-Xbootclasspath/p:" + Dir.BOOT_PATH.file.getPath(),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   219
                    "-d", XPreferTest.OUTPUT_DIR.getPath(),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   220
                    explicit.getPath()
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   221
            }, new PrintWriter(sw));
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   222
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   223
            return sw.toString();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   224
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   225
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   226
        void createTestFiles() throws IOException {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   227
            long t = 1390927988755L;  // Tue Jan 28 17:53:08 CET 2014
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   228
            for (Dir dir : dirs) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   229
                createFile(dir).setLastModified(t);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   230
                t -= 10000;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   231
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   232
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   233
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   234
        File createFile(Dir dir) throws IOException {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   235
            File src = new File(dir.file, classId + ".java");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   236
            try (FileWriter w = new FileWriter(src)) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   237
                w.append("public class " + classId + " {}");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   238
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   239
            // If we're after the ".java" representation, we're done...
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   240
            if(dir == Dir.SOURCE_PATH)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   241
                return src;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   242
            // ...otherwise compile into a ".class".
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
   243
            CompilationTask task = comp.getTask(null, fm, null, null, null,
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 25005
diff changeset
   244
                    fm.getJavaFileObjects(src));
24298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   245
            File dest = new File(dir.file, classId + ".class");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   246
            if(!task.call() || !dest.exists())
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   247
                throw new RuntimeException("Compilation failure.");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   248
            src.delete();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   249
            return dest;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   250
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   251
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   252
}
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   253
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   254
// Iterator for iteration over all subsequences of a given list.
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   255
class SubseqIter<T> implements Iterator<List<T>> {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   256
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   257
    List<T> elements;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   258
    boolean[] states;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   259
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   260
    public SubseqIter(Collection<T> c) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   261
        states = new boolean[c.size()];
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   262
        elements = new ArrayList<T>(c);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   263
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   264
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   265
    public static <T> Iterable<List<T>> subseqsOf(final T[] t) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   266
        return new Iterable<List<T>>() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   267
            @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   268
            public Iterator<List<T>> iterator() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   269
                return new SubseqIter<T>(Arrays.asList(t));
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   270
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   271
        };
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   272
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   273
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   274
    // Roll values in 'states' from index i and forward.
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   275
    // Return true if we wrapped back to zero.
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   276
    private boolean roll(int i) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   277
        if (i == states.length)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   278
            return true;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   279
        if (!roll(i + 1))
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   280
            return false;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   281
        states[i] = !states[i];
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   282
        return !states[i];
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   283
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   284
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   285
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   286
    public List<T> next() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   287
        if (!hasNext())
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   288
            throw new NoSuchElementException();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   289
        // Include element i if states[i] is true
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   290
        List<T> next = new ArrayList<T>();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   291
        for (int i = 0; i < states.length; i++)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   292
            if (states[i])
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   293
                next.add(elements.get(i));
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   294
        if (roll(0))
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   295
            states = null; // hasNext() == false from now on.
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   296
        return next;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   297
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   299
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   300
    public boolean hasNext() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   301
        return states != null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   302
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   303
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   304
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   305
    public void remove() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   306
        throw new UnsupportedOperationException();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   307
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   308
}
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   309
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   310
class PermutationIterator<T> implements Iterator<List<T>> {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   311
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   312
    DirInt head;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   313
    boolean hasNext = true;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   314
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   315
    public PermutationIterator(List<T> toPermute) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   316
        ListIterator<T> iter = toPermute.listIterator();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   317
        if (iter.hasNext())
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   318
            head = new DirInt(iter.nextIndex(), iter.next());
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   319
        DirInt prev = head;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   320
        while (iter.hasNext()) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   321
            DirInt di = new DirInt(iter.nextIndex(), iter.next());
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   322
            di.left = prev;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   323
            prev.right = di;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   324
            prev = di;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   325
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   326
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   327
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   328
    public static <T> Iterable<List<T>> permutationsOf(final List<T> list) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   329
        return new Iterable<List<T>>() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   330
            public Iterator<List<T>> iterator() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   331
                return new PermutationIterator<>(list);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   332
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   333
        };
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   334
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   335
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   336
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   337
    public boolean hasNext() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   338
        return hasNext;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   339
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   340
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   341
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   342
    public List<T> next() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   343
        // Prep return value based on current state
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   344
        List<T> result = new ArrayList<>();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   345
        for (DirInt di = head; di != null; di = di.right)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   346
            result.add(di.object);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   347
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   348
        // Step state forward
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   349
        DirInt maxMob = null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   350
        for (DirInt di = head; di != null; di = di.right)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   351
            if (di.isMobile() && (maxMob == null || di.val > maxMob.val))
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   352
                maxMob = di;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   353
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   354
        if (maxMob == null) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   355
            hasNext = false;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   356
        } else {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   357
            maxMob.swapWithAdjacent();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   358
            for (DirInt di = head; di != null; di = di.right)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   359
                if (di.val > maxMob.val)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   360
                    di.facingLeft = !di.facingLeft;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   361
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   362
        return result;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   363
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   364
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   365
    private final class DirInt {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   366
        int val;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   367
        T object;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   368
        DirInt left, right;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   369
        boolean facingLeft = true;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   370
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   371
        public DirInt(int val, T object) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   372
            this.val = val;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   373
            this.object = object;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   374
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   375
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   376
        boolean isMobile() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   377
            DirInt adjacent = facingLeft ? left : right;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   378
            return adjacent != null && val > adjacent.val;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   379
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   380
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   381
        public void swapWithAdjacent() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   382
            DirInt l = facingLeft ? left : this;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   383
            DirInt r = facingLeft ? this : right;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   384
            if (head == l) head = r;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   385
            if (l.left  != null) l.left.right = r;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   386
            if (r.right != null) r.right.left = l;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   387
            l.right = r.right;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   388
            r.left = l.left;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   389
            r.right = l;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   390
            l.left = r;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   391
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   392
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   393
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   394
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   395
    public void remove() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   396
        throw new UnsupportedOperationException();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   397
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   398
}