test/langtools/tools/javac/7199823/InnerClassCannotBeVerified.java
changeset 50735 2f2af62dfac7
parent 47216 71c04702a3d5
equal deleted inserted replaced
50734:0828a0f6676b 50735:2f2af62dfac7
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    27  * @summary javac generates inner class that can't be verified
    27  * @summary javac generates inner class that can't be verified
    28  * @modules jdk.jdeps/com.sun.tools.classfile
    28  * @modules jdk.jdeps/com.sun.tools.classfile
    29  * @run main InnerClassCannotBeVerified
    29  * @run main InnerClassCannotBeVerified
    30  */
    30  */
    31 
    31 
       
    32 import java.nio.file.NoSuchFileException;
    32 import java.util.Arrays;
    33 import java.util.Arrays;
    33 import javax.tools.JavaFileObject;
    34 import javax.tools.JavaFileObject;
    34 import java.net.URI;
    35 import java.net.URI;
    35 import javax.tools.SimpleJavaFileObject;
    36 import javax.tools.SimpleJavaFileObject;
    36 import javax.tools.ToolProvider;
    37 import javax.tools.ToolProvider;
    41 import java.io.File;
    42 import java.io.File;
    42 import java.io.IOException;
    43 import java.io.IOException;
    43 
    44 
    44 public class InnerClassCannotBeVerified {
    45 public class InnerClassCannotBeVerified {
    45 
    46 
       
    47     enum CompilationKind {
       
    48         PRE_NESTMATES("-source", "10", "-target", "10"),
       
    49         POST_NESTMATES();
       
    50 
       
    51         String[] opts;
       
    52 
       
    53         CompilationKind(String... opts) {
       
    54             this.opts = opts;
       
    55         }
       
    56     }
       
    57 
    46     private static final String errorMessage =
    58     private static final String errorMessage =
    47             "Compile error while compiling the following source:\n";
    59             "Compile error while compiling the following source:\n";
    48 
    60 
    49     public static void main(String... args) throws Exception {
    61     public static void main(String... args) throws Exception {
    50         new InnerClassCannotBeVerified().run();
    62         new InnerClassCannotBeVerified().run();
    51     }
    63     }
    52 
    64 
    53     void run() throws Exception {
    65     void run() throws Exception {
    54         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    66         for (CompilationKind ck : CompilationKind.values()) {
    55         JavaSource source = new JavaSource();
    67             File file = new File("Test$1.class");
    56         JavacTask ct = (JavacTask)comp.getTask(null, null, null,
    68             if (file.exists()) {
    57                 null, null, Arrays.asList(source));
    69                 file.delete();
    58         try {
    70             }
    59             if (!ct.call()) {
    71             JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
       
    72             JavaSource source = new JavaSource();
       
    73             JavacTask ct = (JavacTask)comp.getTask(null, null, null,
       
    74                     Arrays.asList(ck.opts), null, Arrays.asList(source));
       
    75             try {
       
    76                 if (!ct.call()) {
       
    77                     throw new AssertionError(errorMessage +
       
    78                             source.getCharContent(true));
       
    79                 }
       
    80             } catch (Throwable ex) {
    60                 throw new AssertionError(errorMessage +
    81                 throw new AssertionError(errorMessage +
    61                         source.getCharContent(true));
    82                         source.getCharContent(true));
    62             }
    83             }
    63         } catch (Throwable ex) {
    84             check(ck);
    64             throw new AssertionError(errorMessage +
       
    65                     source.getCharContent(true));
       
    66         }
    85         }
    67         check();
       
    68     }
    86     }
    69 
    87 
    70     private void check() throws IOException, ConstantPoolException {
    88     private void check(CompilationKind ck) throws IOException, ConstantPoolException {
    71         File file = new File("Test$1.class");
    89         try {
    72         ClassFile classFile = ClassFile.read(file);
    90             File file = new File("Test$1.class");
    73         boolean inheritsFromObject =
    91             ClassFile classFile = ClassFile.read(file);
    74                 classFile.getSuperclassName().equals("java/lang/Object");
    92             if (ck == CompilationKind.POST_NESTMATES) {
    75         boolean implementsNoInterface = classFile.interfaces.length == 0;
    93                 throw new AssertionError("Unexpected constructor tag class!");
    76         boolean noMethods = classFile.methods.length == 0;
    94             }
    77         if (!(inheritsFromObject &&
    95             boolean inheritsFromObject =
    78               implementsNoInterface &&
    96                     classFile.getSuperclassName().equals("java/lang/Object");
    79               noMethods)) {
    97             boolean implementsNoInterface = classFile.interfaces.length == 0;
    80             throw new AssertionError("The inner classes reused as " +
    98             boolean noMethods = classFile.methods.length == 0;
    81                     "access constructor tag for this code must be empty");
    99             if (!(inheritsFromObject &&
       
   100                     implementsNoInterface &&
       
   101                     noMethods)) {
       
   102                 throw new AssertionError("The inner classes reused as " +
       
   103                         "access constructor tag for this code must be empty");
       
   104             }
       
   105         } catch (NoSuchFileException ex) {
       
   106             if (ck == CompilationKind.PRE_NESTMATES) {
       
   107                 throw new AssertionError("Constructor tag class missing!");
       
   108             }
    82         }
   109         }
    83     }
   110     }
    84 
   111 
    85     class JavaSource extends SimpleJavaFileObject {
   112     class JavaSource extends SimpleJavaFileObject {
    86 
   113