Sun, 11 Dec 2011 17:48:25 +0000
7120266: javac fails to compile hotspot code
Summary: Parser changes for method references cause bad intercation with method call syntax
Reviewed-by: jjg
mcimadamore@1145 | 1 | /* |
mcimadamore@1145 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1145 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1145 | 4 | * |
mcimadamore@1145 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1145 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1145 | 7 | * published by the Free Software Foundation. |
mcimadamore@1145 | 8 | * |
mcimadamore@1145 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1145 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1145 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1145 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1145 | 13 | * accompanied this code). |
mcimadamore@1145 | 14 | * |
mcimadamore@1145 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1145 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1145 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1145 | 18 | * |
mcimadamore@1145 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1145 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1145 | 21 | * questions. |
mcimadamore@1145 | 22 | */ |
mcimadamore@1145 | 23 | |
mcimadamore@1145 | 24 | /* |
mcimadamore@1145 | 25 | * @test |
mcimadamore@1145 | 26 | * @bug 7115052 |
mcimadamore@1150 | 27 | * @ignore 7120266 |
mcimadamore@1145 | 28 | * @summary Add parser support for method references |
mcimadamore@1145 | 29 | */ |
mcimadamore@1145 | 30 | |
mcimadamore@1145 | 31 | import com.sun.source.util.JavacTask; |
mcimadamore@1145 | 32 | import java.net.URI; |
mcimadamore@1145 | 33 | import java.util.Arrays; |
mcimadamore@1145 | 34 | import javax.tools.Diagnostic; |
mcimadamore@1145 | 35 | import javax.tools.JavaCompiler; |
mcimadamore@1145 | 36 | import javax.tools.JavaFileObject; |
mcimadamore@1145 | 37 | import javax.tools.SimpleJavaFileObject; |
mcimadamore@1145 | 38 | import javax.tools.StandardJavaFileManager; |
mcimadamore@1145 | 39 | import javax.tools.ToolProvider; |
mcimadamore@1145 | 40 | |
mcimadamore@1145 | 41 | public class MethodReferenceParserTest { |
mcimadamore@1145 | 42 | |
mcimadamore@1145 | 43 | static int checkCount = 0; |
mcimadamore@1145 | 44 | |
mcimadamore@1145 | 45 | enum ReferenceKind { |
mcimadamore@1145 | 46 | METHOD_REF("#Q##Gm"), |
mcimadamore@1145 | 47 | CONSTRUCTOR_REF("#Q##Gnew"), |
mcimadamore@1145 | 48 | ERR_SUPER("#Q##Gsuper"), |
mcimadamore@1145 | 49 | ERR_METH0("#Q##Gm()"), |
mcimadamore@1145 | 50 | ERR_METH1("#Q##Gm(X)"), |
mcimadamore@1145 | 51 | ERR_CONSTR0("#Q##Gnew()"), |
mcimadamore@1145 | 52 | ERR_CONSTR1("#Q##Gnew(X)"); |
mcimadamore@1145 | 53 | |
mcimadamore@1145 | 54 | String referenceTemplate; |
mcimadamore@1145 | 55 | |
mcimadamore@1145 | 56 | ReferenceKind(String referenceTemplate) { |
mcimadamore@1145 | 57 | this.referenceTemplate = referenceTemplate; |
mcimadamore@1145 | 58 | } |
mcimadamore@1145 | 59 | |
mcimadamore@1145 | 60 | String getReferenceString(QualifierKind qk, GenericKind gk) { |
mcimadamore@1145 | 61 | return referenceTemplate |
mcimadamore@1145 | 62 | .replaceAll("#Q", qk.qualifier) |
mcimadamore@1145 | 63 | .replaceAll("#G", gk.typeParameters); |
mcimadamore@1145 | 64 | } |
mcimadamore@1145 | 65 | |
mcimadamore@1145 | 66 | boolean erroneous() { |
mcimadamore@1145 | 67 | switch (this) { |
mcimadamore@1145 | 68 | case ERR_SUPER: |
mcimadamore@1145 | 69 | case ERR_METH0: |
mcimadamore@1145 | 70 | case ERR_METH1: |
mcimadamore@1145 | 71 | case ERR_CONSTR0: |
mcimadamore@1145 | 72 | case ERR_CONSTR1: |
mcimadamore@1145 | 73 | return true; |
mcimadamore@1145 | 74 | default: return false; |
mcimadamore@1145 | 75 | } |
mcimadamore@1145 | 76 | } |
mcimadamore@1145 | 77 | } |
mcimadamore@1145 | 78 | |
mcimadamore@1145 | 79 | enum GenericKind { |
mcimadamore@1145 | 80 | NONE(""), |
mcimadamore@1145 | 81 | ONE("<X>"), |
mcimadamore@1145 | 82 | TWO("<X,Y>"); |
mcimadamore@1145 | 83 | |
mcimadamore@1145 | 84 | String typeParameters; |
mcimadamore@1145 | 85 | |
mcimadamore@1145 | 86 | GenericKind(String typeParameters) { |
mcimadamore@1145 | 87 | this.typeParameters = typeParameters; |
mcimadamore@1145 | 88 | } |
mcimadamore@1145 | 89 | } |
mcimadamore@1145 | 90 | |
mcimadamore@1145 | 91 | enum QualifierKind { |
mcimadamore@1145 | 92 | THIS("this"), |
mcimadamore@1145 | 93 | SUPER("super"), |
mcimadamore@1145 | 94 | NEW("new Foo()"), |
mcimadamore@1145 | 95 | METHOD("m()"), |
mcimadamore@1145 | 96 | FIELD("a.f"), |
mcimadamore@1145 | 97 | UBOUND_SIMPLE("A"), |
mcimadamore@1145 | 98 | UNBOUND_GENERIC1("A<X>"), |
mcimadamore@1145 | 99 | UNBOUND_GENERIC2("A<X, Y>"), |
mcimadamore@1145 | 100 | UNBOUND_GENERIC3("A<? extends X, ? super Y>"); |
mcimadamore@1145 | 101 | |
mcimadamore@1145 | 102 | String qualifier; |
mcimadamore@1145 | 103 | |
mcimadamore@1145 | 104 | QualifierKind(String qualifier) { |
mcimadamore@1145 | 105 | this.qualifier = qualifier; |
mcimadamore@1145 | 106 | } |
mcimadamore@1145 | 107 | } |
mcimadamore@1145 | 108 | |
mcimadamore@1145 | 109 | enum ExprKind { |
mcimadamore@1145 | 110 | NONE("#R#S"), |
mcimadamore@1145 | 111 | SINGLE_PAREN1("(#R#S)"), |
mcimadamore@1145 | 112 | SINGLE_PAREN2("(#R)#S"), |
mcimadamore@1145 | 113 | DOUBLE_PAREN1("((#R#S))"), |
mcimadamore@1145 | 114 | DOUBLE_PAREN2("((#R)#S)"), |
mcimadamore@1145 | 115 | DOUBLE_PAREN3("((#R))#S"); |
mcimadamore@1145 | 116 | |
mcimadamore@1145 | 117 | String expressionTemplate; |
mcimadamore@1145 | 118 | |
mcimadamore@1145 | 119 | ExprKind(String expressionTemplate) { |
mcimadamore@1145 | 120 | this.expressionTemplate = expressionTemplate; |
mcimadamore@1145 | 121 | } |
mcimadamore@1145 | 122 | |
mcimadamore@1145 | 123 | String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) { |
mcimadamore@1145 | 124 | return expressionTemplate |
mcimadamore@1145 | 125 | .replaceAll("#R", rk.getReferenceString(qk, gk)) |
mcimadamore@1145 | 126 | .replaceAll("#S", sk.subExpression); |
mcimadamore@1145 | 127 | } |
mcimadamore@1145 | 128 | } |
mcimadamore@1145 | 129 | |
mcimadamore@1145 | 130 | enum SubExprKind { |
mcimadamore@1145 | 131 | NONE(""), |
mcimadamore@1145 | 132 | SELECT_FIELD(".f"), |
mcimadamore@1145 | 133 | SELECT_METHOD(".f()"), |
mcimadamore@1145 | 134 | SELECT_NEW(".new Foo()"), |
mcimadamore@1145 | 135 | POSTINC("++"), |
mcimadamore@1145 | 136 | POSTDEC("--"); |
mcimadamore@1145 | 137 | |
mcimadamore@1145 | 138 | String subExpression; |
mcimadamore@1145 | 139 | |
mcimadamore@1145 | 140 | SubExprKind(String subExpression) { |
mcimadamore@1145 | 141 | this.subExpression = subExpression; |
mcimadamore@1145 | 142 | } |
mcimadamore@1145 | 143 | } |
mcimadamore@1145 | 144 | |
mcimadamore@1145 | 145 | public static void main(String... args) throws Exception { |
mcimadamore@1145 | 146 | |
mcimadamore@1145 | 147 | //create default shared JavaCompiler - reused across multiple compilations |
mcimadamore@1145 | 148 | JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@1145 | 149 | StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
mcimadamore@1145 | 150 | |
mcimadamore@1145 | 151 | for (ReferenceKind rk : ReferenceKind.values()) { |
mcimadamore@1145 | 152 | for (QualifierKind qk : QualifierKind.values()) { |
mcimadamore@1145 | 153 | for (GenericKind gk : GenericKind.values()) { |
mcimadamore@1145 | 154 | for (SubExprKind sk : SubExprKind.values()) { |
mcimadamore@1145 | 155 | for (ExprKind ek : ExprKind.values()) { |
mcimadamore@1145 | 156 | new MethodReferenceParserTest(rk, qk, gk, sk, ek).run(comp, fm); |
mcimadamore@1145 | 157 | } |
mcimadamore@1145 | 158 | } |
mcimadamore@1145 | 159 | } |
mcimadamore@1145 | 160 | } |
mcimadamore@1145 | 161 | } |
mcimadamore@1145 | 162 | System.out.println("Total check executed: " + checkCount); |
mcimadamore@1145 | 163 | } |
mcimadamore@1145 | 164 | |
mcimadamore@1145 | 165 | ReferenceKind rk; |
mcimadamore@1145 | 166 | QualifierKind qk; |
mcimadamore@1145 | 167 | GenericKind gk; |
mcimadamore@1145 | 168 | SubExprKind sk; |
mcimadamore@1145 | 169 | ExprKind ek; |
mcimadamore@1145 | 170 | JavaSource source; |
mcimadamore@1145 | 171 | DiagnosticChecker diagChecker; |
mcimadamore@1145 | 172 | |
mcimadamore@1145 | 173 | MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek) { |
mcimadamore@1145 | 174 | this.rk = rk; |
mcimadamore@1145 | 175 | this.qk = qk; |
mcimadamore@1145 | 176 | this.gk = gk; |
mcimadamore@1145 | 177 | this.sk = sk; |
mcimadamore@1145 | 178 | this.ek = ek; |
mcimadamore@1145 | 179 | this.source = new JavaSource(); |
mcimadamore@1145 | 180 | this.diagChecker = new DiagnosticChecker(); |
mcimadamore@1145 | 181 | } |
mcimadamore@1145 | 182 | |
mcimadamore@1145 | 183 | class JavaSource extends SimpleJavaFileObject { |
mcimadamore@1145 | 184 | |
mcimadamore@1145 | 185 | String template = "class Test {\n" + |
mcimadamore@1145 | 186 | " SAM s = #E;\n" + |
mcimadamore@1145 | 187 | "}"; |
mcimadamore@1145 | 188 | |
mcimadamore@1145 | 189 | String source; |
mcimadamore@1145 | 190 | |
mcimadamore@1145 | 191 | public JavaSource() { |
mcimadamore@1145 | 192 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@1145 | 193 | source = template.replaceAll("#E", ek.expressionString(rk, qk, gk, sk)); |
mcimadamore@1145 | 194 | } |
mcimadamore@1145 | 195 | |
mcimadamore@1145 | 196 | @Override |
mcimadamore@1145 | 197 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@1145 | 198 | return source; |
mcimadamore@1145 | 199 | } |
mcimadamore@1145 | 200 | } |
mcimadamore@1145 | 201 | |
mcimadamore@1145 | 202 | void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { |
mcimadamore@1145 | 203 | JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, |
mcimadamore@1145 | 204 | Arrays.asList("-XDallowMethodReferences"), null, Arrays.asList(source)); |
mcimadamore@1145 | 205 | try { |
mcimadamore@1145 | 206 | ct.parse(); |
mcimadamore@1145 | 207 | } catch (Throwable ex) { |
mcimadamore@1145 | 208 | throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true)); |
mcimadamore@1145 | 209 | } |
mcimadamore@1145 | 210 | check(); |
mcimadamore@1145 | 211 | } |
mcimadamore@1145 | 212 | |
mcimadamore@1145 | 213 | void check() { |
mcimadamore@1145 | 214 | checkCount++; |
mcimadamore@1145 | 215 | |
mcimadamore@1145 | 216 | if (diagChecker.errorFound != rk.erroneous()) { |
mcimadamore@1145 | 217 | throw new Error("invalid diagnostics for source:\n" + |
mcimadamore@1145 | 218 | source.getCharContent(true) + |
mcimadamore@1145 | 219 | "\nFound error: " + diagChecker.errorFound + |
mcimadamore@1145 | 220 | "\nExpected error: " + rk.erroneous()); |
mcimadamore@1145 | 221 | } |
mcimadamore@1145 | 222 | } |
mcimadamore@1145 | 223 | |
mcimadamore@1145 | 224 | static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
mcimadamore@1145 | 225 | |
mcimadamore@1145 | 226 | boolean errorFound; |
mcimadamore@1145 | 227 | |
mcimadamore@1145 | 228 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@1145 | 229 | if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
mcimadamore@1145 | 230 | errorFound = true; |
mcimadamore@1145 | 231 | } |
mcimadamore@1145 | 232 | } |
mcimadamore@1145 | 233 | } |
mcimadamore@1145 | 234 | } |