File: EnclosingMethodTest.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 (540 lines) | stat: -rw-r--r-- 20,517 bytes parent folder | download | duplicates (9)
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*
 * Copyright (c) 2014, 2016, 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 8042931
 * @summary Checking EnclosingMethod attribute of anonymous/local class.
 * @library /tools/lib /tools/javac/lib ../lib
 * @modules jdk.compiler/com.sun.tools.javac.api
 *          jdk.compiler/com.sun.tools.javac.main
 *          jdk.jdeps/com.sun.tools.classfile
 * @build toolbox.ToolBox InMemoryFileManager TestResult TestBase
 * @run main EnclosingMethodTest
 */

import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.ClassFile;
import com.sun.tools.classfile.EnclosingMethod_attribute;

import java.io.File;
import java.io.FilenameFilter;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

/**
 * The test checks the enclosing method attribute of anonymous/local classes.
 * The top-level class contains the anonymous and local classes to be tested. The test examines
 * each inner class and determine whether the class should have the EnclosingMethod attribute or not.
 * Golden information about enclosing methods are held in annotation {@code ExpectedEnclosingMethod}.
 *
 * The test assumes that a class must have the EnclosingMethod attribute if the class is annotated or
 * if its parent class is annotated in case of anonymous class. In addition, classes
 * named {@code VariableInitializer} are introduced to test variable initializer cases. These classes
 * must not have the enclosing method attribute, but its anonymous derived class must.
 * After classification of classes, the test checks whether classes contain the correct enclosing
 * method attribute in case of anonymous/local class, or checks whether classes do not contain
 * the EnclosingMethod attribute, otherwise.
 *
 * Test cases:
 *   top-level class as enclosing class:
 *     1. anonymous and local classes in static initializer;
 *     2. anonymous and local classes in instance initializer;
 *     3. anonymous and local classes in lambda;
 *     4. anonymous and local classes in constructor;
 *     5. anonymous and local classes in method;
 *     6. static and instance variable initializer.
 *
 *   inner class as enclosing class:
 *     1. anonymous and local classes in static initializer;
 *     2. anonymous and local classes in instance initializer;
 *     3. anonymous and local classes in lambda;
 *     4. anonymous and local classes in constructor;
 *     5. anonymous and local classes in method;
 *     6. static and instance variable initializer.
 *
 *   enum as enclosing class:
 *     1. anonymous and local classes in static initializer;
 *     2. anonymous and local classes in instance initializer;
 *     3. anonymous and local classes in lambda;
 *     4. anonymous and local classes in constructor;
 *     5. anonymous and local classes in method;
 *     6. static and instance variable initializer.
 *
 *   interface as enclosing class:
 *     1. anonymous and local classes in lambda;
 *     2. anonymous and local classes in static method;
 *     3. anonymous and local classes in default method;
 *     4. static variable initializer.
 *
 *   annotation as enclosing class:
 *     1. anonymous and local classes in lambda;
 *     2. static variable initializer.
 */
public class EnclosingMethodTest extends TestResult {

    private final Map<Class<?>, ExpectedEnclosingMethod> class2EnclosingMethod = new HashMap<>();
    private final Set<Class<?>> noEnclosingMethod = new HashSet<>();

    public EnclosingMethodTest() throws ClassNotFoundException {
        Class<EnclosingMethodTest> outerClass = EnclosingMethodTest.class;
        String outerClassName = outerClass.getSimpleName();
        File testClasses = getClassDir();
        FilenameFilter filter = (dir, name) -> name.matches(outerClassName + ".*\\.class");

        for (File file : testClasses.listFiles(filter)) {
            Class<?> clazz = Class.forName(file.getName().replace(".class", ""));
            if (clazz.isAnonymousClass()) {
                // anonymous class cannot be annotated, information is in its parent class.
                ExpectedEnclosingMethod declaredAnnotation =
                        clazz.getSuperclass().getDeclaredAnnotation(ExpectedEnclosingMethod.class);
                class2EnclosingMethod.put(clazz, declaredAnnotation);
            } else {
                ExpectedEnclosingMethod enclosingMethod = clazz.getDeclaredAnnotation(ExpectedEnclosingMethod.class);
                // if class is annotated and it does not contain information for variable initializer cases,
                // then it must have the enclosing method attribute.
                if (enclosingMethod != null && !clazz.getSimpleName().contains("VariableInitializer")) {
                    class2EnclosingMethod.put(clazz, enclosingMethod);
                } else {
                    noEnclosingMethod.add(clazz);
                }
            }
        }
    }

