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