2011-11-28 16:05:46 +00:00
|
|
|
/*
|
2012-10-06 09:35:38 +00:00
|
|
|
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
|
2011-11-28 16:05:46 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
|
|
|
* @bug 7115052
|
2012-11-17 19:01:03 +00:00
|
|
|
* @bug 8003280
|
|
|
|
* @summary Add lambda tests
|
|
|
|
* Add parser support for method references
|
2011-11-28 16:05:46 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import com.sun.source.util.JavacTask;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import javax.tools.Diagnostic;
|
|
|
|
import javax.tools.JavaCompiler;
|
|
|
|
import javax.tools.JavaFileObject;
|
|
|
|
import javax.tools.SimpleJavaFileObject;
|
|
|
|
import javax.tools.StandardJavaFileManager;
|
|
|
|
import javax.tools.ToolProvider;
|
|
|
|
|
|
|
|
public class MethodReferenceParserTest {
|
|
|
|
|
|
|
|
static int checkCount = 0;
|
|
|
|
|
|
|
|
enum ReferenceKind {
|
2012-10-06 09:35:38 +00:00
|
|
|
METHOD_REF("#Q::#Gm"),
|
|
|
|
CONSTRUCTOR_REF("#Q::#Gnew"),
|
2011-12-19 12:07:07 +00:00
|
|
|
FALSE_REF("min < max"),
|
2012-10-06 09:35:38 +00:00
|
|
|
ERR_SUPER("#Q::#Gsuper"),
|
|
|
|
ERR_METH0("#Q::#Gm()"),
|
|
|
|
ERR_METH1("#Q::#Gm(X)"),
|
|
|
|
ERR_CONSTR0("#Q::#Gnew()"),
|
|
|
|
ERR_CONSTR1("#Q::#Gnew(X)");
|
2011-11-28 16:05:46 +00:00
|
|
|
|
|
|
|
String referenceTemplate;
|
|
|
|
|
|
|
|
ReferenceKind(String referenceTemplate) {
|
|
|
|
this.referenceTemplate = referenceTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
String getReferenceString(QualifierKind qk, GenericKind gk) {
|
|
|
|
return referenceTemplate
|
|
|
|
.replaceAll("#Q", qk.qualifier)
|
|
|
|
.replaceAll("#G", gk.typeParameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean erroneous() {
|
|
|
|
switch (this) {
|
|
|
|
case ERR_SUPER:
|
|
|
|
case ERR_METH0:
|
|
|
|
case ERR_METH1:
|
|
|
|
case ERR_CONSTR0:
|
|
|
|
case ERR_CONSTR1:
|
|
|
|
return true;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-19 12:07:07 +00:00
|
|
|
enum ContextKind {
|
|
|
|
ASSIGN("SAM s = #E;"),
|
|
|
|
METHOD("m(#E, i);");
|
|
|
|
|
|
|
|
String contextTemplate;
|
|
|
|
|
|
|
|
ContextKind(String contextTemplate) {
|
|
|
|
this.contextTemplate = contextTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
String contextString(ExprKind ek, ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
|
|
|
|
return contextTemplate.replaceAll("#E", ek.expressionString(rk, qk, gk, sk));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-28 16:05:46 +00:00
|
|
|
enum GenericKind {
|
|
|
|
NONE(""),
|
|
|
|
ONE("<X>"),
|
|
|
|
TWO("<X,Y>");
|
|
|
|
|
|
|
|
String typeParameters;
|
|
|
|
|
|
|
|
GenericKind(String typeParameters) {
|
|
|
|
this.typeParameters = typeParameters;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum QualifierKind {
|
|
|
|
THIS("this"),
|
|
|
|
SUPER("super"),
|
|
|
|
NEW("new Foo()"),
|
|
|
|
METHOD("m()"),
|
|
|
|
FIELD("a.f"),
|
|
|
|
UBOUND_SIMPLE("A"),
|
2012-10-06 09:35:38 +00:00
|
|
|
UNBOUND_ARRAY1("int[]"),
|
|
|
|
UNBOUND_ARRAY2("A<G>[][]"),
|
2011-11-28 16:05:46 +00:00
|
|
|
UNBOUND_GENERIC1("A<X>"),
|
|
|
|
UNBOUND_GENERIC2("A<X, Y>"),
|
2011-12-19 12:07:07 +00:00
|
|
|
UNBOUND_GENERIC3("A<? extends X, ? super Y>"),
|
|
|
|
UNBOUND_GENERIC4("A<int[], short[][]>"),
|
|
|
|
NESTED_GENERIC1("A<A<X,Y>, A<X,Y>>"),
|
|
|
|
NESTED_GENERIC2("A<A<A<X,Y>,A<X,Y>>, A<A<X,Y>,A<X,Y>>>");
|
2011-11-28 16:05:46 +00:00
|
|
|
|
|
|
|
String qualifier;
|
|
|
|
|
|
|
|
QualifierKind(String qualifier) {
|
|
|
|
this.qualifier = qualifier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ExprKind {
|
2012-10-06 09:35:38 +00:00
|
|
|
NONE("#R::S"),
|
2011-11-28 16:05:46 +00:00
|
|
|
SINGLE_PAREN1("(#R#S)"),
|
|
|
|
SINGLE_PAREN2("(#R)#S"),
|
|
|
|
DOUBLE_PAREN1("((#R#S))"),
|
|
|
|
DOUBLE_PAREN2("((#R)#S)"),
|
|
|
|
DOUBLE_PAREN3("((#R))#S");
|
|
|
|
|
|
|
|
String expressionTemplate;
|
|
|
|
|
|
|
|
ExprKind(String expressionTemplate) {
|
|
|
|
this.expressionTemplate = expressionTemplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
|
|
|
|
return expressionTemplate
|
|
|
|
.replaceAll("#R", rk.getReferenceString(qk, gk))
|
|
|
|
.replaceAll("#S", sk.subExpression);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum SubExprKind {
|
|
|
|
NONE(""),
|
|
|
|
SELECT_FIELD(".f"),
|
|
|
|
SELECT_METHOD(".f()"),
|
|
|
|
SELECT_NEW(".new Foo()"),
|
|
|
|
POSTINC("++"),
|
|
|
|
POSTDEC("--");
|
|
|
|
|
|
|
|
String subExpression;
|
|
|
|
|
|
|
|
SubExprKind(String subExpression) {
|
|
|
|
this.subExpression = subExpression;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String... args) throws Exception {
|
|
|
|
|
|
|
|
//create default shared JavaCompiler - reused across multiple compilations
|
|
|
|
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
|
|
|
|
StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
|
|
|
|
|
|
|
|
for (ReferenceKind rk : ReferenceKind.values()) {
|
|
|
|
for (QualifierKind qk : QualifierKind.values()) {
|
|
|
|
for (GenericKind gk : GenericKind.values()) {
|
|
|
|
for (SubExprKind sk : SubExprKind.values()) {
|
|
|
|
for (ExprKind ek : ExprKind.values()) {
|
2011-12-19 12:07:07 +00:00
|
|
|
for (ContextKind ck : ContextKind.values()) {
|
|
|
|
new MethodReferenceParserTest(rk, qk, gk, sk, ek, ck).run(comp, fm);
|
|
|
|
}
|
2011-11-28 16:05:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println("Total check executed: " + checkCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReferenceKind rk;
|
|
|
|
QualifierKind qk;
|
|
|
|
GenericKind gk;
|
|
|
|
SubExprKind sk;
|
|
|
|
ExprKind ek;
|
2011-12-19 12:07:07 +00:00
|
|
|
ContextKind ck;
|
2011-11-28 16:05:46 +00:00
|
|
|
JavaSource source;
|
|
|
|
DiagnosticChecker diagChecker;
|
|
|
|
|
2011-12-19 12:07:07 +00:00
|
|
|
MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek, ContextKind ck) {
|
2011-11-28 16:05:46 +00:00
|
|
|
this.rk = rk;
|
|
|
|
this.qk = qk;
|
|
|
|
this.gk = gk;
|
|
|
|
this.sk = sk;
|
|
|
|
this.ek = ek;
|
2011-12-19 12:07:07 +00:00
|
|
|
this.ck = ck;
|
2011-11-28 16:05:46 +00:00
|
|
|
this.source = new JavaSource();
|
|
|
|
this.diagChecker = new DiagnosticChecker();
|
|
|
|
}
|
|
|
|
|
|
|
|
class JavaSource extends SimpleJavaFileObject {
|
|
|
|
|
|
|
|
String template = "class Test {\n" +
|
2011-12-19 12:07:07 +00:00
|
|
|
" void test() {\n" +
|
|
|
|
" #C\n" +
|
|
|
|
" }" +
|
2011-11-28 16:05:46 +00:00
|
|
|
"}";
|
|
|
|
|
|
|
|
String source;
|
|
|
|
|
|
|
|
public JavaSource() {
|
|
|
|
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
|
2011-12-19 12:07:07 +00:00
|
|
|
source = template.replaceAll("#C", ck.contextString(ek, rk, qk, gk, sk));
|
2011-11-28 16:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
|
|
|
|
JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
|
2012-11-17 19:01:03 +00:00
|
|
|
null, null, Arrays.asList(source));
|
2011-11-28 16:05:46 +00:00
|
|
|
try {
|
|
|
|
ct.parse();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true));
|
|
|
|
}
|
|
|
|
check();
|
|
|
|
}
|
|
|
|
|
|
|
|
void check() {
|
|
|
|
checkCount++;
|
|
|
|
|
|
|
|
if (diagChecker.errorFound != rk.erroneous()) {
|
|
|
|
throw new Error("invalid diagnostics for source:\n" +
|
|
|
|
source.getCharContent(true) +
|
|
|
|
"\nFound error: " + diagChecker.errorFound +
|
|
|
|
"\nExpected error: " + rk.erroneous());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
|
|
|
|
|
|
|
|
boolean errorFound;
|
|
|
|
|
|
|
|
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
|
|
|
|
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
|
|
|
|
errorFound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|