langtools/test/tools/javac/7003595/T7003595.java
author sogoel
Thu, 05 Jun 2014 10:57:10 -0700
changeset 24797 850ebd4d80a7
parent 10627 d8e900b62e02
child 27319 030080f03e4f
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:
10627
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
     1
/*
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
     4
 *
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
     8
 *
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    14
 *
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    18
 *
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    21
 * questions.
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    22
 */
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    23
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    24
/*
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    25
 * @test
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    26
 * @bug 7003595
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    27
 * @summary IncompatibleClassChangeError with unreferenced local class with subclass
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    28
 */
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    29
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    30
import com.sun.source.util.JavacTask;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    31
import com.sun.tools.classfile.Attribute;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    32
import com.sun.tools.classfile.ClassFile;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.classfile.InnerClasses_attribute;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    34
import com.sun.tools.classfile.ConstantPool.*;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    35
import com.sun.tools.javac.api.JavacTool;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    36
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    37
import java.io.File;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    38
import java.net.URI;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    39
import java.util.Arrays;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    40
import java.util.ArrayList;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    41
import javax.tools.JavaCompiler;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    42
import javax.tools.JavaFileObject;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    43
import javax.tools.SimpleJavaFileObject;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    44
import javax.tools.StandardJavaFileManager;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    45
import javax.tools.ToolProvider;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    46
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    47
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    48
public class T7003595 {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    49
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    50
    /** global decls ***/
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    51
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    52
    // Create a single file manager and reuse it for each compile to save time.
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    53
    static StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    54
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    55
    //statistics
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    56
    static int checkCount = 0;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    57
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    58
    enum ClassKind {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    59
        NESTED("static class #N { #B }", "$", true),
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    60
        INNER("class #N { #B }", "$", false),
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    61
        LOCAL_REF("void test() { class #N { #B }; new #N(); }", "$1", false),
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    62
        LOCAL_NOREF("void test() { class #N { #B }; }", "$1", false),
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    63
        ANON("void test() { new Object() { #B }; }", "$1", false),
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    64
        NONE("", "", false);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    65
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    66
        String memberInnerStr;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    67
        String sep;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    68
        boolean staticAllowed;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    69
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    70
        private ClassKind(String memberInnerStr, String sep, boolean staticAllowed) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    71
            this.memberInnerStr = memberInnerStr;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    72
            this.sep = sep;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    73
            this.staticAllowed = staticAllowed;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    74
        }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    75
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    76
        String getSource(String className, String outerName, String nested) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    77
            return memberInnerStr.replaceAll("#O", outerName).
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    78
                    replaceAll("#N", className).replaceAll("#B", nested);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    79
        }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    80
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    81
        static String getClassfileName(String[] names, ClassKind[] outerKinds, int pos) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    82
            System.out.println(" pos = " + pos + " kind = " + outerKinds[pos] + " sep = " + outerKinds[pos].sep);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    83
            String name = outerKinds[pos] != ANON ?
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    84
                    names[pos] : "";
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    85
            if (pos == 0) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    86
                return "Test" + outerKinds[pos].sep + name;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    87
            } else {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    88
                String outerStr = getClassfileName(names, outerKinds, pos - 1);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    89
                return outerStr + outerKinds[pos].sep + name;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    90
            }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    91
        }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    92
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    93
        boolean isAllowed(ClassKind nestedKind) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    94
            return nestedKind != NESTED ||
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    95
                    staticAllowed;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    96
        }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    97
    }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    98
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
    99
    enum LocalInnerClass {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   100
        LOCAL_REF("class L {}; new L();", "Test$1L"),
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   101
        LOCAL_NOREF("class L {};", "Test$1L"),
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   102
        ANON("new Object() {};", "Test$1"),
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   103
        NONE("", "");
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   104
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   105
        String localInnerStr;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   106
        String canonicalInnerStr;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   107
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   108
        private LocalInnerClass(String localInnerStr, String canonicalInnerStr) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   109
            this.localInnerStr = localInnerStr;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   110
            this.canonicalInnerStr = canonicalInnerStr;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   111
        }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   112
    }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   113
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   114
    public static void main(String... args) throws Exception {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   115
        for (ClassKind ck1 : ClassKind.values()) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   116
            String cname1 = "C1";
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   117
            for (ClassKind ck2 : ClassKind.values()) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   118
                if (!ck1.isAllowed(ck2)) continue;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   119
                String cname2 = "C2";
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   120
                for (ClassKind ck3 : ClassKind.values()) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   121
                    if (!ck2.isAllowed(ck3)) continue;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   122
                    String cname3 = "C3";
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   123
                    new T7003595(new ClassKind[] {ck1, ck2, ck3}, new String[] { cname1, cname2, cname3 }).compileAndCheck();
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   124
                }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   125
            }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   126
        }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   127
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   128
        System.out.println("Total checks made: " + checkCount);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   129
    }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   130
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   131
    /** instance decls **/
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   132
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   133
    ClassKind[] cks;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   134
    String[] cnames;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   135
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   136
    T7003595(ClassKind[] cks, String[] cnames) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   137
        this.cks = cks;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   138
        this.cnames = cnames;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   139
    }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   140
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   141
    void compileAndCheck() throws Exception {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   142
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   143
        JavaSource source = new JavaSource();
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   144
        JavacTask ct = (JavacTask)tool.getTask(null, fm, null,
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   145
                null, null, Arrays.asList(source));
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   146
        ct.call();
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   147
        verifyBytecode(source);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   148
    }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   149
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   150
    void verifyBytecode(JavaSource source) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   151
        for (int i = 0; i < 3 ; i ++) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   152
            if (cks[i] == ClassKind.NONE) break;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   153
            checkCount++;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   154
            String filename = cks[i].getClassfileName(cnames, cks, i);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   155
            File compiledTest = new File(filename + ".class");
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   156
            try {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   157
                ClassFile cf = ClassFile.read(compiledTest);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   158
                if (cf == null) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   159
                    throw new Error("Classfile not found: " + filename);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   160
                }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   161
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   162
                InnerClasses_attribute innerClasses = (InnerClasses_attribute)cf.getAttribute(Attribute.InnerClasses);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   163
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   164
                ArrayList<String> foundInnerSig = new ArrayList<>();
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   165
                if (innerClasses != null) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   166
                    for (InnerClasses_attribute.Info info : innerClasses.classes) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   167
                        String foundSig = info.getInnerClassInfo(cf.constant_pool).getName();
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   168
                        foundInnerSig.add(foundSig);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   169
                    }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   170
                }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   171
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   172
                ArrayList<String> expectedInnerSig = new ArrayList<>();
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   173
                //add inner class (if any)
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   174
                if (i < 2 && cks[i + 1] != ClassKind.NONE) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   175
                    expectedInnerSig.add(cks[i + 1].getClassfileName(cnames, cks, i + 1));
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   176
                }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   177
                //add inner classes
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   178
                for (int j = 0 ; j != i + 1 && j < 3; j++) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   179
                    expectedInnerSig.add(cks[j].getClassfileName(cnames, cks, j));
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   180
                }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   181
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   182
                if (expectedInnerSig.size() != foundInnerSig.size()) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   183
                    throw new Error("InnerClasses attribute for " + cnames[i] + " has wrong size\n" +
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   184
                                    "expected " + expectedInnerSig.size() + "\n" +
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   185
                                    "found " + innerClasses.number_of_classes + "\n" +
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   186
                                    source);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   187
                }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   188
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   189
                for (String foundSig : foundInnerSig) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   190
                    if (!expectedInnerSig.contains(foundSig)) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   191
                        throw new Error("InnerClasses attribute for " + cnames[i] + " has unexpected signature: " +
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   192
                                foundSig + "\n" + source + "\n" + expectedInnerSig);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   193
                    }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   194
                }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   195
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   196
                for (String expectedSig : expectedInnerSig) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   197
                    if (!foundInnerSig.contains(expectedSig)) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   198
                        throw new Error("InnerClasses attribute for " + cnames[i] + " does not contain expected signature: " +
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   199
                                    expectedSig + "\n" + source);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   200
                    }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   201
                }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   202
            } catch (Exception e) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   203
                e.printStackTrace();
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   204
                throw new Error("error reading " + compiledTest +": " + e);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   205
            }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   206
        }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   207
    }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   208
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   209
    class JavaSource extends SimpleJavaFileObject {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   210
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   211
        static final String source_template = "class Test { #C }";
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   212
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   213
        String source;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   214
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   215
        public JavaSource() {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   216
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   217
            String c3 = cks[2].getSource(cnames[2], cnames[1], "");
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   218
            String c2 = cks[1].getSource(cnames[1], cnames[0], c3);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   219
            String c1 = cks[0].getSource(cnames[0], "Test", c2);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   220
            source = source_template.replace("#C", c1);
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   221
        }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   222
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   223
        @Override
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   224
        public String toString() {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   225
            return source;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   226
        }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   227
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   228
        @Override
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   229
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   230
            return source;
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   231
        }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   232
    }
d8e900b62e02 7003595: IncompatibleClassChangeError with unreferenced local class with subclass
mcimadamore
parents:
diff changeset
   233
}