Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6863465/TestCircularClassfile.java
38813 views
/*1* Copyright (c) 2010, 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* @run main TestCircularClassfile28*/2930import java.io.*;31import java.net.URI;32import java.util.Arrays;33import javax.tools.Diagnostic;34import javax.tools.DiagnosticListener;35import javax.tools.JavaCompiler;36import javax.tools.JavaFileObject;37import javax.tools.SimpleJavaFileObject;38import javax.tools.ToolProvider;3940import com.sun.source.util.JavacTask;41import java.util.EnumSet;4243public class TestCircularClassfile {4445enum ClassName {46A("A"),47B("B"),48C("C"),49OBJECT("Object");5051String name;5253ClassName(String name) {54this.name = name;55}56}5758static class JavaSource extends SimpleJavaFileObject {5960final static String sourceStub = "class #C extends #S {}";6162String source;6364public JavaSource(ClassName clazz, ClassName sup) {65super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);66source = sourceStub.replace("#C", clazz.name).replace("#S", sup.name);67}6869@Override70public CharSequence getCharContent(boolean ignoreEncodingErrors) {71return source;72}73}7475public static void main(String... args) throws Exception {76int count = 0;77for (ClassName clazz : EnumSet.of(ClassName.A, ClassName.B, ClassName.C)) {78for (ClassName sup : EnumSet.of(ClassName.A, ClassName.B, ClassName.C)) {79if (sup.ordinal() < clazz.ordinal()) continue;80check("sub_"+count++, clazz, sup);81}82}83}8485static JavaSource[] initialSources = new JavaSource[] {86new JavaSource(ClassName.A, ClassName.OBJECT),87new JavaSource(ClassName.B, ClassName.A),88new JavaSource(ClassName.C, ClassName.B)89};9091static String workDir = System.getProperty("user.dir");9293static void check(String destPath, ClassName clazz, ClassName sup) throws Exception {94File destDir = new File(workDir, destPath); destDir.mkdir();95final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();96JavacTask ct = (JavacTask)tool.getTask(null, null, null,97Arrays.asList("-d", destPath), null, Arrays.asList(initialSources));98ct.generate();99File fileToRemove = new File(destPath, clazz.name + ".class");100fileToRemove.delete();101JavaSource newSource = new JavaSource(clazz, sup);102DiagnosticChecker checker = new DiagnosticChecker();103ct = (JavacTask)tool.getTask(null, null, checker,104Arrays.asList("-cp", destPath), null, Arrays.asList(newSource));105ct.analyze();106if (!checker.errorFound) {107throw new AssertionError(newSource.source);108}109}110111static class DiagnosticChecker implements DiagnosticListener<JavaFileObject> {112113boolean errorFound = false;114115public void report(Diagnostic<? extends JavaFileObject> diagnostic) {116if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&117diagnostic.getCode().equals("compiler.err.cyclic.inheritance")) {118errorFound = true;119}120}121}122}123124125