mcimadamore@1145: /* vromero@1482: * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. mcimadamore@1145: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@1145: * mcimadamore@1145: * This code is free software; you can redistribute it and/or modify it mcimadamore@1145: * under the terms of the GNU General Public License version 2 only, as mcimadamore@1145: * published by the Free Software Foundation. mcimadamore@1145: * mcimadamore@1145: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@1145: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@1145: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@1145: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@1145: * accompanied this code). mcimadamore@1145: * mcimadamore@1145: * You should have received a copy of the GNU General Public License version mcimadamore@1145: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@1145: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@1145: * mcimadamore@1145: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@1145: * or visit www.oracle.com if you need additional information or have any mcimadamore@1145: * questions. mcimadamore@1145: */ mcimadamore@1145: mcimadamore@1145: /* mcimadamore@1145: * @test mcimadamore@1145: * @bug 7115052 vromero@1520: * @bug 8003280 8006694 mcimadamore@1415: * @summary Add lambda tests mcimadamore@1415: * Add parser support for method references vromero@1520: * temporarily workaround combo tests are causing time out in several platforms vromero@1482: * @library ../lib vromero@1482: * @build JavacTestingAbstractThreadedTest vromero@1520: * @run main/othervm MethodReferenceParserTest mcimadamore@1145: */ mcimadamore@1145: vromero@1520: // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) vromero@1520: // see JDK-8006746 vromero@1520: mcimadamore@1145: import java.net.URI; mcimadamore@1145: import java.util.Arrays; mcimadamore@1145: import javax.tools.Diagnostic; mcimadamore@1145: import javax.tools.JavaFileObject; mcimadamore@1145: import javax.tools.SimpleJavaFileObject; vromero@1482: import com.sun.source.util.JavacTask; mcimadamore@1145: vromero@1482: public class MethodReferenceParserTest vromero@1482: extends JavacTestingAbstractThreadedTest vromero@1482: implements Runnable { mcimadamore@1145: mcimadamore@1145: enum ReferenceKind { mcimadamore@1352: METHOD_REF("#Q::#Gm"), mcimadamore@1352: CONSTRUCTOR_REF("#Q::#Gnew"), mcimadamore@1165: FALSE_REF("min < max"), mcimadamore@1352: ERR_SUPER("#Q::#Gsuper"), mcimadamore@1352: ERR_METH0("#Q::#Gm()"), mcimadamore@1352: ERR_METH1("#Q::#Gm(X)"), mcimadamore@1352: ERR_CONSTR0("#Q::#Gnew()"), mcimadamore@1352: ERR_CONSTR1("#Q::#Gnew(X)"); mcimadamore@1145: mcimadamore@1145: String referenceTemplate; mcimadamore@1145: mcimadamore@1145: ReferenceKind(String referenceTemplate) { mcimadamore@1145: this.referenceTemplate = referenceTemplate; mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: String getReferenceString(QualifierKind qk, GenericKind gk) { mcimadamore@1145: return referenceTemplate mcimadamore@1145: .replaceAll("#Q", qk.qualifier) mcimadamore@1145: .replaceAll("#G", gk.typeParameters); mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: boolean erroneous() { mcimadamore@1145: switch (this) { mcimadamore@1145: case ERR_SUPER: mcimadamore@1145: case ERR_METH0: mcimadamore@1145: case ERR_METH1: mcimadamore@1145: case ERR_CONSTR0: mcimadamore@1145: case ERR_CONSTR1: mcimadamore@1145: return true; mcimadamore@1145: default: return false; mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: mcimadamore@1165: enum ContextKind { mcimadamore@1165: ASSIGN("SAM s = #E;"), mcimadamore@1165: METHOD("m(#E, i);"); mcimadamore@1165: mcimadamore@1165: String contextTemplate; mcimadamore@1165: mcimadamore@1165: ContextKind(String contextTemplate) { mcimadamore@1165: this.contextTemplate = contextTemplate; mcimadamore@1165: } mcimadamore@1165: vromero@1482: String contextString(ExprKind ek, ReferenceKind rk, QualifierKind qk, vromero@1482: GenericKind gk, SubExprKind sk) { mcimadamore@1165: return contextTemplate.replaceAll("#E", ek.expressionString(rk, qk, gk, sk)); mcimadamore@1165: } mcimadamore@1165: } mcimadamore@1165: mcimadamore@1145: enum GenericKind { mcimadamore@1145: NONE(""), mcimadamore@1145: ONE(""), mcimadamore@1145: TWO(""); mcimadamore@1145: mcimadamore@1145: String typeParameters; mcimadamore@1145: mcimadamore@1145: GenericKind(String typeParameters) { mcimadamore@1145: this.typeParameters = typeParameters; mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: enum QualifierKind { mcimadamore@1145: THIS("this"), mcimadamore@1145: SUPER("super"), mcimadamore@1145: NEW("new Foo()"), mcimadamore@1145: METHOD("m()"), mcimadamore@1145: FIELD("a.f"), mcimadamore@1145: UBOUND_SIMPLE("A"), mcimadamore@1352: UNBOUND_ARRAY1("int[]"), mcimadamore@1352: UNBOUND_ARRAY2("A[][]"), mcimadamore@1145: UNBOUND_GENERIC1("A"), mcimadamore@1145: UNBOUND_GENERIC2("A"), mcimadamore@1165: UNBOUND_GENERIC3("A"), mcimadamore@1165: UNBOUND_GENERIC4("A"), mcimadamore@1165: NESTED_GENERIC1("A, A>"), mcimadamore@1165: NESTED_GENERIC2("A,A>, A,A>>"); mcimadamore@1145: mcimadamore@1145: String qualifier; mcimadamore@1145: mcimadamore@1145: QualifierKind(String qualifier) { mcimadamore@1145: this.qualifier = qualifier; mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: enum ExprKind { mcimadamore@1352: NONE("#R::S"), mcimadamore@1145: SINGLE_PAREN1("(#R#S)"), mcimadamore@1145: SINGLE_PAREN2("(#R)#S"), mcimadamore@1145: DOUBLE_PAREN1("((#R#S))"), mcimadamore@1145: DOUBLE_PAREN2("((#R)#S)"), mcimadamore@1145: DOUBLE_PAREN3("((#R))#S"); mcimadamore@1145: mcimadamore@1145: String expressionTemplate; mcimadamore@1145: mcimadamore@1145: ExprKind(String expressionTemplate) { mcimadamore@1145: this.expressionTemplate = expressionTemplate; mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) { mcimadamore@1145: return expressionTemplate mcimadamore@1145: .replaceAll("#R", rk.getReferenceString(qk, gk)) mcimadamore@1145: .replaceAll("#S", sk.subExpression); mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: enum SubExprKind { mcimadamore@1145: NONE(""), mcimadamore@1145: SELECT_FIELD(".f"), mcimadamore@1145: SELECT_METHOD(".f()"), mcimadamore@1145: SELECT_NEW(".new Foo()"), mcimadamore@1145: POSTINC("++"), mcimadamore@1145: POSTDEC("--"); mcimadamore@1145: mcimadamore@1145: String subExpression; mcimadamore@1145: mcimadamore@1145: SubExprKind(String subExpression) { mcimadamore@1145: this.subExpression = subExpression; mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: public static void main(String... args) throws Exception { mcimadamore@1145: for (ReferenceKind rk : ReferenceKind.values()) { mcimadamore@1145: for (QualifierKind qk : QualifierKind.values()) { mcimadamore@1145: for (GenericKind gk : GenericKind.values()) { mcimadamore@1145: for (SubExprKind sk : SubExprKind.values()) { mcimadamore@1145: for (ExprKind ek : ExprKind.values()) { mcimadamore@1165: for (ContextKind ck : ContextKind.values()) { vromero@1482: pool.execute(new MethodReferenceParserTest(rk, qk, gk, sk, ek, ck)); mcimadamore@1165: } mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: } vromero@1482: vromero@1482: checkAfterExec(); mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: ReferenceKind rk; mcimadamore@1145: QualifierKind qk; mcimadamore@1145: GenericKind gk; mcimadamore@1145: SubExprKind sk; mcimadamore@1145: ExprKind ek; mcimadamore@1165: ContextKind ck; mcimadamore@1145: JavaSource source; mcimadamore@1145: DiagnosticChecker diagChecker; mcimadamore@1145: mcimadamore@1165: MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek, ContextKind ck) { mcimadamore@1145: this.rk = rk; mcimadamore@1145: this.qk = qk; mcimadamore@1145: this.gk = gk; mcimadamore@1145: this.sk = sk; mcimadamore@1145: this.ek = ek; mcimadamore@1165: this.ck = ck; mcimadamore@1145: this.source = new JavaSource(); mcimadamore@1145: this.diagChecker = new DiagnosticChecker(); mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: class JavaSource extends SimpleJavaFileObject { mcimadamore@1145: mcimadamore@1145: String template = "class Test {\n" + mcimadamore@1165: " void test() {\n" + mcimadamore@1165: " #C\n" + mcimadamore@1165: " }" + mcimadamore@1145: "}"; mcimadamore@1145: mcimadamore@1145: String source; mcimadamore@1145: mcimadamore@1145: public JavaSource() { mcimadamore@1145: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); mcimadamore@1165: source = template.replaceAll("#C", ck.contextString(ek, rk, qk, gk, sk)); mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: @Override mcimadamore@1145: public CharSequence getCharContent(boolean ignoreEncodingErrors) { mcimadamore@1145: return source; mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: vromero@1482: @Override vromero@1482: public void run() { vromero@1482: JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker, mcimadamore@1415: null, null, Arrays.asList(source)); mcimadamore@1145: try { mcimadamore@1145: ct.parse(); mcimadamore@1145: } catch (Throwable ex) { vromero@1482: processException(ex); vromero@1482: return; mcimadamore@1145: } mcimadamore@1145: check(); mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: void check() { vromero@1482: checkCount.incrementAndGet(); mcimadamore@1145: mcimadamore@1145: if (diagChecker.errorFound != rk.erroneous()) { mcimadamore@1145: throw new Error("invalid diagnostics for source:\n" + mcimadamore@1145: source.getCharContent(true) + mcimadamore@1145: "\nFound error: " + diagChecker.errorFound + mcimadamore@1145: "\nExpected error: " + rk.erroneous()); mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: mcimadamore@1145: static class DiagnosticChecker implements javax.tools.DiagnosticListener { mcimadamore@1145: mcimadamore@1145: boolean errorFound; mcimadamore@1145: mcimadamore@1145: public void report(Diagnostic diagnostic) { mcimadamore@1145: if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { mcimadamore@1145: errorFound = true; mcimadamore@1145: } mcimadamore@1145: } mcimadamore@1145: } vromero@1482: mcimadamore@1145: }