test/tools/javac/lambda/TestSelfRef.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2148
c4292590fc70
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 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 8003280
aoqi@0 27 * @summary Add lambda tests
aoqi@0 28 * Check that self/forward references from lambda expressions behave
aoqi@0 29 * consistently w.r.t. local inner classes
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 import java.net.URI;
aoqi@0 33 import java.util.Arrays;
aoqi@0 34 import javax.tools.Diagnostic;
aoqi@0 35 import javax.tools.JavaCompiler;
aoqi@0 36 import javax.tools.JavaFileObject;
aoqi@0 37 import javax.tools.SimpleJavaFileObject;
aoqi@0 38 import javax.tools.StandardJavaFileManager;
aoqi@0 39 import javax.tools.ToolProvider;
aoqi@0 40 import com.sun.source.util.JavacTask;
aoqi@0 41
aoqi@0 42 public class TestSelfRef {
aoqi@0 43
aoqi@0 44 static int checkCount = 0;
aoqi@0 45
aoqi@0 46 enum RefKind {
aoqi@0 47 SELF_LAMBDA("SAM s = x->{ System.out.println(s); };", true, false),
aoqi@0 48 FORWARD_LAMBDA("SAM s = x->{ System.out.println(f); };\nObject f = null;", false, true),
aoqi@0 49 SELF_ANON("Object s = new Object() { void test() { System.out.println(s); } };", true, false),
aoqi@0 50 FORWARD_ANON("Object s = new Object() { void test() { System.out.println(f); } }; Object f = null;", false, true);
aoqi@0 51
aoqi@0 52 String refStr;
aoqi@0 53 boolean selfRef;
aoqi@0 54 boolean forwardRef;
aoqi@0 55
aoqi@0 56 private RefKind(String refStr, boolean selfRef, boolean forwardRef) {
aoqi@0 57 this.refStr = refStr;
aoqi@0 58 this.selfRef = selfRef;
aoqi@0 59 this.forwardRef = forwardRef;
aoqi@0 60 }
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 enum EnclosingKind {
aoqi@0 64 TOPLEVEL("class C { #S }"),
aoqi@0 65 MEMBER_INNER("class Outer { class C { #S } }"),
aoqi@0 66 NESTED_INNER("class Outer { static class C { #S } }");
aoqi@0 67
aoqi@0 68 String enclStr;
aoqi@0 69
aoqi@0 70 private EnclosingKind(String enclStr) {
aoqi@0 71 this.enclStr = enclStr;
aoqi@0 72 }
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 enum InnerKind {
aoqi@0 76 NONE("#R"),
aoqi@0 77 LOCAL_NONE("class Local { #R }"),
aoqi@0 78 LOCAL_MTH("class Local { void test() { #R } }"),
aoqi@0 79 ANON_NONE("new Object() { #R };"),
aoqi@0 80 ANON_MTH("new Object() { void test() { #R } };");
aoqi@0 81
aoqi@0 82 String innerStr;
aoqi@0 83
aoqi@0 84 private InnerKind(String innerStr) {
aoqi@0 85 this.innerStr = innerStr;
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 boolean inMethodContext(SiteKind sk) {
aoqi@0 89 switch (this) {
aoqi@0 90 case LOCAL_MTH:
aoqi@0 91 case ANON_MTH: return true;
aoqi@0 92 case NONE: return sk != SiteKind.NONE;
aoqi@0 93 default:
aoqi@0 94 return false;
aoqi@0 95 }
aoqi@0 96 }
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 enum SiteKind {
aoqi@0 100 NONE("#I"),
aoqi@0 101 STATIC_INIT("static { #I }"),
aoqi@0 102 INSTANCE_INIT("{ #I }"),
aoqi@0 103 CONSTRUCTOR("C() { #I }"),
aoqi@0 104 METHOD("void test() { #I }");
aoqi@0 105
aoqi@0 106 String siteStr;
aoqi@0 107
aoqi@0 108 private SiteKind(String siteStr) {
aoqi@0 109 this.siteStr = siteStr;
aoqi@0 110 }
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 public static void main(String... args) throws Exception {
aoqi@0 114
aoqi@0 115 //create default shared JavaCompiler - reused across multiple compilations
aoqi@0 116 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
aoqi@0 117 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
aoqi@0 118
aoqi@0 119 for (EnclosingKind ek : EnclosingKind.values()) {
aoqi@0 120 for (SiteKind sk : SiteKind.values()) {
aoqi@0 121 if (sk == SiteKind.STATIC_INIT && ek == EnclosingKind.MEMBER_INNER)
aoqi@0 122 continue;
aoqi@0 123 for (InnerKind ik : InnerKind.values()) {
aoqi@0 124 if (ik != InnerKind.NONE && sk == SiteKind.NONE)
aoqi@0 125 break;
aoqi@0 126 for (RefKind rk : RefKind.values()) {
aoqi@0 127 new TestSelfRef(ek, sk, ik, rk).run(comp, fm);
aoqi@0 128 }
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131 }
aoqi@0 132 System.out.println("Total check executed: " + checkCount);
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 EnclosingKind ek;
aoqi@0 136 SiteKind sk;
aoqi@0 137 InnerKind ik;
aoqi@0 138 RefKind rk;
aoqi@0 139 JavaSource source;
aoqi@0 140 DiagnosticChecker diagChecker;
aoqi@0 141
aoqi@0 142 TestSelfRef(EnclosingKind ek, SiteKind sk, InnerKind ik, RefKind rk) {
aoqi@0 143 this.ek = ek;
aoqi@0 144 this.sk = sk;
aoqi@0 145 this.ik = ik;
aoqi@0 146 this.rk = rk;
aoqi@0 147 this.source = new JavaSource();
aoqi@0 148 this.diagChecker = new DiagnosticChecker();
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 class JavaSource extends SimpleJavaFileObject {
aoqi@0 152
aoqi@0 153 String bodyTemplate = "interface SAM { void test(Object o); }\n#B";
aoqi@0 154 String source;
aoqi@0 155
aoqi@0 156 public JavaSource() {
aoqi@0 157 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
aoqi@0 158 source = bodyTemplate.replace("#B",
aoqi@0 159 ek.enclStr.replace("#S", sk.siteStr.replace("#I", ik.innerStr.replace("#R", rk.refStr))));
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 @Override
aoqi@0 163 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
aoqi@0 164 return source;
aoqi@0 165 }
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
aoqi@0 169 JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
aoqi@0 170 null, null, Arrays.asList(source));
aoqi@0 171 try {
aoqi@0 172 ct.analyze();
aoqi@0 173 } catch (Throwable ex) {
aoqi@0 174 throw new AssertionError("Error thron when compiling the following code:\n" + source.getCharContent(true));
aoqi@0 175 }
aoqi@0 176 check();
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 boolean isErrorExpected() {
aoqi@0 180 //illegal forward ref
aoqi@0 181 boolean result = ik.inMethodContext(sk) && (rk.selfRef || rk.forwardRef);
aoqi@0 182 result |= (rk == RefKind.SELF_LAMBDA || rk == RefKind.FORWARD_LAMBDA);
aoqi@0 183 return result;
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 void check() {
aoqi@0 187 checkCount++;
aoqi@0 188 boolean errorExpected = isErrorExpected();
aoqi@0 189 if (diagChecker.errorFound != errorExpected) {
aoqi@0 190 throw new Error("invalid diagnostics for source:\n" +
aoqi@0 191 source.getCharContent(true) +
aoqi@0 192 "\nFound error: " + diagChecker.errorFound +
aoqi@0 193 "\nExpected error: " + errorExpected);
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
aoqi@0 198
aoqi@0 199 boolean errorFound;
aoqi@0 200
aoqi@0 201 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
aoqi@0 202 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
aoqi@0 203 errorFound = true;
aoqi@0 204 }
aoqi@0 205 }
aoqi@0 206 }
aoqi@0 207 }

mercurial