test/tools/javac/lambda/FunctionalInterfaceConversionTest.java

changeset 1434
34d1ebaf4645
child 1482
954541f13717
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/FunctionalInterfaceConversionTest.java	Fri Nov 30 15:14:25 2012 +0000
     1.3 @@ -0,0 +1,280 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 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 8004102
    1.30 + * @summary Add lambda tests
    1.31 + *  perform several automated checks in lambda conversion, esp. around accessibility
    1.32 + * @author  Maurizio Cimadamore
    1.33 + * @run main FunctionalInterfaceConversionTest
    1.34 + */
    1.35 +
    1.36 +import com.sun.source.util.JavacTask;
    1.37 +import java.net.URI;
    1.38 +import java.util.Arrays;
    1.39 +import javax.tools.Diagnostic;
    1.40 +import javax.tools.JavaCompiler;
    1.41 +import javax.tools.JavaFileObject;
    1.42 +import javax.tools.SimpleJavaFileObject;
    1.43 +import javax.tools.StandardJavaFileManager;
    1.44 +import javax.tools.ToolProvider;
    1.45 +
    1.46 +public class FunctionalInterfaceConversionTest {
    1.47 +
    1.48 +    enum PackageKind {
    1.49 +        NO_PKG(""),
    1.50 +        PKG_A("a");
    1.51 +
    1.52 +        String pkg;
    1.53 +
    1.54 +        PackageKind(String pkg) {
    1.55 +            this.pkg = pkg;
    1.56 +        }
    1.57 +
    1.58 +        String getPkgDecl() {
    1.59 +            return this == NO_PKG ?
    1.60 +                "" :
    1.61 +                "package " + pkg + ";";
    1.62 +        }
    1.63 +
    1.64 +        String getImportStat() {
    1.65 +            return this == NO_PKG ?
    1.66 +                "" :
    1.67 +                "import " + pkg + ".*;";
    1.68 +        }
    1.69 +    }
    1.70 +
    1.71 +    enum SamKind {
    1.72 +        CLASS("public class Sam {  }"),
    1.73 +        ABSTACT_CLASS("public abstract class Sam {  }"),
    1.74 +        ANNOTATION("public @interface Sam {  }"),
    1.75 +        ENUM("public enum Sam { }"),
    1.76 +        INTERFACE("public interface Sam { \n #METH; \n }");
    1.77 +
    1.78 +        String sam_str;
    1.79 +
    1.80 +        SamKind(String sam_str) {
    1.81 +            this.sam_str = sam_str;
    1.82 +        }
    1.83 +
    1.84 +        String getSam(String methStr) {
    1.85 +            return sam_str.replaceAll("#METH", methStr);
    1.86 +        }
    1.87 +    }
    1.88 +
    1.89 +    enum ModifierKind {
    1.90 +        PUBLIC("public"),
    1.91 +        PACKAGE("");
    1.92 +
    1.93 +        String modifier_str;
    1.94 +
    1.95 +        ModifierKind(String modifier_str) {
    1.96 +            this.modifier_str = modifier_str;
    1.97 +        }
    1.98 +
    1.99 +        boolean stricterThan(ModifierKind that) {
   1.100 +            return this.ordinal() > that.ordinal();
   1.101 +        }
   1.102 +    }
   1.103 +
   1.104 +    enum TypeKind {
   1.105 +        EXCEPTION("Exception"),
   1.106 +        PKG_CLASS("PackageClass");
   1.107 +
   1.108 +        String typeStr;
   1.109 +
   1.110 +        private TypeKind(String typeStr) {
   1.111 +            this.typeStr = typeStr;
   1.112 +        }
   1.113 +    }
   1.114 +
   1.115 +    enum ExprKind {
   1.116 +        LAMBDA("x -> null"),
   1.117 +        MREF("this::m");
   1.118 +
   1.119 +        String exprStr;
   1.120 +
   1.121 +        private ExprKind(String exprStr) {
   1.122 +            this.exprStr = exprStr;
   1.123 +        }
   1.124 +    }
   1.125 +
   1.126 +    enum MethodKind {
   1.127 +        NONE(""),
   1.128 +        NON_GENERIC("public abstract #R m(#ARG s) throws #T;"),
   1.129 +        GENERIC("public abstract <X> #R m(#ARG s) throws #T;");
   1.130 +
   1.131 +        String methodTemplate;
   1.132 +
   1.133 +        private MethodKind(String methodTemplate) {
   1.134 +            this.methodTemplate = methodTemplate;
   1.135 +        }
   1.136 +
   1.137 +        String getMethod(TypeKind retType, TypeKind argType, TypeKind thrownType) {
   1.138 +            return methodTemplate.replaceAll("#R", retType.typeStr).
   1.139 +                    replaceAll("#ARG", argType.typeStr).
   1.140 +                    replaceAll("#T", thrownType.typeStr);
   1.141 +        }
   1.142 +    }
   1.143 +
   1.144 +    public static void main(String[] args) throws Exception {
   1.145 +        final JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   1.146 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   1.147 +        for (PackageKind samPkg : PackageKind.values()) {
   1.148 +            for (ModifierKind modKind : ModifierKind.values()) {
   1.149 +                for (SamKind samKind : SamKind.values()) {
   1.150 +                    for (MethodKind samMeth : MethodKind.values()) {
   1.151 +                        for (MethodKind clientMeth : MethodKind.values()) {
   1.152 +                            for (TypeKind retType : TypeKind.values()) {
   1.153 +                                for (TypeKind argType : TypeKind.values()) {
   1.154 +                                    for (TypeKind thrownType : TypeKind.values()) {
   1.155 +                                        for (ExprKind exprKind : ExprKind.values()) {
   1.156 +                                            new FunctionalInterfaceConversionTest(samPkg, modKind, samKind,
   1.157 +                                                    samMeth, clientMeth, retType, argType, thrownType, exprKind).test(comp, fm);
   1.158 +                                        }
   1.159 +                                    }
   1.160 +                                }
   1.161 +                            }
   1.162 +                        }
   1.163 +                    }
   1.164 +                }
   1.165 +            }
   1.166 +        }
   1.167 +    }
   1.168 +
   1.169 +    PackageKind samPkg;
   1.170 +    ModifierKind modKind;
   1.171 +    SamKind samKind;
   1.172 +    MethodKind samMeth;
   1.173 +    MethodKind clientMeth;
   1.174 +    TypeKind retType;
   1.175 +    TypeKind argType;
   1.176 +    TypeKind thrownType;
   1.177 +    ExprKind exprKind;
   1.178 +    DiagnosticChecker dc;
   1.179 +
   1.180 +    SourceFile samSourceFile = new SourceFile("Sam.java", "#P \n #C") {
   1.181 +        public String toString() {
   1.182 +            return template.replaceAll("#P", samPkg.getPkgDecl()).
   1.183 +                    replaceAll("#C", samKind.getSam(samMeth.getMethod(retType, argType, thrownType)));
   1.184 +        }
   1.185 +    };
   1.186 +
   1.187 +    SourceFile pkgClassSourceFile = new SourceFile("PackageClass.java",
   1.188 +                                                   "#P\n #M class PackageClass extends Exception { }") {
   1.189 +        public String toString() {
   1.190 +            return template.replaceAll("#P", samPkg.getPkgDecl()).
   1.191 +                    replaceAll("#M", modKind.modifier_str);
   1.192 +        }
   1.193 +    };
   1.194 +
   1.195 +    SourceFile clientSourceFile = new SourceFile("Client.java",
   1.196 +                                                 "#I\n abstract class Client { \n" +
   1.197 +                                                 "  Sam s = #E;\n" +
   1.198 +                                                 "  #M \n }") {
   1.199 +        public String toString() {
   1.200 +            return template.replaceAll("#I", samPkg.getImportStat())
   1.201 +                    .replaceAll("#E", exprKind.exprStr)
   1.202 +                    .replaceAll("#M", clientMeth.getMethod(retType, argType, thrownType));
   1.203 +        }
   1.204 +    };
   1.205 +
   1.206 +    FunctionalInterfaceConversionTest(PackageKind samPkg, ModifierKind modKind, SamKind samKind,
   1.207 +            MethodKind samMeth, MethodKind clientMeth, TypeKind retType, TypeKind argType,
   1.208 +            TypeKind thrownType, ExprKind exprKind) {
   1.209 +        this.samPkg = samPkg;
   1.210 +        this.modKind = modKind;
   1.211 +        this.samKind = samKind;
   1.212 +        this.samMeth = samMeth;
   1.213 +        this.clientMeth = clientMeth;
   1.214 +        this.retType = retType;
   1.215 +        this.argType = argType;
   1.216 +        this.thrownType = thrownType;
   1.217 +        this.exprKind = exprKind;
   1.218 +        this.dc = new DiagnosticChecker();
   1.219 +    }
   1.220 +
   1.221 +    void test(JavaCompiler comp, StandardJavaFileManager fm) throws Exception {
   1.222 +        JavacTask ct = (JavacTask)comp.getTask(null, fm, dc,
   1.223 +                null, null, Arrays.asList(samSourceFile, pkgClassSourceFile, clientSourceFile));
   1.224 +        ct.analyze();
   1.225 +        if (dc.errorFound == checkSamConversion()) {
   1.226 +            throw new AssertionError(samSourceFile + "\n\n" + pkgClassSourceFile + "\n\n" + clientSourceFile);
   1.227 +        }
   1.228 +    }
   1.229 +
   1.230 +    boolean checkSamConversion() {
   1.231 +        if (samKind != SamKind.INTERFACE) {
   1.232 +            //sam type must be an interface
   1.233 +            return false;
   1.234 +        } else if (samMeth == MethodKind.NONE) {
   1.235 +            //interface must have at least a method
   1.236 +            return false;
   1.237 +        } else if (exprKind == ExprKind.LAMBDA &&
   1.238 +                samMeth != MethodKind.NON_GENERIC) {
   1.239 +            //target method for lambda must be non-generic
   1.240 +            return false;
   1.241 +        } else if (exprKind == ExprKind.MREF &&
   1.242 +                clientMeth == MethodKind.NONE) {
   1.243 +            return false;
   1.244 +        } else if (samPkg != PackageKind.NO_PKG &&
   1.245 +                modKind != ModifierKind.PUBLIC &&
   1.246 +                (retType == TypeKind.PKG_CLASS ||
   1.247 +                argType == TypeKind.PKG_CLASS ||
   1.248 +                thrownType == TypeKind.PKG_CLASS)) {
   1.249 +            //target must not contain inaccessible types
   1.250 +            return false;
   1.251 +        } else {
   1.252 +            return true;
   1.253 +        }
   1.254 +    }
   1.255 +
   1.256 +    abstract class SourceFile extends SimpleJavaFileObject {
   1.257 +
   1.258 +        protected String template;
   1.259 +
   1.260 +        public SourceFile(String filename, String template) {
   1.261 +            super(URI.create("myfo:/" + filename), JavaFileObject.Kind.SOURCE);
   1.262 +            this.template = template;
   1.263 +        }
   1.264 +
   1.265 +        @Override
   1.266 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.267 +            return toString();
   1.268 +        }
   1.269 +
   1.270 +        public abstract String toString();
   1.271 +    }
   1.272 +
   1.273 +    static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
   1.274 +
   1.275 +        boolean errorFound = false;
   1.276 +
   1.277 +        public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.278 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
   1.279 +                errorFound = true;
   1.280 +            }
   1.281 +        }
   1.282 +    }
   1.283 +}

mercurial