Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javap/TestSuperclass.java
32285 views
/*1* Copyright (c) 2011, 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 703100526* @summary javap prints "extends java.lang.Object"27*/2829import java.io.File;30import java.io.IOException;31import java.io.PrintWriter;32import java.io.StringWriter;33import java.net.URI;34import java.util.Arrays;35import javax.tools.JavaCompiler;36import javax.tools.JavaCompiler.CompilationTask;37import javax.tools.JavaFileObject;38import javax.tools.SimpleJavaFileObject;39import javax.tools.StandardJavaFileManager;40import javax.tools.StandardLocation;41import javax.tools.ToolProvider;4243public class TestSuperclass {44enum ClassKind {45CLASS("class"),46INTERFACE("interface");47ClassKind(String keyword) {48this.keyword = keyword;49}50final String keyword;51}5253enum GenericKind {54NO(""),55YES("<T>");56GenericKind(String typarams) {57this.typarams = typarams;58}59final String typarams;60}6162enum SuperKind {63NONE(null),64SUPER("Super");65SuperKind(String name) {66this.name = name;67}68String extend() {69return (name == null) ? "" : "extends " + name;70}71String decl(ClassKind ck) {72return (name == null) ? "" : ck.keyword + " " + name + " { }";73}74final String name;75}7677public static void main(String... args) throws Exception {78JavaCompiler comp = ToolProvider.getSystemJavaCompiler();79StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);80int errors = 0;8182for (ClassKind ck: ClassKind.values()) {83for (GenericKind gk: GenericKind.values()) {84for (SuperKind sk: SuperKind.values()) {85errors += new TestSuperclass(ck, gk, sk).run(comp, fm);86}87}88}8990if (errors > 0)91throw new Exception(errors + " errors found");92}9394final ClassKind ck;95final GenericKind gk;96final SuperKind sk;9798TestSuperclass(ClassKind ck, GenericKind gk, SuperKind sk) {99this.ck = ck;100this.gk = gk;101this.sk = sk;102}103104int run(JavaCompiler comp, StandardJavaFileManager fm) throws IOException {105System.err.println("test: ck:" + ck + " gk:" + gk + " sk:" + sk);106File testDir = new File(ck + "-" + gk + "-" + sk);107testDir.mkdirs();108fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(testDir));109110JavaSource js = new JavaSource();111System.err.println(js.getCharContent(false));112CompilationTask t = comp.getTask(null, fm, null, null, null, Arrays.asList(js));113if (!t.call())114throw new Error("compilation failed");115116File testClass = new File(testDir, "Test.class");117String out = javap(testClass);118119// Extract class sig from first line of Java source120String expect = js.source.replaceAll("(?s)^(.* Test[^{]+?) *\\{.*", "$1");121122// Extract class sig from line from javap output123String found = out.replaceAll("(?s).*\n(.* Test[^{]+?) *\\{.*", "$1");124125checkEqual("class signature", expect, found);126127return errors;128}129130String javap(File file) {131StringWriter sw = new StringWriter();132PrintWriter pw = new PrintWriter(sw);133String[] args = { file.getPath() };134int rc = com.sun.tools.javap.Main.run(args, pw);135pw.close();136String out = sw.toString();137if (!out.isEmpty())138System.err.println(out);139if (rc != 0)140throw new Error("javap failed: rc=" + rc);141return out;142}143144void checkEqual(String label, String expect, String found) {145if (!expect.equals(found))146error("Unexpected " + label + " found: '" + found + "', expected: '" + expect + "'");147}148149void error(String msg) {150System.err.println("Error: " + msg);151errors++;152}153154int errors;155156class JavaSource extends SimpleJavaFileObject {157static final String template =158"#CK Test#GK #EK { }\n"159+ "#SK\n";160final String source;161162public JavaSource() {163super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);164source = template165.replace("#CK", ck.keyword)166.replace("#GK", gk.typarams)167.replace("#EK", sk.extend())168.replace("#SK", sk.decl(ck));169}170171@Override172public CharSequence getCharContent(boolean ignoreEncodingErrors) {173return source;174}175}176177}178179180