test/tools/javac/lambda/MethodReferenceParserTest.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, 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 7115052 8003280 8006694
aoqi@0 27 * @summary Add lambda tests
aoqi@0 28 * Add parser support for method references
aoqi@0 29 * temporarily workaround combo tests are causing time out in several platforms
aoqi@0 30 * @library ../lib
aoqi@0 31 * @build JavacTestingAbstractThreadedTest
aoqi@0 32 * @run main/othervm MethodReferenceParserTest
aoqi@0 33 */
aoqi@0 34
aoqi@0 35 // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
aoqi@0 36 // see JDK-8006746
aoqi@0 37
aoqi@0 38 import java.net.URI;
aoqi@0 39 import java.util.Arrays;
aoqi@0 40 import javax.tools.Diagnostic;
aoqi@0 41 import javax.tools.JavaFileObject;
aoqi@0 42 import javax.tools.SimpleJavaFileObject;
aoqi@0 43 import com.sun.source.util.JavacTask;
aoqi@0 44
aoqi@0 45 public class MethodReferenceParserTest
aoqi@0 46 extends JavacTestingAbstractThreadedTest
aoqi@0 47 implements Runnable {
aoqi@0 48
aoqi@0 49 enum ReferenceKind {
aoqi@0 50 METHOD_REF("#Q::#Gm"),
aoqi@0 51 CONSTRUCTOR_REF("#Q::#Gnew"),
aoqi@0 52 FALSE_REF("min < max"),
aoqi@0 53 ERR_SUPER("#Q::#Gsuper"),
aoqi@0 54 ERR_METH0("#Q::#Gm()"),
aoqi@0 55 ERR_METH1("#Q::#Gm(X)"),
aoqi@0 56 ERR_CONSTR0("#Q::#Gnew()"),
aoqi@0 57 ERR_CONSTR1("#Q::#Gnew(X)");
aoqi@0 58
aoqi@0 59 String referenceTemplate;
aoqi@0 60
aoqi@0 61 ReferenceKind(String referenceTemplate) {
aoqi@0 62 this.referenceTemplate = referenceTemplate;
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 String getReferenceString(QualifierKind qk, GenericKind gk) {
aoqi@0 66 return referenceTemplate
aoqi@0 67 .replaceAll("#Q", qk.qualifier)
aoqi@0 68 .replaceAll("#G", gk.typeParameters);
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 boolean erroneous() {
aoqi@0 72 switch (this) {
aoqi@0 73 case ERR_SUPER:
aoqi@0 74 case ERR_METH0:
aoqi@0 75 case ERR_METH1:
aoqi@0 76 case ERR_CONSTR0:
aoqi@0 77 case ERR_CONSTR1:
aoqi@0 78 return true;
aoqi@0 79 default: return false;
aoqi@0 80 }
aoqi@0 81 }
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 enum ContextKind {
aoqi@0 85 ASSIGN("SAM s = #E;"),
aoqi@0 86 METHOD("m(#E, i);");
aoqi@0 87
aoqi@0 88 String contextTemplate;
aoqi@0 89
aoqi@0 90 ContextKind(String contextTemplate) {
aoqi@0 91 this.contextTemplate = contextTemplate;
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 String contextString(ExprKind ek, ReferenceKind rk, QualifierKind qk,
aoqi@0 95 GenericKind gk, SubExprKind sk) {
aoqi@0 96 return contextTemplate.replaceAll("#E", ek.expressionString(rk, qk, gk, sk));
aoqi@0 97 }
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 enum GenericKind {
aoqi@0 101 NONE(""),
aoqi@0 102 ONE("<X>"),
aoqi@0 103 TWO("<X,Y>");
aoqi@0 104
aoqi@0 105 String typeParameters;
aoqi@0 106
aoqi@0 107 GenericKind(String typeParameters) {
aoqi@0 108 this.typeParameters = typeParameters;
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 enum QualifierKind {
aoqi@0 113 THIS("this"),
aoqi@0 114 SUPER("super"),
aoqi@0 115 NEW("new Foo()"),
aoqi@0 116 METHOD("m()"),
aoqi@0 117 FIELD("a.f"),
aoqi@0 118 UBOUND_SIMPLE("A"),
aoqi@0 119 UNBOUND_ARRAY1("int[]"),
aoqi@0 120 UNBOUND_ARRAY2("A<G>[][]"),
aoqi@0 121 UNBOUND_GENERIC1("A<X>"),
aoqi@0 122 UNBOUND_GENERIC2("A<X, Y>"),
aoqi@0 123 UNBOUND_GENERIC3("A<? extends X, ? super Y>"),
aoqi@0 124 UNBOUND_GENERIC4("A<int[], short[][]>"),
aoqi@0 125 NESTED_GENERIC1("A<A<X,Y>, A<X,Y>>"),
aoqi@0 126 NESTED_GENERIC2("A<A<A<X,Y>,A<X,Y>>, A<A<X,Y>,A<X,Y>>>");
aoqi@0 127
aoqi@0 128 String qualifier;
aoqi@0 129
aoqi@0 130 QualifierKind(String qualifier) {
aoqi@0 131 this.qualifier = qualifier;
aoqi@0 132 }
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 enum ExprKind {
aoqi@0 136 NONE("#R::S"),
aoqi@0 137 SINGLE_PAREN1("(#R#S)"),
aoqi@0 138 SINGLE_PAREN2("(#R)#S"),
aoqi@0 139 DOUBLE_PAREN1("((#R#S))"),
aoqi@0 140 DOUBLE_PAREN2("((#R)#S)"),
aoqi@0 141 DOUBLE_PAREN3("((#R))#S");
aoqi@0 142
aoqi@0 143 String expressionTemplate;
aoqi@0 144
aoqi@0 145 ExprKind(String expressionTemplate) {
aoqi@0 146 this.expressionTemplate = expressionTemplate;
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
aoqi@0 150 return expressionTemplate
aoqi@0 151 .replaceAll("#R", rk.getReferenceString(qk, gk))
aoqi@0 152 .replaceAll("#S", sk.subExpression);
aoqi@0 153 }
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 enum SubExprKind {
aoqi@0 157 NONE(""),
aoqi@0 158 SELECT_FIELD(".f"),
aoqi@0 159 SELECT_METHOD(".f()"),
aoqi@0 160 SELECT_NEW(".new Foo()"),
aoqi@0 161 POSTINC("++"),
aoqi@0 162 POSTDEC("--");
aoqi@0 163
aoqi@0 164 String subExpression;
aoqi@0 165
aoqi@0 166 SubExprKind(String subExpression) {
aoqi@0 167 this.subExpression = subExpression;
aoqi@0 168 }
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 public static void main(String... args) throws Exception {
aoqi@0 172 for (ReferenceKind rk : ReferenceKind.values()) {
aoqi@0 173 for (QualifierKind qk : QualifierKind.values()) {
aoqi@0 174 for (GenericKind gk : GenericKind.values()) {
aoqi@0 175 for (SubExprKind sk : SubExprKind.values()) {
aoqi@0 176 for (ExprKind ek : ExprKind.values()) {
aoqi@0 177 for (ContextKind ck : ContextKind.values()) {
aoqi@0 178 pool.execute(new MethodReferenceParserTest(rk, qk, gk, sk, ek, ck));
aoqi@0 179 }
aoqi@0 180 }
aoqi@0 181 }
aoqi@0 182 }
aoqi@0 183 }
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 checkAfterExec();
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 ReferenceKind rk;
aoqi@0 190 QualifierKind qk;
aoqi@0 191 GenericKind gk;
aoqi@0 192 SubExprKind sk;
aoqi@0 193 ExprKind ek;
aoqi@0 194 ContextKind ck;
aoqi@0 195 JavaSource source;
aoqi@0 196 DiagnosticChecker diagChecker;
aoqi@0 197
aoqi@0 198 MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek, ContextKind ck) {
aoqi@0 199 this.rk = rk;
aoqi@0 200 this.qk = qk;
aoqi@0 201 this.gk = gk;
aoqi@0 202 this.sk = sk;
aoqi@0 203 this.ek = ek;
aoqi@0 204 this.ck = ck;
aoqi@0 205 this.source = new JavaSource();
aoqi@0 206 this.diagChecker = new DiagnosticChecker();
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 class JavaSource extends SimpleJavaFileObject {
aoqi@0 210
aoqi@0 211 String template = "class Test {\n" +
aoqi@0 212 " void test() {\n" +
aoqi@0 213 " #C\n" +
aoqi@0 214 " }" +
aoqi@0 215 "}";
aoqi@0 216
aoqi@0 217 String source;
aoqi@0 218
aoqi@0 219 public JavaSource() {
aoqi@0 220 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
aoqi@0 221 source = template.replaceAll("#C", ck.contextString(ek, rk, qk, gk, sk));
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 @Override
aoqi@0 225 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 226 return source;
aoqi@0 227 }
aoqi@0 228 }
aoqi@0 229
aoqi@0 230 @Override
aoqi@0 231 public void run() {
aoqi@0 232 JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
aoqi@0 233 null, null, Arrays.asList(source));
aoqi@0 234 try {
aoqi@0 235 ct.parse();
aoqi@0 236 } catch (Throwable ex) {
aoqi@0 237 processException(ex);
aoqi@0 238 return;
aoqi@0 239 }
aoqi@0 240 check();
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 void check() {
aoqi@0 244 checkCount.incrementAndGet();
aoqi@0 245
aoqi@0 246 if (diagChecker.errorFound != rk.erroneous()) {
aoqi@0 247 throw new Error("invalid diagnostics for source:\n" +
aoqi@0 248 source.getCharContent(true) +
aoqi@0 249 "\nFound error: " + diagChecker.errorFound +
aoqi@0 250 "\nExpected error: " + rk.erroneous());
aoqi@0 251 }
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
aoqi@0 255
aoqi@0 256 boolean errorFound;
aoqi@0 257
aoqi@0 258 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
aoqi@0 259 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
aoqi@0 260 errorFound = true;
aoqi@0 261 }
aoqi@0 262 }
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 }

mercurial