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