test/tools/javap/TestSuperclass.java

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 0
959103a6100f
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 7031005
aoqi@0 27 * @summary javap prints "extends java.lang.Object"
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import java.io.File;
aoqi@0 31 import java.io.IOException;
aoqi@0 32 import java.io.PrintWriter;
aoqi@0 33 import java.io.StringWriter;
aoqi@0 34 import java.net.URI;
aoqi@0 35 import java.util.Arrays;
aoqi@0 36 import javax.tools.JavaCompiler;
aoqi@0 37 import javax.tools.JavaCompiler.CompilationTask;
aoqi@0 38 import javax.tools.JavaFileObject;
aoqi@0 39 import javax.tools.SimpleJavaFileObject;
aoqi@0 40 import javax.tools.StandardJavaFileManager;
aoqi@0 41 import javax.tools.StandardLocation;
aoqi@0 42 import javax.tools.ToolProvider;
aoqi@0 43
aoqi@0 44 public class TestSuperclass {
aoqi@0 45 enum ClassKind {
aoqi@0 46 CLASS("class"),
aoqi@0 47 INTERFACE("interface");
aoqi@0 48 ClassKind(String keyword) {
aoqi@0 49 this.keyword = keyword;
aoqi@0 50 }
aoqi@0 51 final String keyword;
aoqi@0 52 }
aoqi@0 53
aoqi@0 54 enum GenericKind {
aoqi@0 55 NO(""),
aoqi@0 56 YES("<T>");
aoqi@0 57 GenericKind(String typarams) {
aoqi@0 58 this.typarams = typarams;
aoqi@0 59 }
aoqi@0 60 final String typarams;
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 enum SuperKind {
aoqi@0 64 NONE(null),
aoqi@0 65 SUPER("Super");
aoqi@0 66 SuperKind(String name) {
aoqi@0 67 this.name = name;
aoqi@0 68 }
aoqi@0 69 String extend() {
aoqi@0 70 return (name == null) ? "" : "extends " + name;
aoqi@0 71 }
aoqi@0 72 String decl(ClassKind ck) {
aoqi@0 73 return (name == null) ? "" : ck.keyword + " " + name + " { }";
aoqi@0 74 }
aoqi@0 75 final String name;
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 public static void main(String... args) throws Exception {
aoqi@0 79 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
aoqi@0 80 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
aoqi@0 81 int errors = 0;
aoqi@0 82
aoqi@0 83 for (ClassKind ck: ClassKind.values()) {
aoqi@0 84 for (GenericKind gk: GenericKind.values()) {
aoqi@0 85 for (SuperKind sk: SuperKind.values()) {
aoqi@0 86 errors += new TestSuperclass(ck, gk, sk).run(comp, fm);
aoqi@0 87 }
aoqi@0 88 }
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 if (errors > 0)
aoqi@0 92 throw new Exception(errors + " errors found");
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 final ClassKind ck;
aoqi@0 96 final GenericKind gk;
aoqi@0 97 final SuperKind sk;
aoqi@0 98
aoqi@0 99 TestSuperclass(ClassKind ck, GenericKind gk, SuperKind sk) {
aoqi@0 100 this.ck = ck;
aoqi@0 101 this.gk = gk;
aoqi@0 102 this.sk = sk;
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 int run(JavaCompiler comp, StandardJavaFileManager fm) throws IOException {
aoqi@0 106 System.err.println("test: ck:" + ck + " gk:" + gk + " sk:" + sk);
aoqi@0 107 File testDir = new File(ck + "-" + gk + "-" + sk);
aoqi@0 108 testDir.mkdirs();
aoqi@0 109 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(testDir));
aoqi@0 110
aoqi@0 111 JavaSource js = new JavaSource();
aoqi@0 112 System.err.println(js.getCharContent(false));
aoqi@0 113 CompilationTask t = comp.getTask(null, fm, null, null, null, Arrays.asList(js));
aoqi@0 114 if (!t.call())
aoqi@0 115 throw new Error("compilation failed");
aoqi@0 116
aoqi@0 117 File testClass = new File(testDir, "Test.class");
aoqi@0 118 String out = javap(testClass);
aoqi@0 119
aoqi@0 120 // Extract class sig from first line of Java source
aoqi@0 121 String expect = js.source.replaceAll("(?s)^(.* Test[^{]+?) *\\{.*", "$1");
aoqi@0 122
aoqi@0 123 // Extract class sig from line from javap output
aoqi@0 124 String found = out.replaceAll("(?s).*\n(.* Test[^{]+?) *\\{.*", "$1");
aoqi@0 125
aoqi@0 126 checkEqual("class signature", expect, found);
aoqi@0 127
aoqi@0 128 return errors;
aoqi@0 129 }
aoqi@0 130
aoqi@0 131 String javap(File file) {
aoqi@0 132 StringWriter sw = new StringWriter();
aoqi@0 133 PrintWriter pw = new PrintWriter(sw);
aoqi@0 134 String[] args = { file.getPath() };
aoqi@0 135 int rc = com.sun.tools.javap.Main.run(args, pw);
aoqi@0 136 pw.close();
aoqi@0 137 String out = sw.toString();
aoqi@0 138 if (!out.isEmpty())
aoqi@0 139 System.err.println(out);
aoqi@0 140 if (rc != 0)
aoqi@0 141 throw new Error("javap failed: rc=" + rc);
aoqi@0 142 return out;
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 void checkEqual(String label, String expect, String found) {
aoqi@0 146 if (!expect.equals(found))
aoqi@0 147 error("Unexpected " + label + " found: '" + found + "', expected: '" + expect + "'");
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 void error(String msg) {
aoqi@0 151 System.err.println("Error: " + msg);
aoqi@0 152 errors++;
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 int errors;
aoqi@0 156
aoqi@0 157 class JavaSource extends SimpleJavaFileObject {
aoqi@0 158 static final String template =
aoqi@0 159 "#CK Test#GK #EK { }\n"
aoqi@0 160 + "#SK\n";
aoqi@0 161 final String source;
aoqi@0 162
aoqi@0 163 public JavaSource() {
aoqi@0 164 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
aoqi@0 165 source = template
aoqi@0 166 .replace("#CK", ck.keyword)
aoqi@0 167 .replace("#GK", gk.typarams)
aoqi@0 168 .replace("#EK", sk.extend())
aoqi@0 169 .replace("#SK", sk.decl(ck));
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 @Override
aoqi@0 173 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 174 return source;
aoqi@0 175 }
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 }

mercurial