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 jdk.jdeps/com.sun.tools.classfile
 29  *  @compile SyntheticClasses.java
 30  *  @run main SyntheticClasses
 31  */
 32 
 33 import java.io.*;
 34 import java.util.*;
 35 import com.sun.tools.classfile.*;
 36 
 37 public class SyntheticClasses {
 38 
 39     public static void main(String[] args) throws IOException, ConstantPoolException {
 40         new SyntheticClasses().run();
 41     }
 42 
 43     private void run() throws IOException, ConstantPoolException {
 44         File testClasses = new File(System.getProperty("test.classes"));
 45         for (File classFile : testClasses.listFiles(f -> f.getName().endsWith(".class"))) {
 46             ClassFile cf = ClassFile.read(classFile);
 47             if ((cf.access_flags.flags & (AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_VALUE | AccessFlags.ACC_ABSTRACT)) == AccessFlags.ACC_SYNTHETIC) {
 48                 if ((cf.access_flags.flags & AccessFlags.ACC_IDENTITY) == 0) {
 49                     throw new IllegalStateException("Missing ACC_IDENTITY on synthetic concrete identity class: " + cf.getName());
 50                 }
 51             }
 52             if (cf.getName().matches(".*\\$[0-9]+")) {
 53                 EnclosingMethod_attribute encl =
 54                         (EnclosingMethod_attribute) cf.getAttribute(Attribute.EnclosingMethod);
 55                 if (encl != null) {
 56                     if (encl.method_index != 0)
 57                         throw new IllegalStateException("Invalid EnclosingMethod.method_index: " +
 58                                                         encl.method_index + ".");
 59                 }
 60             }
 61             InnerClasses_attribute attr =
 62                     (InnerClasses_attribute) cf.getAttribute(Attribute.InnerClasses);
 63             if (attr != null) {
 64                 for (InnerClasses_attribute.Info info : attr.classes) {
 65                     if (cf.major_version < 51)
 66                         throw new IllegalStateException();
 67                     if (info.inner_name_index == 0 && info.outer_class_info_index != 0)
 68                         throw new IllegalStateException("Invalid outer_class_info_index=" +
 69                                                         info.outer_class_info_index +
 70                                                         "; inner_name_index=" +
 71                                                         info.inner_name_index + ".");
 72                 }
 73             }
 74         }
 75     }
 76 }
 77 
 78 class SyntheticConstructorAccessTag {
 79 
 80     private static class A {
 81         private A(){}
 82     }
 83 
 84     public void test() {
 85         new A();
 86     }
 87 }
 88 
 89 class SyntheticEnumMapping {
 90     private int convert(E e) {
 91         switch (e) {
 92             case A: return 0;
 93             default: return -1;
 94         }
 95     }
 96     enum E { A }
 97 }
 98 
 99 interface SyntheticAssertionsDisabled {
100     public default void test() {
101         assert false;
102     }
103 }