Thu, 11 Jul 2013 14:07:39 +0100
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
8020010: Move lambda bridge creation from metafactory and VM to compiler
Summary: langtools/javac component of the bridge support and MethodType vs. MethodHandle changes.
Reviewed-by: jjg, vromero, briangoetz, forax
Contributed-by: robert.field@oracle.com
mcimadamore@1882 | 1 | /* |
mcimadamore@1882 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1882 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1882 | 4 | * |
mcimadamore@1882 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1882 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1882 | 7 | * published by the Free Software Foundation. |
mcimadamore@1882 | 8 | * |
mcimadamore@1882 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1882 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1882 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1882 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1882 | 13 | * accompanied this code). |
mcimadamore@1882 | 14 | * |
mcimadamore@1882 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1882 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1882 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1882 | 18 | * |
mcimadamore@1882 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1882 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1882 | 21 | * questions. |
mcimadamore@1882 | 22 | */ |
mcimadamore@1882 | 23 | |
mcimadamore@1882 | 24 | /* |
mcimadamore@1882 | 25 | * @test |
mcimadamore@1882 | 26 | * @bug 8013789 |
mcimadamore@1882 | 27 | * @summary Compiler should emit bridges in interfaces |
mcimadamore@1882 | 28 | * @library /tools/javac/lib |
mcimadamore@1882 | 29 | * @build JavacTestingAbstractProcessor BridgeHarness |
mcimadamore@1882 | 30 | * @run main BridgeHarness |
mcimadamore@1882 | 31 | */ |
mcimadamore@1882 | 32 | |
mcimadamore@1882 | 33 | import com.sun.source.util.JavacTask; |
mcimadamore@1882 | 34 | import com.sun.tools.classfile.AccessFlags; |
mcimadamore@1882 | 35 | import com.sun.tools.classfile.ClassFile; |
mcimadamore@1882 | 36 | import com.sun.tools.classfile.ConstantPool; |
mcimadamore@1882 | 37 | import com.sun.tools.classfile.ConstantPoolException; |
mcimadamore@1882 | 38 | import com.sun.tools.classfile.Method; |
mcimadamore@1882 | 39 | import com.sun.tools.javac.code.Symbol.ClassSymbol; |
mcimadamore@1882 | 40 | import com.sun.tools.javac.util.List; |
mcimadamore@1882 | 41 | |
mcimadamore@1882 | 42 | import java.io.File; |
mcimadamore@1882 | 43 | import java.util.Arrays; |
mcimadamore@1882 | 44 | import java.util.Collections; |
mcimadamore@1882 | 45 | import java.util.HashMap; |
mcimadamore@1882 | 46 | import java.util.Map; |
mcimadamore@1882 | 47 | import java.util.Set; |
mcimadamore@1882 | 48 | |
mcimadamore@1882 | 49 | import javax.annotation.processing.RoundEnvironment; |
mcimadamore@1882 | 50 | import javax.annotation.processing.SupportedAnnotationTypes; |
mcimadamore@1882 | 51 | import javax.lang.model.element.Element; |
mcimadamore@1882 | 52 | import javax.lang.model.element.TypeElement; |
mcimadamore@1882 | 53 | import javax.tools.JavaCompiler; |
mcimadamore@1882 | 54 | import javax.tools.JavaFileObject; |
mcimadamore@1882 | 55 | import javax.tools.StandardJavaFileManager; |
mcimadamore@1882 | 56 | import javax.tools.ToolProvider; |
mcimadamore@1882 | 57 | |
mcimadamore@1882 | 58 | import static javax.tools.StandardLocation.*; |
mcimadamore@1882 | 59 | |
mcimadamore@1882 | 60 | public class BridgeHarness { |
mcimadamore@1882 | 61 | |
mcimadamore@1882 | 62 | /** number of errors found (must be zero for the test to pass) */ |
mcimadamore@1882 | 63 | static int nerrors = 0; |
mcimadamore@1882 | 64 | |
mcimadamore@1882 | 65 | /** the (shared) Java compiler used for compiling the tests */ |
mcimadamore@1882 | 66 | static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@1882 | 67 | |
mcimadamore@1882 | 68 | /** the (shared) file manager used by the compiler */ |
mcimadamore@1882 | 69 | static final StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
mcimadamore@1882 | 70 | |
mcimadamore@1882 | 71 | public static void main(String[] args) throws Exception { |
mcimadamore@1882 | 72 | //set sourcepath |
mcimadamore@1882 | 73 | fm.setLocation(SOURCE_PATH, |
mcimadamore@1882 | 74 | Arrays.asList(new File(System.getProperty("test.src"), "tests"))); |
mcimadamore@1882 | 75 | //set output (-d) |
mcimadamore@1882 | 76 | fm.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, |
mcimadamore@1882 | 77 | Arrays.asList(new File(System.getProperty("user.dir")))); |
mcimadamore@1882 | 78 | for (JavaFileObject jfo : fm.list(SOURCE_PATH, "", Collections.singleton(JavaFileObject.Kind.SOURCE), true)) { |
mcimadamore@1882 | 79 | //for each source, compile and check against annotations |
mcimadamore@1882 | 80 | new BridgeHarness(jfo).compileAndCheck(); |
mcimadamore@1882 | 81 | } |
mcimadamore@1882 | 82 | //if there were errors, fail |
mcimadamore@1882 | 83 | if (nerrors > 0) { |
mcimadamore@1882 | 84 | throw new AssertionError("Errors were found"); |
mcimadamore@1882 | 85 | } |
mcimadamore@1882 | 86 | } |
mcimadamore@1882 | 87 | |
mcimadamore@1882 | 88 | /* utility methods */ |
mcimadamore@1882 | 89 | |
mcimadamore@1882 | 90 | /** |
mcimadamore@1882 | 91 | * Remove an element from a list |
mcimadamore@1882 | 92 | */ |
mcimadamore@1882 | 93 | static <Z> List<Z> drop(List<Z> lz, Z z) { |
mcimadamore@1882 | 94 | if (lz.head == z) { |
mcimadamore@1882 | 95 | return drop(lz.tail, z); |
mcimadamore@1882 | 96 | } else if (lz.isEmpty()) { |
mcimadamore@1882 | 97 | return lz; |
mcimadamore@1882 | 98 | } else { |
mcimadamore@1882 | 99 | return drop(lz.tail, z).prepend(lz.head); |
mcimadamore@1882 | 100 | } |
mcimadamore@1882 | 101 | } |
mcimadamore@1882 | 102 | |
mcimadamore@1882 | 103 | /** |
mcimadamore@1882 | 104 | * return a string representation of a bytecode method |
mcimadamore@1882 | 105 | */ |
mcimadamore@1882 | 106 | static String descriptor(Method m, ConstantPool cp) throws ConstantPoolException { |
mcimadamore@1882 | 107 | return m.getName(cp) + m.descriptor.getValue(cp); |
mcimadamore@1882 | 108 | } |
mcimadamore@1882 | 109 | |
mcimadamore@1882 | 110 | /* test harness */ |
mcimadamore@1882 | 111 | |
mcimadamore@1882 | 112 | /** Test file to be compiled */ |
mcimadamore@1882 | 113 | JavaFileObject jfo; |
mcimadamore@1882 | 114 | |
mcimadamore@1882 | 115 | /** Mapping between class name and list of bridges in class with that name */ |
mcimadamore@1882 | 116 | Map<String, List<Bridge>> bridgesMap = new HashMap<String, List<Bridge>>(); |
mcimadamore@1882 | 117 | |
mcimadamore@1882 | 118 | protected BridgeHarness(JavaFileObject jfo) { |
mcimadamore@1882 | 119 | this.jfo = jfo; |
mcimadamore@1882 | 120 | } |
mcimadamore@1882 | 121 | |
mcimadamore@1882 | 122 | /** |
mcimadamore@1882 | 123 | * Compile a test using a custom annotation processor and check the generated |
mcimadamore@1882 | 124 | * bytecode against discovered annotations. |
mcimadamore@1882 | 125 | */ |
mcimadamore@1882 | 126 | protected void compileAndCheck() throws Exception { |
mcimadamore@1882 | 127 | JavacTask ct = (JavacTask)comp.getTask(null, fm, null, null, null, Arrays.asList(jfo)); |
mcimadamore@1882 | 128 | ct.setProcessors(Collections.singleton(new BridgeFinder())); |
mcimadamore@1882 | 129 | |
mcimadamore@1882 | 130 | for (JavaFileObject jfo : ct.generate()) { |
mcimadamore@1882 | 131 | checkBridges(jfo); |
mcimadamore@1882 | 132 | } |
mcimadamore@1882 | 133 | } |
mcimadamore@1882 | 134 | |
mcimadamore@1882 | 135 | /** |
mcimadamore@1882 | 136 | * Check that every bridge in the generated classfile has a matching bridge |
mcimadamore@1882 | 137 | * annotation in the bridge map |
mcimadamore@1882 | 138 | */ |
mcimadamore@1882 | 139 | protected void checkBridges(JavaFileObject jfo) { |
mcimadamore@1882 | 140 | try { |
mcimadamore@1882 | 141 | ClassFile cf = ClassFile.read(jfo.openInputStream()); |
mcimadamore@1882 | 142 | System.err.println("checking: " + cf.getName()); |
mcimadamore@1882 | 143 | |
mcimadamore@1882 | 144 | List<Bridge> bridgeList = bridgesMap.get(cf.getName()); |
mcimadamore@1882 | 145 | if (bridgeList == null) { |
mcimadamore@1882 | 146 | //no bridges - nothing to check; |
mcimadamore@1882 | 147 | bridgeList = List.nil(); |
mcimadamore@1882 | 148 | } |
mcimadamore@1882 | 149 | |
mcimadamore@1882 | 150 | for (Method m : cf.methods) { |
mcimadamore@1882 | 151 | if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) { |
mcimadamore@1882 | 152 | //this is a bridge - see if there's a match in the bridge list |
mcimadamore@1882 | 153 | Bridge match = null; |
mcimadamore@1882 | 154 | for (Bridge b : bridgeList) { |
mcimadamore@1882 | 155 | if (b.value().equals(descriptor(m, cf.constant_pool))) { |
mcimadamore@1882 | 156 | match = b; |
mcimadamore@1882 | 157 | break; |
mcimadamore@1882 | 158 | } |
mcimadamore@1882 | 159 | } |
mcimadamore@1882 | 160 | if (match == null) { |
mcimadamore@1882 | 161 | error("No annotation for bridge method: " + descriptor(m, cf.constant_pool)); |
mcimadamore@1882 | 162 | } else { |
mcimadamore@1882 | 163 | bridgeList = drop(bridgeList, match); |
mcimadamore@1882 | 164 | } |
mcimadamore@1882 | 165 | } |
mcimadamore@1882 | 166 | } |
mcimadamore@1882 | 167 | if (bridgeList.nonEmpty()) { |
mcimadamore@1882 | 168 | error("Redundant bridge annotation found: " + bridgeList.head.value()); |
mcimadamore@1882 | 169 | } |
mcimadamore@1882 | 170 | } catch (Exception e) { |
mcimadamore@1882 | 171 | e.printStackTrace(); |
mcimadamore@1882 | 172 | throw new Error("error reading " + jfo.toUri() +": " + e); |
mcimadamore@1882 | 173 | } |
mcimadamore@1882 | 174 | } |
mcimadamore@1882 | 175 | |
mcimadamore@1882 | 176 | /** |
mcimadamore@1882 | 177 | * Log an error |
mcimadamore@1882 | 178 | */ |
mcimadamore@1882 | 179 | protected void error(String msg) { |
mcimadamore@1882 | 180 | nerrors++; |
mcimadamore@1882 | 181 | System.err.printf("Error occurred while checking file: %s\nreason: %s\n", jfo.getName(), msg); |
mcimadamore@1882 | 182 | } |
mcimadamore@1882 | 183 | |
mcimadamore@1882 | 184 | /** |
mcimadamore@1882 | 185 | * This annotation processor is used to populate the bridge map with the |
mcimadamore@1882 | 186 | * contents of the annotations that are found on the tests being compiled |
mcimadamore@1882 | 187 | */ |
mcimadamore@1882 | 188 | @SupportedAnnotationTypes({"Bridges","Bridge"}) |
mcimadamore@1882 | 189 | class BridgeFinder extends JavacTestingAbstractProcessor { |
mcimadamore@1882 | 190 | @Override |
mcimadamore@1882 | 191 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
mcimadamore@1882 | 192 | if (roundEnv.processingOver()) |
mcimadamore@1882 | 193 | return true; |
mcimadamore@1882 | 194 | |
mcimadamore@1882 | 195 | TypeElement bridgeAnno = elements.getTypeElement("Bridge"); |
mcimadamore@1882 | 196 | TypeElement bridgesAnno = elements.getTypeElement("Bridges"); |
mcimadamore@1882 | 197 | |
mcimadamore@1882 | 198 | //see if there are repeated annos |
mcimadamore@1882 | 199 | for (Element elem: roundEnv.getElementsAnnotatedWith(bridgesAnno)) { |
mcimadamore@1882 | 200 | List<Bridge> bridgeList = List.nil(); |
mcimadamore@1882 | 201 | Bridges bridges = elem.getAnnotation(Bridges.class); |
mcimadamore@1882 | 202 | for (Bridge bridge : bridges.value()) { |
mcimadamore@1882 | 203 | bridgeList = bridgeList.prepend(bridge); |
mcimadamore@1882 | 204 | } |
mcimadamore@1882 | 205 | bridgesMap.put(((ClassSymbol)elem).flatname.toString(), bridgeList); |
mcimadamore@1882 | 206 | } |
mcimadamore@1882 | 207 | |
mcimadamore@1882 | 208 | //see if there are non-repeated annos |
mcimadamore@1882 | 209 | for (Element elem: roundEnv.getElementsAnnotatedWith(bridgeAnno)) { |
mcimadamore@1882 | 210 | Bridge bridge = elem.getAnnotation(Bridge.class); |
mcimadamore@1882 | 211 | bridgesMap.put(((ClassSymbol)elem).flatname.toString(), |
mcimadamore@1882 | 212 | List.of(bridge)); |
mcimadamore@1882 | 213 | } |
mcimadamore@1882 | 214 | |
mcimadamore@1882 | 215 | return true; |
mcimadamore@1882 | 216 | } |
mcimadamore@1882 | 217 | } |
mcimadamore@1882 | 218 | } |