langtools/test/tools/javac/6863465/TestCircularClassfile.java
author sogoel
Thu, 05 Jun 2014 10:57:10 -0700
changeset 24797 850ebd4d80a7
parent 6711 fc9732adc790
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:
6711
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
     1
/*
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
     4
 *
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
     8
 *
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    14
 *
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    18
 *
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    21
 * questions.
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    22
 */
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    23
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    24
/*
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    25
 * @test
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    26
 * @bug 6863465
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    27
 * @summary  javac doesn't detect circular subclass dependencies via qualified names
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    28
 * @run main TestCircularClassfile
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    29
 */
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    30
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    31
import java.io.*;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    32
import java.net.URI;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    33
import java.util.Arrays;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    34
import javax.tools.Diagnostic;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    35
import javax.tools.DiagnosticListener;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    36
import javax.tools.JavaCompiler;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    37
import javax.tools.JavaFileObject;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    38
import javax.tools.SimpleJavaFileObject;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    39
import javax.tools.ToolProvider;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    40
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    41
import com.sun.source.util.JavacTask;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    42
import java.util.EnumSet;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    43
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    44
public class TestCircularClassfile {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    45
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    46
    enum ClassName {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    47
        A("A"),
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    48
        B("B"),
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    49
        C("C"),
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    50
        OBJECT("Object");
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    51
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    52
        String name;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    53
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    54
        ClassName(String name) {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    55
            this.name = name;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    56
        }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    57
    }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    58
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    59
    static class JavaSource extends SimpleJavaFileObject {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    60
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    61
        final static String sourceStub = "class #C extends #S {}";
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    62
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    63
        String source;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    64
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    65
        public JavaSource(ClassName clazz, ClassName sup) {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    66
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    67
            source = sourceStub.replace("#C", clazz.name).replace("#S", sup.name);
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    68
        }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    69
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    70
        @Override
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    71
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    72
            return source;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    73
        }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    74
    }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    75
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    76
    public static void main(String... args) throws Exception {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    77
        int count = 0;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    78
        for (ClassName clazz : EnumSet.of(ClassName.A, ClassName.B, ClassName.C)) {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    79
            for (ClassName sup : EnumSet.of(ClassName.A, ClassName.B, ClassName.C)) {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    80
                if (sup.ordinal() < clazz.ordinal()) continue;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    81
                check("sub_"+count++, clazz, sup);
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    82
            }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    83
        }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    84
    }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    85
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    86
    static JavaSource[] initialSources = new JavaSource[] {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    87
            new JavaSource(ClassName.A, ClassName.OBJECT),
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    88
            new JavaSource(ClassName.B, ClassName.A),
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    89
            new JavaSource(ClassName.C, ClassName.B)
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    90
        };
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    91
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    92
    static String workDir = System.getProperty("user.dir");
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    93
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    94
    static void check(String destPath, ClassName clazz, ClassName sup) throws Exception {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    95
        File destDir = new File(workDir, destPath); destDir.mkdir();
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    96
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    97
        JavacTask ct = (JavacTask)tool.getTask(null, null, null,
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    98
                Arrays.asList("-d", destPath), null, Arrays.asList(initialSources));
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
    99
        ct.generate();
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   100
        File fileToRemove = new File(destPath, clazz.name + ".class");
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   101
        fileToRemove.delete();
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   102
        JavaSource newSource = new JavaSource(clazz, sup);
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   103
        DiagnosticChecker checker = new DiagnosticChecker();
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   104
        ct = (JavacTask)tool.getTask(null, null, checker,
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   105
                Arrays.asList("-cp", destPath), null, Arrays.asList(newSource));
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   106
        ct.analyze();
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   107
        if (!checker.errorFound) {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   108
            throw new AssertionError(newSource.source);
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   109
        }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   110
    }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   111
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   112
    static class DiagnosticChecker implements DiagnosticListener<JavaFileObject> {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   113
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   114
        boolean errorFound = false;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   115
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   116
        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   117
            if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   118
                    diagnostic.getCode().equals("compiler.err.cyclic.inheritance")) {
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   119
                errorFound = true;
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   120
            }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   121
        }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   122
    }
fc9732adc790 6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
diff changeset
   123
}