aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 7031005 aoqi@0: * @summary javap prints "extends java.lang.Object" aoqi@0: */ aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.io.PrintWriter; aoqi@0: import java.io.StringWriter; aoqi@0: import java.net.URI; aoqi@0: import java.util.Arrays; aoqi@0: import javax.tools.JavaCompiler; aoqi@0: import javax.tools.JavaCompiler.CompilationTask; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: import javax.tools.StandardLocation; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: public class TestSuperclass { aoqi@0: enum ClassKind { aoqi@0: CLASS("class"), aoqi@0: INTERFACE("interface"); aoqi@0: ClassKind(String keyword) { aoqi@0: this.keyword = keyword; aoqi@0: } aoqi@0: final String keyword; aoqi@0: } aoqi@0: aoqi@0: enum GenericKind { aoqi@0: NO(""), aoqi@0: YES(""); aoqi@0: GenericKind(String typarams) { aoqi@0: this.typarams = typarams; aoqi@0: } aoqi@0: final String typarams; aoqi@0: } aoqi@0: aoqi@0: enum SuperKind { aoqi@0: NONE(null), aoqi@0: SUPER("Super"); aoqi@0: SuperKind(String name) { aoqi@0: this.name = name; aoqi@0: } aoqi@0: String extend() { aoqi@0: return (name == null) ? "" : "extends " + name; aoqi@0: } aoqi@0: String decl(ClassKind ck) { aoqi@0: return (name == null) ? "" : ck.keyword + " " + name + " { }"; aoqi@0: } aoqi@0: final String name; aoqi@0: } aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); aoqi@0: StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); aoqi@0: int errors = 0; aoqi@0: aoqi@0: for (ClassKind ck: ClassKind.values()) { aoqi@0: for (GenericKind gk: GenericKind.values()) { aoqi@0: for (SuperKind sk: SuperKind.values()) { aoqi@0: errors += new TestSuperclass(ck, gk, sk).run(comp, fm); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors found"); aoqi@0: } aoqi@0: aoqi@0: final ClassKind ck; aoqi@0: final GenericKind gk; aoqi@0: final SuperKind sk; aoqi@0: aoqi@0: TestSuperclass(ClassKind ck, GenericKind gk, SuperKind sk) { aoqi@0: this.ck = ck; aoqi@0: this.gk = gk; aoqi@0: this.sk = sk; aoqi@0: } aoqi@0: aoqi@0: int run(JavaCompiler comp, StandardJavaFileManager fm) throws IOException { aoqi@0: System.err.println("test: ck:" + ck + " gk:" + gk + " sk:" + sk); aoqi@0: File testDir = new File(ck + "-" + gk + "-" + sk); aoqi@0: testDir.mkdirs(); aoqi@0: fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(testDir)); aoqi@0: aoqi@0: JavaSource js = new JavaSource(); aoqi@0: System.err.println(js.getCharContent(false)); aoqi@0: CompilationTask t = comp.getTask(null, fm, null, null, null, Arrays.asList(js)); aoqi@0: if (!t.call()) aoqi@0: throw new Error("compilation failed"); aoqi@0: aoqi@0: File testClass = new File(testDir, "Test.class"); aoqi@0: String out = javap(testClass); aoqi@0: aoqi@0: // Extract class sig from first line of Java source aoqi@0: String expect = js.source.replaceAll("(?s)^(.* Test[^{]+?) *\\{.*", "$1"); aoqi@0: aoqi@0: // Extract class sig from line from javap output aoqi@0: String found = out.replaceAll("(?s).*\n(.* Test[^{]+?) *\\{.*", "$1"); aoqi@0: aoqi@0: checkEqual("class signature", expect, found); aoqi@0: aoqi@0: return errors; aoqi@0: } aoqi@0: aoqi@0: String javap(File file) { aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter pw = new PrintWriter(sw); aoqi@0: String[] args = { file.getPath() }; aoqi@0: int rc = com.sun.tools.javap.Main.run(args, pw); aoqi@0: pw.close(); aoqi@0: String out = sw.toString(); aoqi@0: if (!out.isEmpty()) aoqi@0: System.err.println(out); aoqi@0: if (rc != 0) aoqi@0: throw new Error("javap failed: rc=" + rc); aoqi@0: return out; aoqi@0: } aoqi@0: aoqi@0: void checkEqual(String label, String expect, String found) { aoqi@0: if (!expect.equals(found)) aoqi@0: error("Unexpected " + label + " found: '" + found + "', expected: '" + expect + "'"); aoqi@0: } aoqi@0: aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: int errors; aoqi@0: aoqi@0: class JavaSource extends SimpleJavaFileObject { aoqi@0: static final String template = aoqi@0: "#CK Test#GK #EK { }\n" aoqi@0: + "#SK\n"; aoqi@0: final String source; aoqi@0: aoqi@0: public JavaSource() { aoqi@0: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); aoqi@0: source = template aoqi@0: .replace("#CK", ck.keyword) aoqi@0: .replace("#GK", gk.typarams) aoqi@0: .replace("#EK", sk.extend()) aoqi@0: .replace("#SK", sk.decl(ck)); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) { aoqi@0: return source; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: }