langtools/test/tools/javac/importscope/ImportDependenciesTest.java
author jjg
Thu, 31 Mar 2016 15:20:50 -0700
changeset 36778 e04318f39f92
parent 36526 3b41f1c69604
permissions -rw-r--r--
8152897: refactor ToolBox to allow reduced documented dependencies Reviewed-by: vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27997
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
     1
/*
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
27997
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
     4
 *
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
     7
 * published by the Free Software Foundation.
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
     8
 *
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    13
 * accompanied this code).
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    14
 *
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    18
 *
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    21
 * questions.
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    22
 */
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    23
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    24
/*
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    25
 * @test
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    26
 * @bug 8065360
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    27
 * @summary The test checks dependencies through type parameters and implements/extends statements.
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    28
 * @library /tools/lib
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27997
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27997
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.main
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    31
 * @build toolbox.ToolBox ImportDependenciesTest
27997
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    32
 * @run main ImportDependenciesTest
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    33
 */
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    34
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    35
import javax.tools.JavaCompiler;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    36
import javax.tools.ToolProvider;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    37
import java.io.StringWriter;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    38
import java.util.ArrayList;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    39
import java.util.Arrays;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    40
import java.util.List;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    41
import java.util.stream.Collectors;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    42
import java.util.stream.Stream;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    43
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    44
import toolbox.ToolBox;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    45
27997
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    46
/**
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    47
 * The test checks that code which contains dependencies through type parameters,
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    48
 * implements/extends statements compiles properly. All combinations of
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    49
 * import types are tested. In addition, the test checks various combinations
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    50
 * of classes.
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    51
 */
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    52
public class ImportDependenciesTest {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    53
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    54
    private static final String sourceTemplate =
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    55
            "package pkg;\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    56
            "#IMPORT\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    57
            "public class Test {\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    58
            "    static #CLASS_TYPE InnerClass#TYPE_PARAMETER #PARENT {\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    59
            "        static class Inner1 {\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    60
            "        }\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    61
            "        interface Inner2 {\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    62
            "        }\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    63
            "        interface Inner3 {\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    64
            "        }\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    65
            "    }\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    66
            "    static class InnerClass1 {\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    67
            "        static class IInner1 {\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    68
            "        }\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    69
            "    }\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    70
            "    static class InnerInterface1 {\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    71
            "        interface IInner2 {\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    72
            "        }\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    73
            "    }\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    74
            "    static class InnerInterface2 {\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    75
            "        interface IInner3 {\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    76
            "        }\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    77
            "    }\n" +
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    78
            "}";
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    79
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    80
    public static void main(String[] args) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    81
        new ImportDependenciesTest().test();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    82
    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    83
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    84
    public void test() {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    85
        List<List<InnerClass>> typeParameters = InnerClass.getAllCombinationsForTypeParameter();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    86
        List<List<InnerClass>> parents = InnerClass.getAllCombinationsForInheritance();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    87
        int passed = 0;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    88
        int total = 0;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    89
        for (ClassType classType : ClassType.values()) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    90
            for (List<InnerClass> parent : parents) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    91
                if (!classType.canBeInherited(parent)) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    92
                    continue;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    93
                }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    94
                for (List<InnerClass> typeParameter : typeParameters) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    95
                    List<InnerClass> innerClasses = new ArrayList<>(typeParameter);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    96
                    innerClasses.addAll(parent);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    97
                    for (ImportType importType : ImportType.values()) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    98
                        ++total;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
    99
                        String source = sourceTemplate
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   100
                                .replace("#IMPORT", importType.generateImports(innerClasses))
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   101
                                .replace("#CLASS_TYPE", classType.getClassType())
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   102
                                .replace("#TYPE_PARAMETER", generateTypeParameter(typeParameter))
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   103
                                .replace("#PARENT", classType.generateInheritanceString(parent));
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   104
                        CompilationResult result = compile(new ToolBox.JavaSource("pkg/Test.java", source));
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   105
                        if (!result.isSuccessful) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   106
                            echo("Compilation failed!");
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   107
                            echo(source);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   108
                            echo(result.message);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   109
                            echo();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   110
                        } else {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   111
                            ++passed;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   112
                        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   113
                    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   114
                }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   115
            }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   116
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   117
        String message = String.format(
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   118
                "Total test cases run: %d, passed: %d, failed: %d.",
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   119
                total, passed, total - passed);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   120
        if (passed != total) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   121
            throw new RuntimeException(message);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   122
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   123
        echo(message);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   124
    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   125
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   126
    private String generateTypeParameter(List<InnerClass> typeParameters) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   127
        if (typeParameters.isEmpty()) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   128
            return "";
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   129
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   130
        return String.format("<T extends %s>", typeParameters.stream()
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   131
                .map(InnerClass::getSimpleName)
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   132
                .collect(Collectors.joining(" & ")));
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   133
    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   134
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   135
    private static class CompilationResult {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   136
        public final boolean isSuccessful;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   137
        public final String message;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   138
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   139
        public CompilationResult(boolean isSuccessful, String message) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   140
            this.isSuccessful = isSuccessful;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   141
            this.message = message;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   142
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   143
    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   144
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   145
    private CompilationResult compile(ToolBox.JavaSource...sources) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   146
        StringWriter writer = new StringWriter();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   147
        JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   148
        Boolean call = jc.getTask(writer, null, null, null, null, Arrays.asList(sources)).call();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   149
        return new CompilationResult(call, writer.toString().replace(ToolBox.lineSeparator, "\n"));
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   150
    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   151
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   152
    public void echo() {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   153
        echo("");
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   154
    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   155
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   156
    public void echo(String output) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   157
        printf(output + "\n");
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   158
    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   159
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   160
    public void printf(String template, Object...args) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   161
        System.err.print(String.format(template, args).replace("\n", ToolBox.lineSeparator));
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   162
    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   163
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   164
    enum ImportType {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   165
        IMPORT("import"), STATIC_IMPORT("import static"),
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   166
        IMPORT_ON_DEMAND("import"), STATIC_IMPORT_ON_DEMAND("import static");
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   167
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   168
        private final String importType;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   169
        private ImportType(String importType) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   170
            this.importType = importType;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   171
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   172
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   173
        private boolean isOnDemand() {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   174
            return this == IMPORT_ON_DEMAND || this == STATIC_IMPORT_ON_DEMAND;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   175
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   176
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   177
        public String generateImports(List<InnerClass> innerClasses) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   178
            return innerClasses.stream()
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   179
                    .map(i -> isOnDemand() ? i.getPackageName() + ".*" : i.getCanonicalName())
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   180
                    .distinct()
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   181
                    .map(s -> String.format("%s %s;", importType, s))
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   182
                    .collect(Collectors.joining("\n"));
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   183
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   184
    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   185
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   186
    enum ClassType {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   187
        CLASS("class") {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   188
            @Override
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   189
            public boolean canBeInherited(List<InnerClass> innerClasses) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   190
                return true;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   191
            }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   192
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   193
            @Override
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   194
            public String generateInheritanceString(List<InnerClass> innerClasses) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   195
                if (innerClasses.isEmpty()) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   196
                    return "";
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   197
                }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   198
                StringBuilder sb = new StringBuilder();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   199
                InnerClass firstClass = innerClasses.get(0);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   200
                if (firstClass.isClass()) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   201
                    sb.append("extends ").append(firstClass.getSimpleName()).append(" ");
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   202
                }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   203
                String str = innerClasses.stream()
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   204
                        .filter(x -> !x.isClass())
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   205
                        .map(InnerClass::getSimpleName)
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   206
                        .collect(Collectors.joining(", "));
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   207
                if (!str.isEmpty()) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   208
                    sb.append("implements ").append(str);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   209
                }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   210
                return sb.toString();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   211
            }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   212
        }, INTERFACE("interface") {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   213
            @Override
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   214
            public boolean canBeInherited(List<InnerClass> innerClasses) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   215
                return !innerClasses.stream().anyMatch(InnerClass::isClass);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   216
            }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   217
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   218
            @Override
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   219
            public String generateInheritanceString(List<InnerClass> innerClasses) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   220
                if (innerClasses.isEmpty()) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   221
                    return "";
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   222
                }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   223
                return "extends " + innerClasses.stream()
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   224
                        .map(InnerClass::getSimpleName)
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   225
                        .collect(Collectors.joining(", "));
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   226
            }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   227
        };
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   228
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   229
        private final String classType;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   230
        private ClassType(String classType) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   231
            this.classType = classType;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   232
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   233
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   234
        public String getClassType() {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   235
            return classType;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   236
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   237
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   238
        public abstract boolean canBeInherited(List<InnerClass> innerClasses);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   239
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   240
        public abstract String generateInheritanceString(List<InnerClass> innerClasses);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   241
    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   242
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   243
    enum InnerClass {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   244
        INNER_1("pkg.Test.InnerClass.Inner1", true),
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   245
        INNER_2("pkg.Test.InnerClass.Inner2", true),
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   246
        INNER_3("pkg.Test.InnerClass.Inner3", true),
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   247
        IINNER_1("pkg.Test.InnerClass1.IInner1", false),
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   248
        IINNER_2("pkg.Test.InnerInterface1.IInner2", false),
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   249
        IINNER_3("pkg.Test.InnerInterface2.IInner3", false);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   250
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   251
        private final String canonicalName;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   252
        private final boolean isForTypeParameter;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   253
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   254
        private InnerClass(String canonicalName, boolean isForTypeParameter) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   255
            this.canonicalName = canonicalName;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   256
            this.isForTypeParameter = isForTypeParameter;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   257
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   258
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   259
        private static List<List<InnerClass>> getAllCombinations(boolean isTypeParameter) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   260
            List<List<InnerClass>> result = new ArrayList<>();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   261
            List<InnerClass> tmpl = Stream.of(InnerClass.values())
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   262
                    .filter(i -> i.isForTypeParameter() == isTypeParameter)
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   263
                    .collect(Collectors.toCollection(ArrayList::new));
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   264
            result.add(Arrays.asList());
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   265
            for (int i = 0; i < tmpl.size(); ++i) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   266
                result.add(Arrays.asList(tmpl.get(i)));
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   267
                for (int j = i + 1; j < tmpl.size(); ++j) {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   268
                    result.add(Arrays.asList(tmpl.get(i), tmpl.get(j)));
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   269
                }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   270
            }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   271
            result.add(tmpl);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   272
            return result;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   273
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   274
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   275
        public static List<List<InnerClass>> getAllCombinationsForTypeParameter() {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   276
            return getAllCombinations(true);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   277
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   278
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   279
        public static List<List<InnerClass>> getAllCombinationsForInheritance() {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   280
            return getAllCombinations(false);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   281
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   282
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   283
        public String getCanonicalName() {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   284
            return canonicalName;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   285
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   286
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   287
        public String getSimpleName() {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   288
            String cName = getCanonicalName();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   289
            return cName.substring(cName.lastIndexOf('.') + 1);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   290
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   291
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   292
        public String getPackageName() {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   293
            String cName = getCanonicalName();
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   294
            int dotIndex = cName.lastIndexOf('.');
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   295
            return dotIndex == -1 ? "" : cName.substring(0, dotIndex);
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   296
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   297
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   298
        public boolean isClass() {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   299
            return this == INNER_1 || this == IINNER_1;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   300
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   301
        private boolean isForTypeParameter() {
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   302
            return isForTypeParameter;
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   303
        }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   304
    }
44407cab4584 8065360: Implement a test that checks possibilty of class members to be imported
aeremeev
parents:
diff changeset
   305
}