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