1 /*
 2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /** @test
25  *  @bug 8034854
26  *  @summary Verify that the InnerClasses attribute has outer_class_info_index zero if it has
27  *           inner_name_index zero (for synthetic classes)
28  *  @enablePreview
29  *  @modules java.base/jdk.internal.classfile.impl
30  *  @compile SyntheticClasses.java
31  *  @run main SyntheticClasses
32  */
33 
34 import java.io.*;
35 import java.util.*;
36 import java.lang.classfile.*;
37 import java.lang.classfile.attribute.*;
38 
39 public class SyntheticClasses {
40 
41     public static void main(String[] args) throws IOException {
42         new SyntheticClasses().run();
43     }
44 
45     private void run() throws IOException {
46         File testClasses = new File(System.getProperty("test.classes"));
47         for (File classFile : Objects.requireNonNull(testClasses.listFiles(f -> f.getName().endsWith(".class")))) {
48             ClassModel cf = ClassFile.of().parse(classFile.toPath());
49             if (cf.thisClass().asInternalName().matches(".*\\$[0-9]+")) {
50                 EnclosingMethodAttribute encl = cf.findAttribute(Attributes.ENCLOSING_METHOD).orElse(null);
51                 if (encl != null) {
52                     if (encl.enclosingMethodName().isPresent())
53                         throw new IllegalStateException("Invalid EnclosingMethod.method: " +
54                                                         encl.enclosingMethodName().get().stringValue() + ".");
55                 }
56             }
57             InnerClassesAttribute attr = cf.findAttribute(Attributes.INNER_CLASSES).orElse(null);
58             if (attr != null) {
59                 for (InnerClassInfo info : attr.classes()) {
60                     if (cf.majorVersion() < 51)
61                         throw new IllegalStateException();
62                     if (info.innerName().isEmpty() && info.outerClass().isPresent() )
63                         throw new IllegalStateException("Invalid outer_class_info: " +
64                                                         info.outerClass().get().asInternalName() +
65                                                         "; inner_name is empty");
66                 }
67             }
68         }
69     }
70 }
71 
72 class SyntheticConstructorAccessTag {
73 
74     private static class A {
75         private A(){}
76     }
77 
78     public void test() {
79         new A();
80     }
81 }
82 
83 class SyntheticEnumMapping {
84     private int convert(E e) {
85         switch (e) {
86             case A: return 0;
87             default: return -1;
88         }
89     }
90     enum E { A }
91 }
92 
93 interface SyntheticAssertionsDisabled {
94     public default void test() {
95         assert false;
96     }
97 }