File: TestDefaultSuperCall.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 65,172 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (383 lines) | stat: -rw-r--r-- 12,927 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * Copyright (c) 2012, 2015, 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 7192246 8006694 8129962
 * @summary Automatic test for checking correctness of default super/this resolution
 *  temporarily workaround combo tests are causing time out in several platforms
 * @library /tools/javac/lib
 * @modules jdk.compiler/com.sun.tools.javac.api
 *          jdk.compiler/com.sun.tools.javac.code
 *          jdk.compiler/com.sun.tools.javac.comp
 *          jdk.compiler/com.sun.tools.javac.main
 *          jdk.compiler/com.sun.tools.javac.tree
 *          jdk.compiler/com.sun.tools.javac.util
 * @build combo.ComboTestHelper
 * @run main TestDefaultSuperCall
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import combo.ComboInstance;
import combo.ComboParameter;
import combo.ComboTask.Result;
import combo.ComboTestHelper;

public class TestDefaultSuperCall extends ComboInstance<TestDefaultSuperCall> {

    enum InterfaceKind implements ComboParameter {
        DEFAULT("interface A extends B { default void m() { } }"),
        ABSTRACT("interface A extends B { void m(); }"),
        NONE("interface A extends B { }");

        String interfaceStr;

        InterfaceKind(String interfaceStr) {
            this.interfaceStr = interfaceStr;
        }

        boolean methodDefined() {
            return this == DEFAULT;
        }

        @Override
        public String expand(String optParameter) {
            return interfaceStr;
        }
    }

    enum PruneKind implements ComboParameter {
        NO_PRUNE("interface C { }"),
        PRUNE("interface C extends A { }");

        boolean methodDefined(InterfaceKind ik) {
            return this == PRUNE &&
                    ik.methodDefined();
        }

        String interfaceStr;

        PruneKind(String interfaceStr) {
            this.interfaceStr = interfaceStr;
        }

        @Override
        public String expand(String optParameter) {
            return interfaceStr;
        }
    }

    enum QualifierKind implements ComboParameter {
        DIRECT_1("C"),
        DIRECT_2("A"),
        INDIRECT("B"),
        UNRELATED("E"),
        ENCLOSING_1("name0"),
        ENCLOSING_2("name1");

        String qualifierStr;

        QualifierKind(String qualifierStr) {
            this.qualifierStr = qualifierStr;
        }

        boolean isEnclosing() {
            return this == ENCLOSING_1 ||
                    this == ENCLOSING_2;
        }

        boolean allowSuperCall(InterfaceKind ik, PruneKind pk) {
            switch (this) {
                case DIRECT_1:
                    return pk.methodDefined(ik);
                case DIRECT_2:
                    return ik.methodDefined() && pk == PruneKind.NO_PRUNE;
                default:
                    return false;
            }
        }

        @Override
        public String expand(String optParameter) {
            return qualifierStr;
        }
    }

    enum ExprKind implements ComboParameter {
        THIS("this"),
        SUPER("super");

        String exprStr;

        ExprKind(String exprStr) {
            this.exprStr = exprStr;
        }

        @Override
        public String expand(String optParameter) {
            return exprStr;
        }
    }

    enum ElementKind implements ComboParameter {
        INTERFACE("interface name#CURR { #BODY }", true),
        INTERFACE_EXTENDS("interface name#CURR extends A, C { #BODY }", true),
        CLASS("class name#CURR { #BODY }", false),
        CLASS_EXTENDS("abstract class name#CURR implements A, C { #BODY }", false),
        STATIC_CLASS("static class name#CURR { #BODY }", true),
        STATIC_CLASS_EXTENDS("abstract static class name#CURR implements A, C { #BODY }", true),
        ANON_CLASS("new Object() { #BODY };", false),
        METHOD("void test() { #BODY }", false),
        STATIC_METHOD("static void test() { #BODY }", true),
        DEFAULT_METHOD("default void test() { #BODY }", false);

        String templateDecl;
        boolean isStatic;

        ElementKind(String templateDecl, boolean isStatic) {
            this.templateDecl = templateDecl;
            this.isStatic = isStatic;
        }

        boolean isClassDecl() {
            switch(this) {
                case METHOD:
                case STATIC_METHOD:
                case DEFAULT_METHOD:
                    return false;
                default:
                    return true;
            }
        }

        boolean isAllowedEnclosing(ElementKind ek, boolean isTop) {
            switch (this) {
                case CLASS:
                case CLASS_EXTENDS:
                    //class is implicitly static inside interface, so skip this combo
                    return ek.isClassDecl() &&
                            ek != INTERFACE && ek != INTERFACE_EXTENDS;
                case ANON_CLASS:
                    return !ek.isClassDecl();
                case METHOD:
                    return ek == CLASS || ek == CLASS_EXTENDS ||
                            ek == STATIC_CLASS || ek == STATIC_CLASS_EXTENDS ||
                            ek == ANON_CLASS;
                case INTERFACE:
                case INTERFACE_EXTENDS:
                case STATIC_CLASS:
                case STATIC_CLASS_EXTENDS:
                case STATIC_METHOD:
                    return (isTop && (ek == CLASS || ek == CLASS_EXTENDS)) ||
                            ek == STATIC_CLASS || ek == STATIC_CLASS_EXTENDS;
                case DEFAULT_METHOD:
                    return ek == INTERFACE || ek == INTERFACE_EXTENDS;
                default:
                    throw new AssertionError("Bad enclosing element kind" + this);
            }
        }

        boolean isAllowedTop() {
            switch (this) {
                case CLASS:
                case CLASS_EXTENDS:
                case INTERFACE:
                case INTERFACE_EXTENDS:
                    return true;
                default:
                    return false;
            }
        }

