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.flags().flagsMask() & (ClassFile.ACC_SYNTHETIC | ClassFile.ACC_ABSTRACT)) == ClassFile.ACC_SYNTHETIC) {
 48                 if ((cf.flags().flagsMask() & ClassFile.ACC_IDENTITY) == 0) {
 49                     throw new IllegalStateException("Missing ACC_IDENTITY on synthetic concrete identity class: " + cf.thisClass().asInternalName());
 50                 }
 51             }
 52             if (cf.thisClass().asInternalName().matches(".*\\$[0-9]+")) {
 53                 EnclosingMethodAttribute encl = cf.findAttribute(Attributes.enclosingMethod()).orElse(null);
 54                 if (encl != null) {
 55                     if (encl.enclosingMethodName().isPresent())
 56                         throw new IllegalStateException("Invalid EnclosingMethod.method: " +
 57                                                         encl.enclosingMethodName().get().stringValue() + ".");
 58                 }
 59             }
 60             InnerClassesAttribute attr = cf.findAttribute(Attributes.innerClasses()).orElse(null);
 61             if (attr != null) {
 62                 for (InnerClassInfo info : attr.classes()) {
 63                     if (cf.majorVersion() < 51)
 64                         throw new IllegalStateException();
 65                     if (info.innerName().isEmpty() && info.outerClass().isPresent() )
 66                         throw new IllegalStateException("Invalid outer_class_info: " +
 67                                                         info.outerClass().get().asInternalName() +
 68                                                         "; inner_name is empty");
 69                 }
 70             }
 71         }
 72     }
 73 }
 74 
 75 class SyntheticConstructorAccessTag {
 76 
 77     private static class A {
 78         private A(){}
 79     }
 80 
 81     public void test() {
 82         new A();
 83     }
 84 }
 85 
 86 class SyntheticEnumMapping {
 87     private int convert(E e) {
 88         switch (e) {
 89             case A: return 0;
 90             default: return -1;
 91         }
 92     }
 93     enum E { A }
 94 }
 95 
 96 interface SyntheticAssertionsDisabled {
 97     public default void test() {
 98         assert false;
 99     }
100 }