Thu, 10 Oct 2013 14:58:04 +0200
8001931: The new build system whitespace cleanup
Reviewed-by: tbell, simonis, erikj
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013, 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 8013576 |
aoqi@0 | 27 | * @summary Add stat support to LambdaToMethod |
aoqi@0 | 28 | * @library ../lib |
aoqi@0 | 29 | * @build JavacTestingAbstractThreadedTest |
aoqi@0 | 30 | * @run main/othervm TestLambdaToMethodStats |
aoqi@0 | 31 | */ |
aoqi@0 | 32 | |
aoqi@0 | 33 | // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) |
aoqi@0 | 34 | // see JDK-8006746 |
aoqi@0 | 35 | |
aoqi@0 | 36 | import java.net.URI; |
aoqi@0 | 37 | import java.util.Arrays; |
aoqi@0 | 38 | |
aoqi@0 | 39 | import javax.tools.Diagnostic; |
aoqi@0 | 40 | import javax.tools.JavaFileObject; |
aoqi@0 | 41 | import javax.tools.SimpleJavaFileObject; |
aoqi@0 | 42 | |
aoqi@0 | 43 | import com.sun.source.util.JavacTask; |
aoqi@0 | 44 | import com.sun.tools.javac.api.ClientCodeWrapper; |
aoqi@0 | 45 | import com.sun.tools.javac.util.JCDiagnostic; |
aoqi@0 | 46 | |
aoqi@0 | 47 | public class TestLambdaToMethodStats |
aoqi@0 | 48 | extends JavacTestingAbstractThreadedTest |
aoqi@0 | 49 | implements Runnable { |
aoqi@0 | 50 | |
aoqi@0 | 51 | enum ExprKind { |
aoqi@0 | 52 | LAMBDA("()->null"), |
aoqi@0 | 53 | MREF1("this::g"), |
aoqi@0 | 54 | MREF2("this::h"); |
aoqi@0 | 55 | |
aoqi@0 | 56 | String exprStr; |
aoqi@0 | 57 | |
aoqi@0 | 58 | ExprKind(String exprStr) { |
aoqi@0 | 59 | this.exprStr = exprStr; |
aoqi@0 | 60 | } |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | enum TargetKind { |
aoqi@0 | 64 | IMPLICIT(""), |
aoqi@0 | 65 | SERIALIZABLE("(A & java.io.Serializable)"); |
aoqi@0 | 66 | |
aoqi@0 | 67 | String targetStr; |
aoqi@0 | 68 | |
aoqi@0 | 69 | TargetKind(String targetStr) { |
aoqi@0 | 70 | this.targetStr = targetStr; |
aoqi@0 | 71 | } |
aoqi@0 | 72 | } |
aoqi@0 | 73 | |
aoqi@0 | 74 | public static void main(String... args) throws Exception { |
aoqi@0 | 75 | for (ExprKind ek : ExprKind.values()) { |
aoqi@0 | 76 | for (TargetKind tk : TargetKind.values()) { |
aoqi@0 | 77 | pool.execute(new TestLambdaToMethodStats(ek, tk)); |
aoqi@0 | 78 | } |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | checkAfterExec(true); |
aoqi@0 | 82 | } |
aoqi@0 | 83 | |
aoqi@0 | 84 | ExprKind ek; |
aoqi@0 | 85 | TargetKind tk; |
aoqi@0 | 86 | JavaSource source; |
aoqi@0 | 87 | DiagnosticChecker diagChecker; |
aoqi@0 | 88 | |
aoqi@0 | 89 | |
aoqi@0 | 90 | TestLambdaToMethodStats(ExprKind ek, TargetKind tk) { |
aoqi@0 | 91 | this.ek = ek; |
aoqi@0 | 92 | this.tk = tk; |
aoqi@0 | 93 | this.source = new JavaSource(); |
aoqi@0 | 94 | this.diagChecker = new DiagnosticChecker(); |
aoqi@0 | 95 | } |
aoqi@0 | 96 | |
aoqi@0 | 97 | class JavaSource extends SimpleJavaFileObject { |
aoqi@0 | 98 | |
aoqi@0 | 99 | String template = "interface A {\n" + |
aoqi@0 | 100 | " Object o();\n" + |
aoqi@0 | 101 | "}\n" + |
aoqi@0 | 102 | "class Test {\n" + |
aoqi@0 | 103 | " A a = #C#E;\n" + |
aoqi@0 | 104 | " Object g() { return null; }\n" + |
aoqi@0 | 105 | " Object h(Object... o) { return null; }\n" + |
aoqi@0 | 106 | "}"; |
aoqi@0 | 107 | |
aoqi@0 | 108 | String source; |
aoqi@0 | 109 | |
aoqi@0 | 110 | public JavaSource() { |
aoqi@0 | 111 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
aoqi@0 | 112 | source = template.replaceAll("#E", ek.exprStr) |
aoqi@0 | 113 | .replaceAll("#C", tk.targetStr); |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | @Override |
aoqi@0 | 117 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
aoqi@0 | 118 | return source; |
aoqi@0 | 119 | } |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | public void run() { |
aoqi@0 | 123 | JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker, |
aoqi@0 | 124 | Arrays.asList("-XDdumpLambdaToMethodStats"), |
aoqi@0 | 125 | null, Arrays.asList(source)); |
aoqi@0 | 126 | try { |
aoqi@0 | 127 | ct.generate(); |
aoqi@0 | 128 | } catch (Throwable ex) { |
aoqi@0 | 129 | throw new |
aoqi@0 | 130 | AssertionError("Error thron when analyzing the following source:\n" + |
aoqi@0 | 131 | source.getCharContent(true)); |
aoqi@0 | 132 | } |
aoqi@0 | 133 | check(); |
aoqi@0 | 134 | } |
aoqi@0 | 135 | |
aoqi@0 | 136 | void check() { |
aoqi@0 | 137 | checkCount.incrementAndGet(); |
aoqi@0 | 138 | |
aoqi@0 | 139 | boolean error = diagChecker.lambda != |
aoqi@0 | 140 | (ek == ExprKind.LAMBDA); |
aoqi@0 | 141 | |
aoqi@0 | 142 | error |= diagChecker.bridge != |
aoqi@0 | 143 | (ek == ExprKind.MREF2); |
aoqi@0 | 144 | |
aoqi@0 | 145 | error |= diagChecker.altMetafactory != |
aoqi@0 | 146 | (tk == TargetKind.SERIALIZABLE); |
aoqi@0 | 147 | |
aoqi@0 | 148 | if (error) { |
aoqi@0 | 149 | throw new AssertionError("Bad stat diagnostic found for source\n" + |
aoqi@0 | 150 | "lambda = " + diagChecker.lambda + "\n" + |
aoqi@0 | 151 | "bridge = " + diagChecker.bridge + "\n" + |
aoqi@0 | 152 | "altMF = " + diagChecker.altMetafactory + "\n" + |
aoqi@0 | 153 | source.source); |
aoqi@0 | 154 | } |
aoqi@0 | 155 | } |
aoqi@0 | 156 | |
aoqi@0 | 157 | static class DiagnosticChecker |
aoqi@0 | 158 | implements javax.tools.DiagnosticListener<JavaFileObject> { |
aoqi@0 | 159 | |
aoqi@0 | 160 | boolean altMetafactory; |
aoqi@0 | 161 | boolean bridge; |
aoqi@0 | 162 | boolean lambda; |
aoqi@0 | 163 | |
aoqi@0 | 164 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
aoqi@0 | 165 | try { |
aoqi@0 | 166 | if (diagnostic.getKind() == Diagnostic.Kind.NOTE) { |
aoqi@0 | 167 | switch (diagnostic.getCode()) { |
aoqi@0 | 168 | case "compiler.note.lambda.stat": |
aoqi@0 | 169 | lambda = true; |
aoqi@0 | 170 | break; |
aoqi@0 | 171 | case "compiler.note.mref.stat": |
aoqi@0 | 172 | lambda = false; |
aoqi@0 | 173 | bridge = false; |
aoqi@0 | 174 | break; |
aoqi@0 | 175 | case "compiler.note.mref.stat.1": |
aoqi@0 | 176 | lambda = false; |
aoqi@0 | 177 | bridge = true; |
aoqi@0 | 178 | break; |
aoqi@0 | 179 | default: |
aoqi@0 | 180 | throw new AssertionError("unexpected note: " + diagnostic.getCode()); |
aoqi@0 | 181 | } |
aoqi@0 | 182 | ClientCodeWrapper.DiagnosticSourceUnwrapper dsu = |
aoqi@0 | 183 | (ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic; |
aoqi@0 | 184 | altMetafactory = (Boolean)dsu.d.getArgs()[0]; |
aoqi@0 | 185 | } |
aoqi@0 | 186 | } catch (RuntimeException t) { |
aoqi@0 | 187 | t.printStackTrace(); |
aoqi@0 | 188 | throw t; |
aoqi@0 | 189 | } |
aoqi@0 | 190 | } |
aoqi@0 | 191 | } |
aoqi@0 | 192 | } |