    public void test() throws TestFailedException {
        try {
            testEnclosingMethodAttribute();
            testLackOfEnclosingMethodAttribute();
        } finally {
            checkStatus();
        }
    }

    private void testLackOfEnclosingMethodAttribute() {
        for (Class<?> clazz : noEnclosingMethod) {
            try {
                addTestCase("Class should not have EnclosingMethod attribute : " + clazz);
                ClassFile classFile = readClassFile(clazz);
                checkEquals(countEnclosingMethodAttributes(classFile),
                        0l, "number of the EnclosingMethod attribute in the class is zero : "
                                + classFile.getName());
            } catch (Exception e) {
                addFailure(e);
            }
        }
    }

    private void testEnclosingMethodAttribute() {
        class2EnclosingMethod.forEach((clazz, enclosingMethod) -> {
            try {
                String info = enclosingMethod.info() + " "
                        + (clazz.isAnonymousClass() ? "anonymous" : "local");
                addTestCase(info);
                printf("Testing test case : %s\n", info);
                ClassFile classFile = readClassFile(clazz);
                String className = clazz.getName();
                checkEquals(countEnclosingMethodAttributes(classFile), 1l,
                        "number of the EnclosingMethod attribute in the class is one : "
                                + clazz);
                EnclosingMethod_attribute attr = (EnclosingMethod_attribute)
                        classFile.getAttribute(Attribute.EnclosingMethod);

                if (!checkNotNull(attr, "the EnclosingMethod attribute is not null : " + className)) {
                    // stop checking, attr is null. test case failed
                    return;
                }
                checkEquals(classFile.constant_pool.getUTF8Value(attr.attribute_name_index),
                        "EnclosingMethod",
                        "attribute_name_index of EnclosingMethod attribute in the class : " + className);
                checkEquals(attr.attribute_length, 4,
                        "attribute_length of EnclosingMethod attribute in the class : " + className);
                String expectedClassName = enclosingMethod.enclosingClazz().getName();
                checkEquals(classFile.constant_pool.getClassInfo(attr.class_index).getName(),
                        expectedClassName, String.format(
                        "enclosing class of EnclosingMethod attribute in the class %s is %s",
                                className, expectedClassName));

                String expectedMethodName = enclosingMethod.enclosingMethod();
                if (expectedMethodName.isEmpty()) {
                    // class does not have an enclosing method
                    checkEquals(attr.method_index, 0, String.format(
                            "enclosing method of EnclosingMethod attribute in the class %s is null", className));
                } else {
                    String methodName = classFile.constant_pool.getNameAndTypeInfo(attr.method_index).getName();
                    checkTrue(methodName.startsWith(expectedMethodName), String.format(
                            "enclosing method of EnclosingMethod attribute in the class %s" +
                                    " is method name %s" +
                                    ", actual method name is %s",
                            className, expectedMethodName, methodName));
                }
            } catch (Exception e) {
                addFailure(e);
            }
        });
    }

    private long countEnclosingMethodAttributes(ClassFile classFile) {
        return Stream.of(classFile.attributes.attrs)
                .filter(x -> x instanceof EnclosingMethod_attribute)
                .count();
    }

    @Retention(RetentionPolicy.RUNTIME)
    public @interface ExpectedEnclosingMethod {
        String info();
        Class<?> enclosingClazz();
        String enclosingMethod() default "";
    }

    public static void main(String[] args) throws ClassNotFoundException, TestFailedException {
        new EnclosingMethodTest().test();
    }

    // Test cases: enclosing class is a top-level class
    static {
        // anonymous and local classes in static initializer
        @ExpectedEnclosingMethod(
                info = "EnclosingStaticInitialization in EnclosingMethodTest",
                enclosingClazz = EnclosingMethodTest.class
        )
        class EnclosingStaticInitialization {
        }
        new EnclosingStaticInitialization() {
        };
    }

