Path: blob/master/test/langtools/tools/javac/6863465/TestCircularClassfile.java
41149 views
/*1* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 686346526* @summary javac doesn't detect circular subclass dependencies via qualified names27* @modules jdk.compiler28* @run main TestCircularClassfile29*/3031import java.io.*;32import java.net.URI;33import java.util.Arrays;34import javax.tools.Diagnostic;35import javax.tools.DiagnosticListener;36import javax.tools.JavaCompiler;37import javax.tools.JavaFileObject;38import javax.tools.SimpleJavaFileObject;39import javax.tools.ToolProvider;4041import com.sun.source.util.JavacTask;42import java.util.EnumSet;4344public class TestCircularClassfile {4546enum ClassName {47A("A"),48B("B"),49C("C"),50OBJECT("Object");5152String name;5354ClassName(String name) {55this.name = name;56}57}5859static class JavaSource extends SimpleJavaFileObject {6061final static String sourceStub = "class #C extends #S {}";6263String source;6465public JavaSource(ClassName clazz, ClassName sup) {66super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);67source = sourceStub.replace("#C", clazz.name).replace("#S", sup.name);68}6970@Override71public CharSequence getCharContent(boolean ignoreEncodingErrors) {72return source;73}74}7576public static void main(String... args) throws Exception {77int count = 0;78for (ClassName clazz : EnumSet.of(ClassName.A, ClassName.B, ClassName.C)) {79for (ClassName sup : EnumSet.of(ClassName.A, ClassName.B, ClassName.C)) {80if (sup.ordinal() < clazz.ordinal()) continue;81check("sub_"+count++, clazz, sup);82}83}84}8586static JavaSource[] initialSources = new JavaSource[] {87new JavaSource(ClassName.A, ClassName.OBJECT),88new JavaSource(ClassName.B, ClassName.A),89new JavaSource(ClassName.C, ClassName.B)90};9192static String workDir = System.getProperty("user.dir");9394static void check(String destPath, ClassName clazz, ClassName sup) throws Exception {95File destDir = new File(workDir, destPath); destDir.mkdir();96final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();97JavacTask ct = (JavacTask)tool.getTask(null, null, null,98Arrays.asList("-d", destPath), null, Arrays.asList(initialSources));99ct.generate();100File fileToRemove = new File(destPath, clazz.name + ".class");101fileToRemove.delete();102JavaSource newSource = new JavaSource(clazz, sup);103DiagnosticChecker checker = new DiagnosticChecker();104ct = (JavacTask)tool.getTask(null, null, checker,105Arrays.asList("-cp", destPath), null, Arrays.asList(newSource));106ct.analyze();107if (!checker.errorFound) {108throw new AssertionError(newSource.source);109}110}111112static class DiagnosticChecker implements DiagnosticListener<JavaFileObject> {113114boolean errorFound = false;115116public void report(Diagnostic<? extends JavaFileObject> diagnostic) {117if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&118diagnostic.getCode().equals("compiler.err.cyclic.inheritance")) {119errorFound = true;120}121}122}123}124125126