test/tools/javac/lambda/TestSelfRef.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/TestSelfRef.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,207 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8003280
    1.30 + * @summary Add lambda tests
    1.31 + *  Check that self/forward references from lambda expressions behave
    1.32 + *          consistently w.r.t. local inner classes
    1.33 + */
    1.34 +
    1.35 +import java.net.URI;
    1.36 +import java.util.Arrays;
    1.37 +import javax.tools.Diagnostic;
    1.38 +import javax.tools.JavaCompiler;
    1.39 +import javax.tools.JavaFileObject;
    1.40 +import javax.tools.SimpleJavaFileObject;
    1.41 +import javax.tools.StandardJavaFileManager;
    1.42 +import javax.tools.ToolProvider;
    1.43 +import com.sun.source.util.JavacTask;
    1.44 +
    1.45 +public class TestSelfRef {
    1.46 +
    1.47 +    static int checkCount = 0;
    1.48 +
    1.49 +    enum RefKind {
    1.50 +        SELF_LAMBDA("SAM s = x->{ System.out.println(s); };", true, false),
    1.51 +        FORWARD_LAMBDA("SAM s = x->{ System.out.println(f); };\nObject f = null;", false, true),
    1.52 +        SELF_ANON("Object s = new Object() { void test() { System.out.println(s); } };", true, false),
    1.53 +        FORWARD_ANON("Object s = new Object() { void test() { System.out.println(f); } }; Object f = null;", false, true);
    1.54 +
    1.55 +        String refStr;
    1.56 +        boolean selfRef;
    1.57 +        boolean forwardRef;
    1.58 +
    1.59 +        private RefKind(String refStr, boolean selfRef, boolean forwardRef) {
    1.60 +            this.refStr = refStr;
    1.61 +            this.selfRef = selfRef;
    1.62 +            this.forwardRef = forwardRef;
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    enum EnclosingKind {
    1.67 +        TOPLEVEL("class C { #S }"),
    1.68 +        MEMBER_INNER("class Outer { class C { #S } }"),
    1.69 +        NESTED_INNER("class Outer { static class C { #S } }");
    1.70 +
    1.71 +        String enclStr;
    1.72 +
    1.73 +        private EnclosingKind(String enclStr) {
    1.74 +            this.enclStr = enclStr;
    1.75 +        }
    1.76 +    }
    1.77 +
    1.78 +    enum InnerKind {
    1.79 +        NONE("#R"),
    1.80 +        LOCAL_NONE("class Local { #R }"),
    1.81 +        LOCAL_MTH("class Local { void test() { #R } }"),
    1.82 +        ANON_NONE("new Object() { #R };"),
    1.83 +        ANON_MTH("new Object() { void test() { #R } };");
    1.84 +
    1.85 +        String innerStr;
    1.86 +
    1.87 +        private InnerKind(String innerStr) {
    1.88 +            this.innerStr = innerStr;
    1.89 +        }
    1.90 +
    1.91 +        boolean inMethodContext(SiteKind sk) {
    1.92 +            switch (this) {
    1.93 +                case LOCAL_MTH:
    1.94 +                case ANON_MTH: return true;
    1.95 +                case NONE: return sk != SiteKind.NONE;
    1.96 +                default:
    1.97 +                    return false;
    1.98 +            }
    1.99 +        }
   1.100 +    }
   1.101 +
   1.102 +    enum SiteKind {
   1.103 +        NONE("#I"),
   1.104 +        STATIC_INIT("static { #I }"),
   1.105 +        INSTANCE_INIT("{ #I }"),
   1.106 +        CONSTRUCTOR("C() { #I }"),
   1.107 +        METHOD("void test() { #I }");
   1.108 +
   1.109 +        String siteStr;
   1.110 +
   1.111 +        private SiteKind(String siteStr) {
   1.112 +            this.siteStr = siteStr;
   1.113 +        }
   1.114 +    }
   1.115 +
   1.116 +    public static void main(String... args) throws Exception {
   1.117 +
   1.118 +        //create default shared JavaCompiler - reused across multiple compilations
   1.119 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   1.120 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   1.121 +
   1.122 +        for (EnclosingKind ek : EnclosingKind.values()) {
   1.123 +            for (SiteKind sk : SiteKind.values()) {
   1.124 +                if (sk == SiteKind.STATIC_INIT && ek == EnclosingKind.MEMBER_INNER)
   1.125 +                    continue;
   1.126 +                for (InnerKind ik : InnerKind.values()) {
   1.127 +                    if (ik != InnerKind.NONE && sk == SiteKind.NONE)
   1.128 +                        break;
   1.129 +                    for (RefKind rk : RefKind.values()) {
   1.130 +                        new TestSelfRef(ek, sk, ik, rk).run(comp, fm);
   1.131 +                    }
   1.132 +                }
   1.133 +            }
   1.134 +        }
   1.135 +        System.out.println("Total check executed: " + checkCount);
   1.136 +    }
   1.137 +
   1.138 +    EnclosingKind ek;
   1.139 +    SiteKind sk;
   1.140 +    InnerKind ik;
   1.141 +    RefKind rk;
   1.142 +    JavaSource source;
   1.143 +    DiagnosticChecker diagChecker;
   1.144 +
   1.145 +    TestSelfRef(EnclosingKind ek, SiteKind sk, InnerKind ik, RefKind rk) {
   1.146 +        this.ek = ek;
   1.147 +        this.sk = sk;
   1.148 +        this.ik = ik;
   1.149 +        this.rk = rk;
   1.150 +        this.source = new JavaSource();
   1.151 +        this.diagChecker = new DiagnosticChecker();
   1.152 +    }
   1.153 +
   1.154 +    class JavaSource extends SimpleJavaFileObject {
   1.155 +
   1.156 +        String bodyTemplate = "interface SAM { void test(Object o); }\n#B";
   1.157 +        String source;
   1.158 +
   1.159 +        public JavaSource() {
   1.160 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   1.161 +            source = bodyTemplate.replace("#B",
   1.162 +                    ek.enclStr.replace("#S", sk.siteStr.replace("#I", ik.innerStr.replace("#R", rk.refStr))));
   1.163 +        }
   1.164 +
   1.165 +        @Override
   1.166 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.167 +            return source;
   1.168 +        }
   1.169 +    }
   1.170 +
   1.171 +    void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
   1.172 +        JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
   1.173 +                null, null, Arrays.asList(source));
   1.174 +        try {
   1.175 +            ct.analyze();
   1.176 +        } catch (Throwable ex) {
   1.177 +            throw new AssertionError("Error thron when compiling the following code:\n" + source.getCharContent(true));
   1.178 +        }
   1.179 +        check();
   1.180 +    }
   1.181 +
   1.182 +    boolean isErrorExpected() {
   1.183 +        //illegal forward ref
   1.184 +        boolean result = ik.inMethodContext(sk) && (rk.selfRef || rk.forwardRef);
   1.185 +        result |= (rk == RefKind.SELF_LAMBDA || rk == RefKind.FORWARD_LAMBDA);
   1.186 +        return result;
   1.187 +    }
   1.188 +
   1.189 +    void check() {
   1.190 +        checkCount++;
   1.191 +        boolean errorExpected = isErrorExpected();
   1.192 +        if (diagChecker.errorFound != errorExpected) {
   1.193 +            throw new Error("invalid diagnostics for source:\n" +
   1.194 +                source.getCharContent(true) +
   1.195 +                "\nFound error: " + diagChecker.errorFound +
   1.196 +                "\nExpected error: " + errorExpected);
   1.197 +        }
   1.198 +    }
   1.199 +
   1.200 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.201 +
   1.202 +        boolean errorFound;
   1.203 +
   1.204 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.205 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.206 +                errorFound = true;
   1.207 +            }
   1.208 +        }
   1.209 +    }
   1.210 +}

mercurial