1 /*
 2  * Copyright (c) 2014, 2024, 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  *  @compile SyntheticClasses.java
29  *  @run main SyntheticClasses
30  */
31 
32 import java.io.*;
33 import java.util.*;
34 import java.lang.classfile.*;
35 import java.lang.classfile.attribute.*;
36 
37 public class SyntheticClasses {
38 
39     public static void main(String[] args) throws IOException {
40         new SyntheticClasses().run();
41     }
42 
43     private void run() throws IOException {
44         File testClasses = new File(System.getProperty("test.classes"));
45         for (File classFile : Objects.requireNonNull(testClasses.listFiles(f -> f.getName().endsWith(".class")))) {
46             ClassModel cf = ClassFile.of().parse(classFile.toPath());
47             if (cf.thisClass().asInternalName().matches(".*\\$[0-9]+")) {
48                 EnclosingMethodAttribute encl = cf.findAttribute(Attributes.enclosingMethod()).orElse(null);
49                 if (encl != null) {
50                     if (encl.enclosingMethodName().isPresent())
51                         throw new IllegalStateException("Invalid EnclosingMethod.method: " +
52                                                         encl.enclosingMethodName().get().stringValue() + ".");
53                 }
54             }
55             InnerClassesAttribute attr = cf.findAttribute(Attributes.innerClasses()).orElse(null);
56             if (attr != null) {
57                 for (InnerClassInfo info : attr.classes()) {
58                     if (cf.majorVersion() < 51)
59                         throw new IllegalStateException();
60                     if (info.innerName().isEmpty() && info.outerClass().isPresent() )
61                         throw new IllegalStateException("Invalid outer_class_info: " +
62                                                         info.outerClass().get().asInternalName() +
63                                                         "; inner_name is empty");
64                 }
65             }
66         }
67     }
68 }
69 
70 class SyntheticConstructorAccessTag {
71 
72     private static class A {
73         private A(){}
74     }
75 
76     public void test() {
77         new A();
78     }
79 }
80 
81 class SyntheticEnumMapping {
82     private int convert(E e) {
83         switch (e) {
84             case A: return 0;
85             default: return -1;
86         }
87     }
88     enum E { A }
89 }
90 
91 interface SyntheticAssertionsDisabled {
92     public default void test() {
93         assert false;
94     }
95 }