Sun, 04 Nov 2012 10:59:42 +0000
7192246: Add type-checking support for default methods
Summary: Add type-checking support for default methods as per Featherweight-Defender document
Reviewed-by: jjg, dlsmith
mcimadamore@1393 | 1 | /* |
mcimadamore@1393 | 2 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1393 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1393 | 4 | * |
mcimadamore@1393 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1393 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1393 | 7 | * published by the Free Software Foundation. |
mcimadamore@1393 | 8 | * |
mcimadamore@1393 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1393 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1393 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1393 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1393 | 13 | * accompanied this code). |
mcimadamore@1393 | 14 | * |
mcimadamore@1393 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1393 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1393 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1393 | 18 | * |
mcimadamore@1393 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1393 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1393 | 21 | * questions. |
mcimadamore@1393 | 22 | */ |
mcimadamore@1393 | 23 | |
mcimadamore@1393 | 24 | /* |
mcimadamore@1393 | 25 | * @test |
mcimadamore@1393 | 26 | * @summary Automatic test for checking correctness of default resolution |
mcimadamore@1393 | 27 | */ |
mcimadamore@1393 | 28 | |
mcimadamore@1393 | 29 | import shapegen.*; |
mcimadamore@1393 | 30 | |
mcimadamore@1393 | 31 | import com.sun.source.util.JavacTask; |
mcimadamore@1393 | 32 | |
mcimadamore@1393 | 33 | import java.net.URI; |
mcimadamore@1393 | 34 | import java.util.Arrays; |
mcimadamore@1393 | 35 | import java.util.ArrayList; |
mcimadamore@1393 | 36 | import java.util.Collection; |
mcimadamore@1393 | 37 | import java.util.List; |
mcimadamore@1393 | 38 | |
mcimadamore@1393 | 39 | import javax.tools.Diagnostic; |
mcimadamore@1393 | 40 | import javax.tools.JavaCompiler; |
mcimadamore@1393 | 41 | import javax.tools.JavaFileObject; |
mcimadamore@1393 | 42 | import javax.tools.SimpleJavaFileObject; |
mcimadamore@1393 | 43 | import javax.tools.StandardJavaFileManager; |
mcimadamore@1393 | 44 | import javax.tools.ToolProvider; |
mcimadamore@1393 | 45 | |
mcimadamore@1393 | 46 | public class FDTest { |
mcimadamore@1393 | 47 | |
mcimadamore@1393 | 48 | enum TestKind { |
mcimadamore@1393 | 49 | POSITIVE, |
mcimadamore@1393 | 50 | NEGATIVE; |
mcimadamore@1393 | 51 | |
mcimadamore@1393 | 52 | Collection<Hierarchy> getHierarchy(HierarchyGenerator hg) { |
mcimadamore@1393 | 53 | return this == POSITIVE ? |
mcimadamore@1393 | 54 | hg.getOK() : hg.getErr(); |
mcimadamore@1393 | 55 | } |
mcimadamore@1393 | 56 | } |
mcimadamore@1393 | 57 | |
mcimadamore@1393 | 58 | public static void main(String[] args) throws Exception { |
mcimadamore@1393 | 59 | //create default shared JavaCompiler - reused across multiple compilations |
mcimadamore@1393 | 60 | JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@1393 | 61 | StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
mcimadamore@1393 | 62 | |
mcimadamore@1393 | 63 | HierarchyGenerator hg = new HierarchyGenerator(); |
mcimadamore@1393 | 64 | for (TestKind tk : TestKind.values()) { |
mcimadamore@1393 | 65 | for (Hierarchy hs : tk.getHierarchy(hg)) { |
mcimadamore@1393 | 66 | new FDTest(tk, hs).run(comp, fm); |
mcimadamore@1393 | 67 | } |
mcimadamore@1393 | 68 | } |
mcimadamore@1393 | 69 | } |
mcimadamore@1393 | 70 | |
mcimadamore@1393 | 71 | TestKind tk; |
mcimadamore@1393 | 72 | Hierarchy hs; |
mcimadamore@1393 | 73 | DefenderTestSource source; |
mcimadamore@1393 | 74 | DiagnosticChecker diagChecker; |
mcimadamore@1393 | 75 | |
mcimadamore@1393 | 76 | FDTest(TestKind tk, Hierarchy hs) { |
mcimadamore@1393 | 77 | this.tk = tk; |
mcimadamore@1393 | 78 | this.hs = hs; |
mcimadamore@1393 | 79 | this.source = new DefenderTestSource(); |
mcimadamore@1393 | 80 | this.diagChecker = new DiagnosticChecker(); |
mcimadamore@1393 | 81 | } |
mcimadamore@1393 | 82 | |
mcimadamore@1393 | 83 | void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception { |
mcimadamore@1393 | 84 | JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker, |
mcimadamore@1393 | 85 | Arrays.asList("-XDallowDefaultMethods"), null, Arrays.asList(source)); |
mcimadamore@1393 | 86 | try { |
mcimadamore@1393 | 87 | ct.analyze(); |
mcimadamore@1393 | 88 | } catch (Throwable ex) { |
mcimadamore@1393 | 89 | throw new AssertionError("Error thrown when analyzing the following source:\n" + source.getCharContent(true)); |
mcimadamore@1393 | 90 | } |
mcimadamore@1393 | 91 | check(); |
mcimadamore@1393 | 92 | } |
mcimadamore@1393 | 93 | |
mcimadamore@1393 | 94 | void check() { |
mcimadamore@1393 | 95 | boolean errorExpected = tk == TestKind.NEGATIVE; |
mcimadamore@1393 | 96 | if (errorExpected != diagChecker.errorFound) { |
mcimadamore@1393 | 97 | throw new AssertionError("problem in source: \n" + |
mcimadamore@1393 | 98 | "\nerror found = " + diagChecker.errorFound + |
mcimadamore@1393 | 99 | "\nerror expected = " + errorExpected + |
mcimadamore@1393 | 100 | "\n" + dumpHierarchy() + |
mcimadamore@1393 | 101 | "\n" + source.getCharContent(true)); |
mcimadamore@1393 | 102 | } |
mcimadamore@1393 | 103 | } |
mcimadamore@1393 | 104 | |
mcimadamore@1393 | 105 | String dumpHierarchy() { |
mcimadamore@1393 | 106 | StringBuilder buf = new StringBuilder(); |
mcimadamore@1393 | 107 | buf.append("root = " + hs.root + "\n"); |
mcimadamore@1393 | 108 | for (ClassCase cc : hs.all) { |
mcimadamore@1393 | 109 | buf.append(" class name = " + cc.getName() + "\n"); |
mcimadamore@1393 | 110 | buf.append(" class OK = " + cc.get_OK() + "\n"); |
mcimadamore@1393 | 111 | buf.append(" prov = " + cc.get_mprov() + "\n"); |
mcimadamore@1393 | 112 | |
mcimadamore@1393 | 113 | } |
mcimadamore@1393 | 114 | return buf.toString(); |
mcimadamore@1393 | 115 | } |
mcimadamore@1393 | 116 | |
mcimadamore@1393 | 117 | class DefenderTestSource extends SimpleJavaFileObject { |
mcimadamore@1393 | 118 | |
mcimadamore@1393 | 119 | String source; |
mcimadamore@1393 | 120 | |
mcimadamore@1393 | 121 | public DefenderTestSource() { |
mcimadamore@1393 | 122 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@1393 | 123 | StringBuilder buf = new StringBuilder(); |
mcimadamore@1393 | 124 | List<ClassCase> defaultRef = new ArrayList<>(); |
mcimadamore@1393 | 125 | for (ClassCase cc : hs.all) { |
mcimadamore@1393 | 126 | hs.genClassDef(buf, cc, null, defaultRef); |
mcimadamore@1393 | 127 | } |
mcimadamore@1393 | 128 | source = buf.toString(); |
mcimadamore@1393 | 129 | } |
mcimadamore@1393 | 130 | |
mcimadamore@1393 | 131 | @Override |
mcimadamore@1393 | 132 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@1393 | 133 | return source; |
mcimadamore@1393 | 134 | } |
mcimadamore@1393 | 135 | } |
mcimadamore@1393 | 136 | |
mcimadamore@1393 | 137 | static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
mcimadamore@1393 | 138 | |
mcimadamore@1393 | 139 | boolean errorFound; |
mcimadamore@1393 | 140 | |
mcimadamore@1393 | 141 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@1393 | 142 | if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
mcimadamore@1393 | 143 | errorFound = true; |
mcimadamore@1393 | 144 | } |
mcimadamore@1393 | 145 | } |
mcimadamore@1393 | 146 | } |
mcimadamore@1393 | 147 | } |