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.compiler/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.getName().matches(".*\\$[0-9]+")) {
  48                 EnclosingMethod_attribute encl =
  49                         (EnclosingMethod_attribute) cf.getAttribute(Attribute.EnclosingMethod);
  50                 if (encl != null) {
  51                     if (encl.method_index != 0)
  52                         throw new IllegalStateException("Invalid EnclosingMethod.method_index: " +
  53                                                         encl.method_index + ".");
  54                 }
  55             }
  56             InnerClasses_attribute attr =
  57                     (InnerClasses_attribute) cf.getAttribute(Attribute.InnerClasses);
  58             if (attr != null) {
  59                 for (InnerClasses_attribute.Info info : attr.classes) {
  60                     if (cf.major_version < 51)
  61                         throw new IllegalStateException();
  62                     if (info.inner_name_index == 0 && info.outer_class_info_index != 0)
  63                         throw new IllegalStateException("Invalid outer_class_info_index=" +
  64                                                         info.outer_class_info_index +
  65                                                         "; inner_name_index=" +
  66                                                         info.inner_name_index + ".");
  67                 }
  68             }
  69         }
  70     }
  71 }
  72 
  73 class SyntheticConstructorAccessTag {
  74 
  75     private static class A {
  76         private A(){}
  77     }
  78 
  79     public void test() {
  80         new A();
  81     }
  82 }
  83 
  84 class SyntheticEnumMapping {
  85     private int convert(E e) {
  86         switch (e) {
  87             case A: return 0;
  88             default: return -1;
  89         }
  90     }
  91     enum E { A }
  92 }
  93 
  94 interface SyntheticAssertionsDisabled {
  95     public default void test() {
  96         assert false;
  97     }
  98 }