test/tools/javac/7199823/InnerClassCannotBeVerified.java

Wed, 08 Oct 2014 14:16:40 -0700

author
asaha
date
Wed, 08 Oct 2014 14:16:40 -0700
changeset 2586
f5e5ca7505e2
parent 0
959103a6100f
permissions
-rw-r--r--

Merge

     1 /*
     2  * Copyright (c) 2012, 2013, 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  */
    24 /*
    25  * @test
    26  * @bug 7199823
    27  * @summary javac generates inner class that can't be verified
    28  * @run main InnerClassCannotBeVerified
    29  */
    31 import java.util.Arrays;
    32 import javax.tools.JavaFileObject;
    33 import java.net.URI;
    34 import javax.tools.SimpleJavaFileObject;
    35 import javax.tools.ToolProvider;
    36 import javax.tools.JavaCompiler;
    37 import com.sun.source.util.JavacTask;
    38 import com.sun.tools.classfile.ClassFile;
    39 import com.sun.tools.classfile.ConstantPoolException;
    40 import java.io.File;
    41 import java.io.IOException;
    43 public class InnerClassCannotBeVerified {
    45     private static final String errorMessage =
    46             "Compile error while compiling the following source:\n";
    48     public static void main(String... args) throws Exception {
    49         new InnerClassCannotBeVerified().run();
    50     }
    52     void run() throws Exception {
    53         JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    54         JavaSource source = new JavaSource();
    55         JavacTask ct = (JavacTask)comp.getTask(null, null, null,
    56                 null, null, Arrays.asList(source));
    57         try {
    58             if (!ct.call()) {
    59                 throw new AssertionError(errorMessage +
    60                         source.getCharContent(true));
    61             }
    62         } catch (Throwable ex) {
    63             throw new AssertionError(errorMessage +
    64                     source.getCharContent(true));
    65         }
    66         check();
    67     }
    69     private void check() throws IOException, ConstantPoolException {
    70         File file = new File("Test$1.class");
    71         ClassFile classFile = ClassFile.read(file);
    72         boolean inheritsFromObject =
    73                 classFile.getSuperclassName().equals("java/lang/Object");
    74         boolean implementsNoInterface = classFile.interfaces.length == 0;
    75         boolean noMethods = classFile.methods.length == 0;
    76         if (!(inheritsFromObject &&
    77               implementsNoInterface &&
    78               noMethods)) {
    79             throw new AssertionError("The inner classes reused as " +
    80                     "access constructor tag for this code must be empty");
    81         }
    82     }
    84     class JavaSource extends SimpleJavaFileObject {
    86         String internalSource =
    87                               "public class Test {\n" +
    88                               "    private static class Foo {}\n" +
    89                               "    public static void main(String[] args){ \n" +
    90                               "        new Foo();\n" +
    91                               "        if(false) {\n" +
    92                               "            new Runnable() {\n" +
    93                               "                @Override\n" +
    94                               "                public void run() {\n" +
    95                               "                    System.out.println();\n" +
    96                               "                }\n" +
    97                               "            }.run();\n" +
    98                               "        }\n" +
    99                               "   }\n" +
   100                               "}";
   101         public JavaSource() {
   102             super(URI.create("Test.java"), JavaFileObject.Kind.SOURCE);
   103         }
   105         @Override
   106         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   107             return internalSource;
   108         }
   109     }
   110 }

mercurial