langtools/test/tools/javac/7199823/InnerClassCannotBeVerified.java
author sogoel
Thu, 05 Jun 2014 10:57:10 -0700
changeset 24797 850ebd4d80a7
parent 15565 040a54631aad
child 30730 d3ce7619db2c
permissions -rw-r--r--
8044072: Group 2: create .out files for OverrideChecks tests in tools/javac dir Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15565
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
     1
/*
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
     4
 *
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
     7
 * published by the Free Software Foundation.
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
     8
 *
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    13
 * accompanied this code).
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    14
 *
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    18
 *
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    21
 * questions.
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    22
 */
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    23
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    24
/*
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    25
 * @test
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    26
 * @bug 7199823
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    27
 * @summary javac generates inner class that can't be verified
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    28
 * @run main InnerClassCannotBeVerified
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    29
 */
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    30
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    31
import java.util.Arrays;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    32
import javax.tools.JavaFileObject;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    33
import java.net.URI;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    34
import javax.tools.SimpleJavaFileObject;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    35
import javax.tools.ToolProvider;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    36
import javax.tools.JavaCompiler;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    37
import com.sun.source.util.JavacTask;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    38
import com.sun.tools.classfile.ClassFile;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    39
import com.sun.tools.classfile.ConstantPoolException;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    40
import java.io.File;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    41
import java.io.IOException;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    42
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    43
public class InnerClassCannotBeVerified {
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    44
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    45
    private static final String errorMessage =
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    46
            "Compile error while compiling the following source:\n";
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    47
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    48
    public static void main(String... args) throws Exception {
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    49
        new InnerClassCannotBeVerified().run();
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    50
    }
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    51
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    52
    void run() throws Exception {
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    53
        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    54
        JavaSource source = new JavaSource();
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    55
        JavacTask ct = (JavacTask)comp.getTask(null, null, null,
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    56
                null, null, Arrays.asList(source));
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    57
        try {
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    58
            if (!ct.call()) {
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    59
                throw new AssertionError(errorMessage +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    60
                        source.getCharContent(true));
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    61
            }
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    62
        } catch (Throwable ex) {
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    63
            throw new AssertionError(errorMessage +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    64
                    source.getCharContent(true));
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    65
        }
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    66
        check();
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    67
    }
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    68
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    69
    private void check() throws IOException, ConstantPoolException {
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    70
        File file = new File("Test$1.class");
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    71
        ClassFile classFile = ClassFile.read(file);
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    72
        boolean inheritsFromObject =
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    73
                classFile.getSuperclassName().equals("java/lang/Object");
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    74
        boolean implementsNoInterface = classFile.interfaces.length == 0;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    75
        boolean noMethods = classFile.methods.length == 0;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    76
        if (!(inheritsFromObject &&
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    77
              implementsNoInterface &&
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    78
              noMethods)) {
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    79
            throw new AssertionError("The inner classes reused as " +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    80
                    "access constructor tag for this code must be empty");
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    81
        }
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    82
    }
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    83
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    84
    class JavaSource extends SimpleJavaFileObject {
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    85
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    86
        String internalSource =
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    87
                              "public class Test {\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    88
                              "    private static class Foo {}\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    89
                              "    public static void main(String[] args){ \n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    90
                              "        new Foo();\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    91
                              "        if(false) {\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    92
                              "            new Runnable() {\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    93
                              "                @Override\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    94
                              "                public void run() {\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    95
                              "                    System.out.println();\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    96
                              "                }\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    97
                              "            }.run();\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    98
                              "        }\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
    99
                              "   }\n" +
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
   100
                              "}";
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
   101
        public JavaSource() {
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
   102
            super(URI.create("Test.java"), JavaFileObject.Kind.SOURCE);
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
   103
        }
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
   104
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
   105
        @Override
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
   106
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
   107
            return internalSource;
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
   108
        }
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
   109
    }
040a54631aad 7199823: javac generates inner class that can't be verified
vromero
parents:
diff changeset
   110
}