File: DiamondAndInnerClassTest.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 (367 lines) | stat: -rw-r--r-- 16,161 bytes parent folder | download
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Copyright (c) 2011, 2013, 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 7046778 8006694
 * @summary Project Coin: problem with diamond and member inner classes
 *  temporarily workaround combo tests are causing time out in several platforms
 * @library ../../../lib
 * @build JavacTestingAbstractThreadedTest
 * @run main/othervm DiamondAndInnerClassTest
 */

// use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
// see JDK-8006746

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

public class DiamondAndInnerClassTest
    extends JavacTestingAbstractThreadedTest
    implements Runnable {

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

        String typeargStr;

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

        boolean compatible(TypeArgumentKind that) {
            switch (this) {
                case NONE: return true;
                case STRING: return that != INTEGER;
                case INTEGER: return that != STRING;
                default: throw new AssertionError("Unexpected decl kind: " + this);
            }
        }

        boolean compatible(ArgumentKind that) {
            switch (this) {
                case NONE: return true;
                case STRING: return that == ArgumentKind.STRING;
                case INTEGER: return that == ArgumentKind.INTEGER;
                default: throw new AssertionError("Unexpected decl kind: " + this);
            }
        }
    }

    enum ArgumentKind {
        OBJECT("(Object)null"),
        STRING("(String)null"),
        INTEGER("(Integer)null");

        String argStr;

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

    enum TypeQualifierArity {
        ONE(1, "A1#TA1"),
        TWO(2, "A1#TA1.A2#TA2"),
        THREE(3, "A1#TA1.A2#TA2.A3#TA3");

        int n;
        String qualifierStr;

        private TypeQualifierArity(int n, String qualifierStr) {
            this.n = n;
            this.qualifierStr = qualifierStr;
        }

        String getType(TypeArgumentKind... typeArgumentKinds) {
            String res = qualifierStr;
            for (int i = 1 ; i <= typeArgumentKinds.length ; i++) {
                res = res.replace("#TA" + i, typeArgumentKinds[i-1].typeargStr);
            }
            return res;
        }

        boolean matches(InnerClassDeclArity innerClassDeclArity) {
            return n ==innerClassDeclArity.n;
        }
    }

    enum InnerClassDeclArity {
        ONE(1, "class A1<X> { A1(X x1) { } #B }"),
        TWO(2, "class A1<X1> { class A2<X2> { A2(X1 x1, X2 x2) { }  #B } }"),
        THREE(3, "class A1<X1> { class A2<X2> { class A3<X3> { A3(X1 x1, X2 x2, X3 x3) { } #B } } }");

        int n;
        String classDeclStr;

        private InnerClassDeclArity(int n, String classDeclStr) {
            this.n = n;
            this.classDeclStr = classDeclStr;
        }
    }

    enum ArgumentListArity {
        ONE(1, "(#A1)"),
        TWO(2, "(#A1,#A2)"),
        THREE(3, "(#A1,#A2,#A3)");

        int n;
        String argListStr;

        private ArgumentListArity(int n, String argListStr) {
            this.n = n;
            this.argListStr = argListStr;
        }

        String getArgs(ArgumentKind... argumentKinds) {
            String res = argListStr;
            for (int i = 1 ; i <= argumentKinds.length ; i++) {
                res = res.replace("#A" + i, argumentKinds[i-1].argStr);
            }
            return res;
        }

        boolean matches(InnerClassDeclArity innerClassDeclArity) {
            return n ==innerClassDeclArity.n;
        }
    }

    public static void main(String... args) throws Exception {
        for (InnerClassDeclArity innerClassDeclArity : InnerClassDeclArity.values()) {
            for (TypeQualifierArity declType : TypeQualifierArity.values()) {
                if (!declType.matches(innerClassDeclArity)) continue;
                for (TypeQualifierArity newClassType : TypeQualifierArity.values()) {
                    if (!newClassType.matches(innerClassDeclArity)) continue;
                    for (ArgumentListArity argList : ArgumentListArity.values()) {
                        if (!argList.matches(innerClassDeclArity)) continue;
                        for (TypeArgumentKind taDecl1 : TypeArgumentKind.values()) {
                            boolean isDeclRaw = taDecl1 == TypeArgumentKind.NONE;
                            //no diamond on decl site
                            if (taDecl1 == TypeArgumentKind.DIAMOND) continue;
                            for (TypeArgumentKind taSite1 : TypeArgumentKind.values()) {
                                boolean isSiteRaw =
                                        taSite1 == TypeArgumentKind.NONE;
                                //diamond only allowed on the last type qualifier
                                if (taSite1 == TypeArgumentKind.DIAMOND &&
                                        innerClassDeclArity !=
                                        InnerClassDeclArity.ONE)
                                    continue;
                                for (ArgumentKind arg1 : ArgumentKind.values()) {
                                    if (innerClassDeclArity == innerClassDeclArity.ONE) {
                                        pool.execute(
                                                new DiamondAndInnerClassTest(
                                                innerClassDeclArity, declType,
                                                newClassType, argList,
                                                new TypeArgumentKind[] {taDecl1},
                                                new TypeArgumentKind[] {taSite1},
                                                new ArgumentKind[] {arg1}));
                                        continue;
                                    }
                                    for (TypeArgumentKind taDecl2 : TypeArgumentKind.values()) {
                                        //no rare types
                                        if (isDeclRaw != (taDecl2 == TypeArgumentKind.NONE))
                                            continue;
                                        //no diamond on decl site
                                        if (taDecl2 == TypeArgumentKind.DIAMOND)
                                            continue;
                                        for (TypeArgumentKind taSite2 : TypeArgumentKind.values()) {
                                            //no rare types
                                            if (isSiteRaw != (taSite2 == TypeArgumentKind.NONE))
                                                continue;
                                            //diamond only allowed on the last type qualifier
                                            if (taSite2 == TypeArgumentKind.DIAMOND &&
                                                    innerClassDeclArity != InnerClassDeclArity.TWO)
                                                continue;
                                            for (ArgumentKind arg2 : ArgumentKind.values()) {
                                                if (innerClassDeclArity == innerClassDeclArity.TWO) {
                                                    pool.execute(
                                                            new DiamondAndInnerClassTest(
                                                            innerClassDeclArity,
                                                            declType,
                                                            newClassType,
                                                            argList,
                                                            new TypeArgumentKind[] {taDecl1, taDecl2},
                                                            new TypeArgumentKind[] {taSite1, taSite2},
                                                            new ArgumentKind[] {arg1, arg2}));
                                                    continue;
                                                }
                                                for (TypeArgumentKind taDecl3 : TypeArgumentKind.values()) {
                                                    //no rare types
                                                    if (isDeclRaw != (taDecl3 == TypeArgumentKind.NONE))
                                                        continue;
                                                    //no diamond on decl site
                                                    if (taDecl3 == TypeArgumentKind.DIAMOND)
                                                        continue;
                                                    for (TypeArgumentKind taSite3 : TypeArgumentKind.values()) {
                                                        //no rare types
                                                        if (isSiteRaw != (taSite3 == TypeArgumentKind.NONE))
                                                            continue;
                                                        //diamond only allowed on the last type qualifier
                                                        if (taSite3 == TypeArgumentKind.DIAMOND &&
                                                            innerClassDeclArity != InnerClassDeclArity.THREE)
                                                            continue;
                                                        for (ArgumentKind arg3 : ArgumentKind.values()) {
                                                            if (innerClassDeclArity ==
                                                                    innerClassDeclArity.THREE) {
                                                                pool.execute(
                                                                        new DiamondAndInnerClassTest(
                                                                        innerClassDeclArity,
                                                                        declType,
                                                                        newClassType,
                                                                        argList,
                                                                        new TypeArgumentKind[] {taDecl1, taDecl2, taDecl3},
                                                                        new TypeArgumentKind[] {taSite1, taSite2, taSite3},
                                                                        new ArgumentKind[] {arg1, arg2, arg3}));
                                                                continue;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        checkAfterExec();
    }

    InnerClassDeclArity innerClassDeclArity;
    TypeQualifierArity declType;
    TypeQualifierArity siteType;
    ArgumentListArity argList;
    TypeArgumentKind[] declTypeArgumentKinds;
    TypeArgumentKind[] siteTypeArgumentKinds;
    ArgumentKind[] argumentKinds;
    JavaSource source;
    DiagnosticChecker diagChecker;

    DiamondAndInnerClassTest(InnerClassDeclArity innerClassDeclArity,
            TypeQualifierArity declType, TypeQualifierArity siteType,
            ArgumentListArity argList, TypeArgumentKind[] declTypeArgumentKinds,
            TypeArgumentKind[] siteTypeArgumentKinds, ArgumentKind[] argumentKinds) {
        this.innerClassDeclArity = innerClassDeclArity;
        this.declType = declType;
        this.siteType = siteType;
        this.argList = argList;
        this.declTypeArgumentKinds = declTypeArgumentKinds;
        this.siteTypeArgumentKinds = siteTypeArgumentKinds;
        this.argumentKinds = argumentKinds;
        this.source = new JavaSource();
        this.diagChecker = new DiagnosticChecker();
    }

    class JavaSource extends SimpleJavaFileObject {

        String bodyTemplate = "#D res = new #S#AL;";

        String source;

        public JavaSource() {
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
            source = innerClassDeclArity.classDeclStr.replace("#B", bodyTemplate)
                    .replace("#D", declType.getType(declTypeArgumentKinds))
                    .replace("#S", siteType.getType(siteTypeArgumentKinds))
                    .replace("#AL", argList.getArgs(argumentKinds));
        }

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

    @Override
    public void run() {
        JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
                null, null, Arrays.asList(source));
        try {
            ct.analyze();
        } catch (Throwable ex) {
            throw new AssertionError("Error thrown when compiling the following code:\n" +
                    source.getCharContent(true));
        }
        check();
    }

    void check() {
        checkCount.incrementAndGet();

        boolean errorExpected = false;

        TypeArgumentKind[] expectedArgKinds =
                new TypeArgumentKind[innerClassDeclArity.n];

        for (int i = 0 ; i < innerClassDeclArity.n ; i++) {
            if (!declTypeArgumentKinds[i].compatible(siteTypeArgumentKinds[i])) {
                errorExpected = true;
                break;
            }
            expectedArgKinds[i] = siteTypeArgumentKinds[i] ==
                    TypeArgumentKind.DIAMOND ?
                declTypeArgumentKinds[i] : siteTypeArgumentKinds[i];
        }

        if (!errorExpected) {
            for (int i = 0 ; i < innerClassDeclArity.n ; i++) {
                if (!expectedArgKinds[i].compatible(argumentKinds[i])) {
                    errorExpected = true;
                    break;
                }
            }
        }

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

    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;
            }
        }
    }

}