langtools/test/tools/javac/options/xprefer/XPreferTest.java
author alundblad
Wed, 07 May 2014 17:29:42 +0200
changeset 24298 cc4f0f71a505
child 24395 5c541aef1350
permissions -rw-r--r--
8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath. Summary: Added -XXuserPathsFirst to allow user classes to take precedence over boot classes Reviewed-by: jjg
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
/*
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    29
 */
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
import java.io.File;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    32
import java.io.FileWriter;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    33
import java.io.IOException;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    34
import java.io.PrintWriter;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    35
import java.io.StringWriter;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    36
import java.util.ArrayList;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    37
import java.util.Arrays;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    38
import java.util.Collection;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    39
import java.util.Iterator;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    40
import java.util.List;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    41
import java.util.ListIterator;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    42
import java.util.NoSuchElementException;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    43
import java.util.Scanner;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    44
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    45
import javax.tools.JavaCompiler;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    46
import javax.tools.JavaCompiler.CompilationTask;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    47
import javax.tools.ToolProvider;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    48
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    49
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    50
public class XPreferTest {
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
    enum Dir {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    53
        SOURCE_PATH("src"),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    54
        CLASS_PATH("cp"),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    55
        BOOT_PATH("boot");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    56
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    57
        File file;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    58
        Dir(String dir) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    59
            this.file = new File(dir);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    60
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    61
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    62
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    63
    enum ImplicitOption {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    64
        XPREFER_SOURCE("-Xprefer:source"),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    65
        XPREFER_NEWER("-Xprefer:newer"),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    66
        XXUSERPATHSFIRST("-XXuserPathsFirst");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    67
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    68
        final String optionString;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    69
        private ImplicitOption(String optionString) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    70
            this.optionString = optionString;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    71
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    72
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    73
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    74
    final static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    75
    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
    76
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    77
    public static void main(String... args) throws Exception {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    78
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    79
        // Initialize test-directories
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    80
        OUTPUT_DIR.mkdir();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    81
        for (Dir dir : Dir.values())
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    82
            dir.file.mkdir();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    83
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    84
        int testCaseCounter = 0;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    85
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    86
        for (List<Dir> dirSubset : SubseqIter.subseqsOf(Dir.values())) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    87
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    88
            if (dirSubset.isEmpty())
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    89
                continue;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    90
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    91
            for (ImplicitOption policy : ImplicitOption.values()) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    92
                for (List<Dir> dirOrder : PermutationIterator.permutationsOf(dirSubset)) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    93
                    new TestCase(dirOrder, policy, testCaseCounter++).run();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    94
                }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    95
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    96
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    97
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    98
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
    99
    static class TestCase {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   100
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   101
        String classId;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   102
        List<Dir> dirs;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   103
        ImplicitOption option;
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
        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
   106
            this.dirs = dirs;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   107
            this.option = option;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   108
            this.classId = "XPreferTestImplicit" + testCaseNum;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   109
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   110
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   111
        void run() throws Exception {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   112
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   113
            System.out.println("Test:");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   114
            System.out.println("    Class id: " + classId);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   115
            System.out.println("    Dirs:     " + dirs);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   116
            System.out.println("    Option:   " + option);
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
            createTestFiles();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   119
            String compileOutput = compile();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   120
            Dir actual = getChosenOrigin(compileOutput);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   121
            Dir expected = getExpectedOrigin();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   122
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   123
            System.out.println("    Expected: " + expected);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   124
            System.out.println("    Actual:   " + actual);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   125
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   126
            if (actual != expected) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   127
                throw new RuntimeException(String.format(
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   128
                        "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
   129
                        expected == null ? "<none>" : expected.name(),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   130
                        actual   == null ? "<none>" : actual.name()));
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   131
            }
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
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   134
        Dir getExpectedOrigin() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   135
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   136
            Dir newest = dirs.get(0);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   137
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   138
            switch (option) {
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
            case XPREFER_NEWER:
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   141
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   142
                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
   143
                        : 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
   144
                        : null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   145
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   146
                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
   147
                        : null;
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
                for (Dir dir : dirs)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   150
                    if (dir == cls || dir == src)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   151
                        return dir;
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
                return null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   154
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   155
            case XPREFER_SOURCE:
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   156
                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
   157
                     : 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
   158
                     : 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
   159
                     : null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   160
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   161
            case XXUSERPATHSFIRST:
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   162
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   163
                for (Dir dir : dirs)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   164
                    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
   165
                        return dir;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   166
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   167
                // 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
   168
                if (newest != Dir.BOOT_PATH)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   169
                    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
   170
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   171
                return Dir.BOOT_PATH;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   172
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   173
            default:
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   174
                throw new RuntimeException("Unhandled policy case.");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   175
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   176
        }
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
        Dir getChosenOrigin(String compilerOutput) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   179
            Scanner s = new Scanner(compilerOutput);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   180
            while (s.hasNextLine()) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   181
                String line = s.nextLine();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   182
                if (line.matches("\\[loading .*\\]"))
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   183
                    for (Dir dir : Dir.values())
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   184
                        if (line.contains(dir.file.getName() + "/" + classId))
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   185
                            return dir;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   186
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   187
            return null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   188
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   189
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   190
        String compile() throws IOException {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   191
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   192
            // Create a class that references classId
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   193
            File explicit = new File("ExplicitClass.java");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   194
            FileWriter filewriter = new FileWriter(explicit);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   195
            filewriter.append("class ExplicitClass { " + classId + " implicit; }");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   196
            filewriter.close();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   197
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   198
            StringWriter sw = new StringWriter();
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
            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
   201
                    "-verbose",
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   202
                    option.optionString,
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   203
                    "-sourcepath", Dir.SOURCE_PATH.file.getPath(),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   204
                    "-classpath", Dir.CLASS_PATH.file.getPath(),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   205
                    "-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
   206
                    "-d", XPreferTest.OUTPUT_DIR.getPath(),
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   207
                    explicit.getPath()
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   208
            }, new PrintWriter(sw));
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   209
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   210
            return sw.toString();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   211
        }
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
        void createTestFiles() throws IOException {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   214
            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
   215
            for (Dir dir : dirs) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   216
                createFile(dir).setLastModified(t);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   217
                t -= 10000;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   218
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   219
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   220
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   221
        File createFile(Dir dir) throws IOException {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   222
            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
   223
            try (FileWriter w = new FileWriter(src)) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   224
                w.append("public class " + classId + " {}");
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
            // 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
   227
            if(dir == Dir.SOURCE_PATH)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   228
                return src;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   229
            // ...otherwise compile into a ".class".
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   230
            CompilationTask task = comp.getTask(null, null, null, null, null,
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   231
                    comp.getStandardFileManager(null, null, null).getJavaFileObjects(src));
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   232
            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
   233
            if(!task.call() || !dest.exists())
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   234
                throw new RuntimeException("Compilation failure.");
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   235
            src.delete();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   236
            return dest;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   237
        }
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
}
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   240
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   241
// 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
   242
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
   243
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   244
    List<T> elements;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   245
    boolean[] states;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   246
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   247
    public SubseqIter(Collection<T> c) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   248
        states = new boolean[c.size()];
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   249
        elements = new ArrayList<T>(c);
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
    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
   253
        return new Iterable<List<T>>() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   254
            @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   255
            public Iterator<List<T>> iterator() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   256
                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
   257
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   258
        };
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
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   261
    // 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
   262
    // 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
   263
    private boolean roll(int i) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   264
        if (i == states.length)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   265
            return true;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   266
        if (!roll(i + 1))
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   267
            return false;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   268
        states[i] = !states[i];
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   269
        return !states[i];
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
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   273
    public List<T> next() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   274
        if (!hasNext())
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   275
            throw new NoSuchElementException();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   276
        // 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
   277
        List<T> next = new ArrayList<T>();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   278
        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
   279
            if (states[i])
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   280
                next.add(elements.get(i));
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   281
        if (roll(0))
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   282
            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
   283
        return next;
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
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   286
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   287
    public boolean hasNext() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   288
        return states != null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   289
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   290
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   291
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   292
    public void remove() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   293
        throw new UnsupportedOperationException();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   294
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   295
}
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   296
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   297
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
   298
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   299
    DirInt head;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   300
    boolean hasNext = true;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   301
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   302
    public PermutationIterator(List<T> toPermute) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   303
        ListIterator<T> iter = toPermute.listIterator();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   304
        if (iter.hasNext())
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   305
            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
   306
        DirInt prev = head;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   307
        while (iter.hasNext()) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   308
            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
   309
            di.left = prev;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   310
            prev.right = di;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   311
            prev = di;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   312
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   313
    }
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 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
   316
        return new Iterable<List<T>>() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   317
            public Iterator<List<T>> iterator() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   318
                return new PermutationIterator<>(list);
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   319
            }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   320
        };
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   321
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   322
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   323
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   324
    public boolean hasNext() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   325
        return hasNext;
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
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   329
    public List<T> next() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   330
        // 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
   331
        List<T> result = new ArrayList<>();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   332
        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
   333
            result.add(di.object);
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
        // Step state forward
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   336
        DirInt maxMob = null;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   337
        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
   338
            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
   339
                maxMob = di;
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
        if (maxMob == null) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   342
            hasNext = false;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   343
        } else {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   344
            maxMob.swapWithAdjacent();
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
                if (di.val > maxMob.val)
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   347
                    di.facingLeft = !di.facingLeft;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   348
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   349
        return result;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   350
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   351
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   352
    private final class DirInt {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   353
        int val;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   354
        T object;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   355
        DirInt left, right;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   356
        boolean facingLeft = true;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   357
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   358
        public DirInt(int val, T object) {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   359
            this.val = val;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   360
            this.object = object;
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
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   363
        boolean isMobile() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   364
            DirInt adjacent = facingLeft ? left : right;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   365
            return adjacent != null && val > adjacent.val;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   366
        }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   367
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   368
        public void swapWithAdjacent() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   369
            DirInt l = facingLeft ? left : this;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   370
            DirInt r = facingLeft ? this : right;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   371
            if (head == l) head = r;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   372
            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
   373
            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
   374
            l.right = r.right;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   375
            r.left = l.left;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   376
            r.right = l;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   377
            l.left = r;
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   378
        }
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
    @Override
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   382
    public void remove() {
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   383
        throw new UnsupportedOperationException();
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   384
    }
cc4f0f71a505 8028196: Javac allows timestamps inside rt.jar to affect compilation when using -sourcepath.
alundblad
parents:
diff changeset
   385
}