        boolean hasSuper() {
            return this == INTERFACE_EXTENDS ||
                    this == STATIC_CLASS_EXTENDS ||
                    this == CLASS_EXTENDS;
        }

        @Override
        public String expand(String optParameter) {
            int nextDepth = new Integer(optParameter) + 1;
            String replStr = (nextDepth <= 4) ?
                    String.format("#{ELEM[%d].%d}", nextDepth, nextDepth) :
                    "#{QUAL}.#{EXPR}.#{METH}();";
            return templateDecl
                    .replaceAll("#CURR", optParameter)
                    .replaceAll("#BODY", replStr);
        }
    }

    static class Shape {

        List<ElementKind> enclosingElements;
        List<String> enclosingNames;
        List<String> elementsWithMethod;

        Shape(ElementKind... elements) {
            enclosingElements = new ArrayList<>();
            enclosingNames = new ArrayList<>();
            elementsWithMethod = new ArrayList<>();
            int count = 0;
            String prevName = null;
            for (ElementKind ek : elements) {
                String name = "name"+count++;
                if (ek.isStatic) {
                    enclosingElements = new ArrayList<>();
                    enclosingNames = new ArrayList<>();
                }
                if (ek.isClassDecl()) {
                    enclosingElements.add(ek);
                    enclosingNames.add(name);
                } else {
                    elementsWithMethod.add(prevName);
                }
                prevName = name;
            }
        }
    }

    public static void main(String... args) throws Exception {
        new ComboTestHelper<TestDefaultSuperCall>()
                .withFilter(TestDefaultSuperCall::filterBadTopElement)
                .withFilter(TestDefaultSuperCall::filterBadIntermediateElement)
                .withFilter(TestDefaultSuperCall::filterBadTerminalElement)
                .withDimension("INTF1", (x, ik) -> x.ik = ik, InterfaceKind.values())
                .withDimension("INTF2", (x, pk) -> x.pk = pk, PruneKind.values())
                .withArrayDimension("ELEM", (x, elem, idx) -> x.elements[idx] = elem, 5, ElementKind.values())
                .withDimension("QUAL", (x, qk) -> x.qk = qk, QualifierKind.values())
                .withDimension("EXPR", (x, ek) -> x.ek = ek, ExprKind.values())
                .run(TestDefaultSuperCall::new);
    }

    InterfaceKind ik;
    PruneKind pk;
    ElementKind[] elements = new ElementKind[5];
    QualifierKind qk;
    ExprKind ek;

    boolean filterBadTopElement() {
        return elements[0].isAllowedTop();
    }

    boolean filterBadIntermediateElement() {
        for (int i = 1 ; i < 4 ; i++) {
            if (!elements[i].isAllowedEnclosing(elements[i - 1], i == 1)) {
                return false;
            }
        }
        return true;
    }

    boolean filterBadTerminalElement() {
        return elements[4].isAllowedEnclosing(elements[3], false) && !elements[4].isClassDecl();
    }

    String template = "interface E {}\n" +
                      "interface B { }\n" +
                      "#{INTF1}\n" +
                      "#{INTF2}\n" +
                      "#{ELEM[0].0}";

    @Override
    public void doWork() throws IOException {
        check(newCompilationTask()
                .withSourceFromTemplate(template, this::methodName)
                .analyze());
    }

    ComboParameter methodName(String parameterName) {
        switch (parameterName) {
            case "METH":
                String methodName = ek == ExprKind.THIS ? "test" : "m";
                return new ComboParameter.Constant<String>(methodName);
            default:
                return null;
        }
    }

    void check(Result<?> res) {
        Shape sh = new Shape(elements);

        boolean errorExpected = false;

        boolean badEnclosing = false;
        boolean badThis = false;
        boolean badSuper = false;

        if (qk == QualifierKind.ENCLOSING_1 &&
                sh.enclosingNames.size() < 1) {
            errorExpected |= true;
            badEnclosing = true;
        }

        if (qk == QualifierKind.ENCLOSING_2 &&
                sh.enclosingNames.size() < 2) {
            errorExpected |= true;
            badEnclosing = true;
        }

        if (ek == ExprKind.THIS) {
            boolean found = false;
            for (int i = 0; i < sh.enclosingElements.size(); i++) {
                if (sh.enclosingElements.get(i) == ElementKind.ANON_CLASS) continue;
                if (sh.enclosingNames.get(i).equals(qk.qualifierStr)) {
                    found = sh.elementsWithMethod.contains(sh.enclosingNames.get(i));
                    break;
                }
            }
            errorExpected |= !found;
            if (!found) {
                badThis = true;
            }
        }

        if (ek == ExprKind.SUPER) {

            int lastIdx = sh.enclosingElements.size() - 1;
            boolean found = lastIdx == -1 ? false :
                    sh.enclosingElements.get(lastIdx).hasSuper() &&
                    qk.allowSuperCall(ik, pk);

            errorExpected |= !found;
            if (!found) {
                badSuper = true;
            }
        }

        if (res.hasErrors() != errorExpected) {
            fail("Problem when compiling source:\n" +
                    res.compilationInfo() +
                    "\nenclosingElems: " + sh.enclosingElements +
                    "\nenclosingNames: " + sh.enclosingNames +
                    "\nelementsWithMethod: " + sh.elementsWithMethod +
                    "\nbad encl: " + badEnclosing +
                    "\nbad this: " + badThis +
                    "\nbad super: " + badSuper +
                    "\nqual kind: " + qk +
                    "\nfound error: " + res.hasErrors());
        }
    }
}