    {
        // anonymous and local classes in instance initializer
        @ExpectedEnclosingMethod(
                info = "EnclosingInitialization in EnclosingMethodTest",
                enclosingClazz = EnclosingMethodTest.class
        )
        class EnclosingInitialization {
        }
        new EnclosingInitialization() {
        };
    }

    Runnable lambda = () -> {
        // anonymous and local classes in lambda
        @ExpectedEnclosingMethod(
                info = "EnclosingLambda in EnclosingMethodTest",
                enclosingMethod = "lambda",
                enclosingClazz = EnclosingMethodTest.class
        )
        class EnclosingLambda {
        }
        new EnclosingLambda() {
        };
    };

    EnclosingMethodTest(int i) {
        // anonymous and local classes in constructor
        @ExpectedEnclosingMethod(
                info = "EnclosingConstructor in EnclosingMethodTest",
                enclosingMethod = "<init>",
                enclosingClazz = EnclosingMethodTest.class
        )
        class EnclosingConstructor {
        }
        new EnclosingConstructor() {
        };
    }

    void method() {
        // anonymous and local classes in method
        @ExpectedEnclosingMethod(
                info = "EnclosingMethod in EnclosingMethodTest",
                enclosingMethod = "method",
                enclosingClazz = EnclosingMethodTest.class
        )
        class EnclosingMethod {
        }
        new EnclosingMethod() {
        };
    }

    @ExpectedEnclosingMethod(
            info = "VariableInitializer in EnclosingMethodTest",
            enclosingClazz = EnclosingMethodTest.class
    )
    static class VariableInitializer {
    }

    // static variable initializer
    private static final VariableInitializer cvi = new VariableInitializer() {
    };

    // instance variable initializer
    private final VariableInitializer ivi = new VariableInitializer() {
    };

    // Test cases: enclosing class is an inner class
    public static class notEnclosing01 {
        static {
            // anonymous and local classes in static initializer
            @ExpectedEnclosingMethod(
                    info = "EnclosingStaticInitialization in notEnclosing01",
                    enclosingClazz = notEnclosing01.class
            )
            class EnclosingStaticInitialization {
            }
            new EnclosingStaticInitialization() {
            };
        }

        {
            // anonymous and local classes in instance initializer
            @ExpectedEnclosingMethod(
                    info = "EnclosingInitialization in notEnclosing01",
                    enclosingClazz = notEnclosing01.class
            )
            class EnclosingInitialization {
            }
            new EnclosingInitialization() {
            };
        }

        Runnable lambda = () -> {
            // anonymous and local classes in lambda
            @ExpectedEnclosingMethod(
                    info = "EnclosingLambda in notEnclosing01",
                    enclosingMethod = "lambda",
                    enclosingClazz = notEnclosing01.class
            )
            class EnclosingLambda {
            }
            new EnclosingLambda() {
            };
        };

        notEnclosing01() {
            // anonymous and local classes in constructor
            @ExpectedEnclosingMethod(
                    info = "EnclosingConstructor in notEnclosing01",
                    enclosingMethod = "<init>",
                    enclosingClazz = notEnclosing01.class
            )
            class EnclosingConstructor {
            }
            new EnclosingConstructor() {
            };
        }

        void method() {
            // anonymous and local classes in method
            @ExpectedEnclosingMethod(
                    info = "EnclosingMethod in notEnclosing01",
                    enclosingMethod = "method",
                    enclosingClazz = notEnclosing01.class
            )
            class EnclosingMethod {
            }
            new EnclosingMethod() {
            };
        }

        @ExpectedEnclosingMethod(
                info = "VariableInitializer in notEnclosing01",
                enclosingClazz = notEnclosing01.class
        )
        static class VariableInitializer {
        }

        // static variable initializer
        private static final VariableInitializer cvi = new VariableInitializer() {
        };

        // instance variable initializer
        private final VariableInitializer ivi = new VariableInitializer() {
        };
    }

