File: GenericConstructorAndDiamondTest.java

package info (click to toggle)
libnb-javaparser-java 7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 35,940 kB
  • ctags: 55,696
  • sloc: java: 264,137; xml: 2,781; sh: 1,135; makefile: 425
file content (272 lines) | stat: -rw-r--r-- 9,507 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 7030150 7039931
 * @summary Type inference for generic instance creation failed for formal type parameter
 */

import com.sun.source.util.JavacTask;
import java.net.URI;
import java.util.Arrays;
import javax.tools.Diagnostic;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

public class GenericConstructorAndDiamondTest {

    enum BoundKind {
        NO_BOUND(""),
        STRING_BOUND("extends String"),
        INTEGER_BOUND("extends Integer");

        String boundStr;

        private BoundKind(String boundStr) {
            this.boundStr = boundStr;
        }

        boolean matches(TypeArgumentKind tak) {
            switch (tak) {
                case NONE: return true;
                case STRING: return this != INTEGER_BOUND;
                case INTEGER: return this != STRING_BOUND;
                default: return false;
            }
        }
    }

    enum ConstructorKind {
        NON_GENERIC("Foo(Object o) {}"),
        GENERIC_NO_BOUND("<T> Foo(T t) {}"),
        GENERIC_STRING_BOUND("<T extends String> Foo(T t) {}"),
        GENERIC_INTEGER_BOUND("<T extends Integer> Foo(T t) {}");

        String constrStr;

        private ConstructorKind(String constrStr) {
            this.constrStr = constrStr;
        }

        boolean matches(ArgumentKind ak) {
            switch (ak) {
                case STRING: return this != GENERIC_INTEGER_BOUND;
                case INTEGER: return this != GENERIC_STRING_BOUND;
                default: return false;
            }
        }
    }

    enum TypeArgArity {
        ONE(1),
        TWO(2),
        THREE(3);

        int n;

        private TypeArgArity(int n) {
            this.n = n;
        }
    }

    enum TypeArgumentKind {
        NONE(""),
        STRING("String"),
        INTEGER("Integer");

        String typeargStr;

        private TypeArgumentKind(String typeargStr) {
            this.typeargStr = typeargStr;
        }

        String getArgs(TypeArgArity arity) {
            if (this == NONE) return "";
            else {
                StringBuilder buf = new StringBuilder();
                String sep = "";
                for (int i = 0 ; i < arity.n ; i++) {
                    buf.append(sep);
                    buf.append(typeargStr);
                    sep = ",";
                }
                return "<" + buf.toString() + ">";
            }
        }

        boolean matches(ArgumentKind ak) {
            switch (ak) {
                case STRING: return this != INTEGER;
                case INTEGER: return this != STRING;
                default: return false;
            }
        }

        boolean matches(TypeArgumentKind other) {
            switch (other) {
                case STRING: return this != INTEGER;
                case INTEGER: return this != STRING;
                default: return true;
            }
        }
    }

    enum ArgumentKind {
        STRING("\"\""),
        INTEGER("1");

        String argStr;

        private ArgumentKind(String argStr) {
            this.argStr = argStr;
        }
    }

    public static void main(String... args) throws Exception {

        //create default shared JavaCompiler - reused across multiple compilations
        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);

        for (BoundKind boundKind : BoundKind.values()) {
            for (ConstructorKind constructorKind : ConstructorKind.values()) {
                for (TypeArgumentKind declArgKind : TypeArgumentKind.values()) {
                    for (TypeArgArity arity : TypeArgArity.values()) {
                        for (TypeArgumentKind useArgKind : TypeArgumentKind.values()) {
                            for (TypeArgumentKind diamondArgKind : TypeArgumentKind.values()) {
                                for (ArgumentKind argKind : ArgumentKind.values()) {
                                    new GenericConstructorAndDiamondTest(boundKind, constructorKind,
                                            declArgKind, arity, useArgKind, diamondArgKind, argKind).run(comp, fm);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    BoundKind boundKind;
    ConstructorKind constructorKind;
    TypeArgumentKind declTypeArgumentKind;
    TypeArgArity useTypeArgArity;
    TypeArgumentKind useTypeArgumentKind;
    TypeArgumentKind diamondTypeArgumentKind;
    ArgumentKind argumentKind;
    JavaSource source;
    DiagnosticChecker diagChecker;

    GenericConstructorAndDiamondTest(BoundKind boundKind, ConstructorKind constructorKind,
            TypeArgumentKind declTypeArgumentKind, TypeArgArity useTypeArgArity,
            TypeArgumentKind useTypeArgumentKind, TypeArgumentKind diamondTypeArgumentKind,
            ArgumentKind argumentKind) {
        this.boundKind = boundKind;
        this.constructorKind = constructorKind;
        this.declTypeArgumentKind = declTypeArgumentKind;
        this.useTypeArgArity = useTypeArgArity;
        this.useTypeArgumentKind = useTypeArgumentKind;
        this.diamondTypeArgumentKind = diamondTypeArgumentKind;
        this.argumentKind = argumentKind;
        this.source = new JavaSource();
        this.diagChecker = new DiagnosticChecker();
    }

    class JavaSource extends SimpleJavaFileObject {

        String template = "class Foo<X #BK> {\n" +
                              "#CK\n" +
                          "}\n" +
                          "class Test {\n" +
                              "void test() {\n" +
                                 "Foo#TA1 f = new #TA2 Foo<#TA3>(#A);\n" +
                              "}\n" +
                          "}\n";

        String source;

        public JavaSource() {
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
            source = template.replace("#BK", boundKind.boundStr).
                    replace("#CK", constructorKind.constrStr)
                    .replace("#TA1", declTypeArgumentKind.getArgs(TypeArgArity.ONE))
                    .replace("#TA2", useTypeArgumentKind.getArgs(useTypeArgArity))
                    .replace("#TA3", diamondTypeArgumentKind.typeargStr)
                    .replace("#A", argumentKind.argStr);
        }

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return source;
        }
    }

    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
                null, null, Arrays.asList(source));
        ct.analyze();
        check();
    }

    void check() {
        boolean badActual = !constructorKind.matches(argumentKind);

        boolean badArity = constructorKind != ConstructorKind.NON_GENERIC &&
                useTypeArgumentKind != TypeArgumentKind.NONE &&
                useTypeArgArity != TypeArgArity.ONE;

        boolean badMethodTypeArg = constructorKind != ConstructorKind.NON_GENERIC &&
                !useTypeArgumentKind.matches(argumentKind);

        boolean badExplicitParams = (useTypeArgumentKind != TypeArgumentKind.NONE &&
                diamondTypeArgumentKind == TypeArgumentKind.NONE) ||
                !boundKind.matches(diamondTypeArgumentKind);

        boolean badGenericType = !boundKind.matches(declTypeArgumentKind) ||
                !diamondTypeArgumentKind.matches(declTypeArgumentKind);

        boolean shouldFail = badActual || badArity ||
                badMethodTypeArg || badExplicitParams || badGenericType;

        if (shouldFail != diagChecker.errorFound) {
            throw new Error("invalid diagnostics for source:\n" +
                source.getCharContent(true) +
                "\nFound error: " + diagChecker.errorFound +
                "\nExpected error: " + shouldFail);
        }
    }

    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {

        boolean errorFound;

        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
                errorFound = true;
            }
        }
    }
}