    // Test cases: enclosing class is an interface
    public interface notEnclosing02 {
        Runnable lambda = () -> {
            // anonymous and local classes in lambda
            @ExpectedEnclosingMethod(
                    info = "EnclosingLambda in notEnclosing02",
                    enclosingMethod = "lambda",
                    enclosingClazz = notEnclosing02.class
            )
            class EnclosingLambda {
            }
            new EnclosingLambda() {
            };
        };

        static void staticMethod() {
            // anonymous and local classes in static method
            @ExpectedEnclosingMethod(
                    info = "EnclosingMethod in notEnclosing02",
                    enclosingMethod = "staticMethod",
                    enclosingClazz = notEnclosing02.class
            )
            class EnclosingMethod {
            }
            new EnclosingMethod() {
            };
        }

        default void defaultMethod() {
            // anonymous and local classes in default method
            @ExpectedEnclosingMethod(
                    info = "EnclosingMethod in notEnclosing02",
                    enclosingMethod = "defaultMethod",
                    enclosingClazz = notEnclosing02.class
            )
            class EnclosingMethod {
            }
            new EnclosingMethod() {
            };
        }

        @ExpectedEnclosingMethod(
                info = "VariableInitializer in notEnclosing02",
                enclosingClazz = notEnclosing02.class
        )
        static class VariableInitializer {
        }

        // static variable initializer
        VariableInitializer cvi = new VariableInitializer() {
        };
    }

    // Test cases: enclosing class is an enum
    public enum notEnclosing03 {;

        static {
            // anonymous and local classes in static initializer
            @ExpectedEnclosingMethod(
                    info = "EnclosingStaticInitialization in notEnclosing03",
                    enclosingClazz = notEnclosing03.class
            )
            class EnclosingStaticInitialization {
            }
            new EnclosingStaticInitialization() {
            };
        }

        {
            // anonymous and local classes in instance initializer
            @ExpectedEnclosingMethod(
                    info = "EnclosingInitialization in notEnclosing03",
                    enclosingClazz = notEnclosing03.class
            )
            class EnclosingInitialization {
            }
            new EnclosingInitialization() {
            };
        }

        Runnable lambda = () -> {
            // anonymous and local classes in lambda
            @ExpectedEnclosingMethod(
                    info = "EnclosingLambda in notEnclosing03",
                    enclosingMethod = "lambda",
                    enclosingClazz = notEnclosing03.class
            )
            class EnclosingLambda {
            }
            new EnclosingLambda() {
            };
        };

        notEnclosing03() {
            // anonymous and local classes in constructor
            @ExpectedEnclosingMethod(
                    info = "EnclosingConstructor in notEnclosing03",
                    enclosingMethod = "<init>",
                    enclosingClazz = notEnclosing03.class
            )
            class EnclosingConstructor {
            }
            new EnclosingConstructor() {
            };
        }

        void method() {
            // anonymous and local classes in method
            @ExpectedEnclosingMethod(
                    info = "EnclosingMethod in notEnclosing03",
                    enclosingMethod = "method",
                    enclosingClazz = notEnclosing03.class
            )
            class EnclosingMethod {
            }
            new EnclosingMethod() {
            };
        }

        @ExpectedEnclosingMethod(
                info = "VariableInitializer in notEnclosing03",
                enclosingClazz = notEnclosing03.class
        )
        static class VariableInitializer {
        }

        // static variable initializer
        private static final VariableInitializer cvi = new VariableInitializer() {
        };

        // instance variable initializer
        private final VariableInitializer ivi = new VariableInitializer() {
        };
    }

    // Test cases: enclosing class is an annotation
    public @interface notEnclosing04 {
        Runnable lambda = () -> {
            // anonymous and local classes in lambda
            @ExpectedEnclosingMethod(
                    info = "EnclosingLambda in notEnclosing04",
                    enclosingMethod = "lambda",
                    enclosingClazz = notEnclosing04.class
            )
            class EnclosingLambda {
            }
            new EnclosingLambda() {
            };
        };

        @ExpectedEnclosingMethod(
                info = "VariableInitializer in notEnclosing04",
                enclosingClazz = notEnclosing04.class
        )
        static class VariableInitializer {
        }

        // static variable initializer
        VariableInitializer cvi = new VariableInitializer() {
        };
    }
}