Fri, 14 Mar 2014 23:03:03 -0700
Merge
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@1883 | 43 | import java.io.InputStream; |
mcimadamore@1882 | 44 | import java.util.Arrays; |
mcimadamore@1882 | 45 | import java.util.Collections; |
mcimadamore@1882 | 46 | import java.util.HashMap; |
mcimadamore@1882 | 47 | import java.util.Map; |
mcimadamore@1882 | 48 | import java.util.Set; |
mcimadamore@1882 | 49 | |
mcimadamore@1882 | 50 | import javax.annotation.processing.RoundEnvironment; |
mcimadamore@1882 | 51 | import javax.annotation.processing.SupportedAnnotationTypes; |
mcimadamore@1882 | 52 | import javax.lang.model.element.Element; |
mcimadamore@1882 | 53 | import javax.lang.model.element.TypeElement; |
mcimadamore@1882 | 54 | import javax.tools.JavaCompiler; |
mcimadamore@1882 | 55 | import javax.tools.JavaFileObject; |
mcimadamore@1882 | 56 | import javax.tools.StandardJavaFileManager; |
mcimadamore@1882 | 57 | import javax.tools.ToolProvider; |
mcimadamore@1882 | 58 | |
mcimadamore@1882 | 59 | import static javax.tools.StandardLocation.*; |
mcimadamore@1882 | 60 | |
mcimadamore@1882 | 61 | public class BridgeHarness { |
mcimadamore@1882 | 62 | |
mcimadamore@1882 | 63 | /** number of errors found (must be zero for the test to pass) */ |
mcimadamore@1882 | 64 | static int nerrors = 0; |
mcimadamore@1882 | 65 | |
mcimadamore@1882 | 66 | /** the (shared) Java compiler used for compiling the tests */ |
mcimadamore@1882 | 67 | static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@1882 | 68 | |
mcimadamore@1882 | 69 | /** the (shared) file manager used by the compiler */ |
mcimadamore@1882 | 70 | static final StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
mcimadamore@1882 | 71 | |
mcimadamore@1882 | 72 | public static void main(String[] args) throws Exception { |
mcimadamore@1882 | 73 | //set sourcepath |
mcimadamore@1882 | 74 | fm.setLocation(SOURCE_PATH, |
mcimadamore@1882 | 75 | Arrays.asList(new File(System.getProperty("test.src"), "tests"))); |
mcimadamore@1882 | 76 | //set output (-d) |
mcimadamore@1882 | 77 | fm.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, |
mcimadamore@1882 | 78 | Arrays.asList(new File(System.getProperty("user.dir")))); |
mcimadamore@1882 | 79 | for (JavaFileObject jfo : fm.list(SOURCE_PATH, "", Collections.singleton(JavaFileObject.Kind.SOURCE), true)) { |
mcimadamore@1882 | 80 | //for each source, compile and check against annotations |
mcimadamore@1882 | 81 | new BridgeHarness(jfo).compileAndCheck(); |
mcimadamore@1882 | 82 | } |
mcimadamore@1882 | 83 | //if there were errors, fail |
mcimadamore@1882 | 84 | if (nerrors > 0) { |
mcimadamore@1882 | 85 | throw new AssertionError("Errors were found"); |
mcimadamore@1882 | 86 | } |
mcimadamore@1882 | 87 | } |
mcimadamore@1882 | 88 | |
mcimadamore@1882 | 89 | /* utility methods */ |
mcimadamore@1882 | 90 | |
mcimadamore@1882 | 91 | /** |
mcimadamore@1882 | 92 | * Remove an element from a list |
mcimadamore@1882 | 93 | */ |
mcimadamore@1882 | 94 | static <Z> List<Z> drop(List<Z> lz, Z z) { |
mcimadamore@1882 | 95 | if (lz.head == z) { |
mcimadamore@1882 | 96 | return drop(lz.tail, z); |
mcimadamore@1882 | 97 | } else if (lz.isEmpty()) { |
mcimadamore@1882 | 98 | return lz; |
mcimadamore@1882 | 99 | } else { |
mcimadamore@1882 | 100 | return drop(lz.tail, z).prepend(lz.head); |
mcimadamore@1882 | 101 | } |
mcimadamore@1882 | 102 | } |
mcimadamore@1882 | 103 | |
mcimadamore@1882 | 104 | /** |
mcimadamore@1882 | 105 | * return a string representation of a bytecode method |
mcimadamore@1882 | 106 | */ |
mcimadamore@1882 | 107 | static String descriptor(Method m, ConstantPool cp) throws ConstantPoolException { |
mcimadamore@1882 | 108 | return m.getName(cp) + m.descriptor.getValue(cp); |
mcimadamore@1882 | 109 | } |
mcimadamore@1882 | 110 | |
mcimadamore@1882 | 111 | /* test harness */ |
mcimadamore@1882 | 112 | |
mcimadamore@1882 | 113 | /** Test file to be compiled */ |
mcimadamore@1882 | 114 | JavaFileObject jfo; |
mcimadamore@1882 | 115 | |
mcimadamore@1882 | 116 | /** Mapping between class name and list of bridges in class with that name */ |
mcimadamore@1882 | 117 | Map<String, List<Bridge>> bridgesMap = new HashMap<String, List<Bridge>>(); |
mcimadamore@1882 | 118 | |
mcimadamore@1882 | 119 | protected BridgeHarness(JavaFileObject jfo) { |
mcimadamore@1882 | 120 | this.jfo = jfo; |
mcimadamore@1882 | 121 | } |
mcimadamore@1882 | 122 | |
mcimadamore@1882 | 123 | /** |
mcimadamore@1882 | 124 | * Compile a test using a custom annotation processor and check the generated |
mcimadamore@1882 | 125 | * bytecode against discovered annotations. |
mcimadamore@1882 | 126 | */ |
mcimadamore@1882 | 127 | protected void compileAndCheck() throws Exception { |
mcimadamore@1882 | 128 | JavacTask ct = (JavacTask)comp.getTask(null, fm, null, null, null, Arrays.asList(jfo)); |
mcimadamore@1882 | 129 | ct.setProcessors(Collections.singleton(new BridgeFinder())); |
mcimadamore@1882 | 130 | |
mcimadamore@1882 | 131 | for (JavaFileObject jfo : ct.generate()) { |
mcimadamore@1882 | 132 | checkBridges(jfo); |
mcimadamore@1882 | 133 | } |
mcimadamore@1882 | 134 | } |
mcimadamore@1882 | 135 | |
mcimadamore@1882 | 136 | /** |
mcimadamore@1882 | 137 | * Check that every bridge in the generated classfile has a matching bridge |
mcimadamore@1882 | 138 | * annotation in the bridge map |
mcimadamore@1882 | 139 | */ |
mcimadamore@1882 | 140 | protected void checkBridges(JavaFileObject jfo) { |
mcimadamore@1883 | 141 | try (InputStream is = jfo.openInputStream()) { |
mcimadamore@1883 | 142 | ClassFile cf = ClassFile.read(is); |
mcimadamore@1882 | 143 | System.err.println("checking: " + cf.getName()); |
mcimadamore@1882 | 144 | |
mcimadamore@1882 | 145 | List<Bridge> bridgeList = bridgesMap.get(cf.getName()); |
mcimadamore@1882 | 146 | if (bridgeList == null) { |
mcimadamore@1882 | 147 | //no bridges - nothing to check; |
mcimadamore@1882 | 148 | bridgeList = List.nil(); |
mcimadamore@1882 | 149 | } |
mcimadamore@1882 | 150 | |
mcimadamore@1882 | 151 | for (Method m : cf.methods) { |
mcimadamore@1882 | 152 | if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) { |
mcimadamore@1882 | 153 | //this is a bridge - see if there's a match in the bridge list |
mcimadamore@1882 | 154 | Bridge match = null; |
mcimadamore@1882 | 155 | for (Bridge b : bridgeList) { |
mcimadamore@1882 | 156 | if (b.value().equals(descriptor(m, cf.constant_pool))) { |
mcimadamore@1882 | 157 | match = b; |
mcimadamore@1882 | 158 | break; |
mcimadamore@1882 | 159 | } |
mcimadamore@1882 | 160 | } |
mcimadamore@1882 | 161 | if (match == null) { |
mcimadamore@1882 | 162 | error("No annotation for bridge method: " + descriptor(m, cf.constant_pool)); |
mcimadamore@1882 | 163 | } else { |
mcimadamore@1882 | 164 | bridgeList = drop(bridgeList, match); |
mcimadamore@1882 | 165 | } |
mcimadamore@1882 | 166 | } |
mcimadamore@1882 | 167 | } |
mcimadamore@1882 | 168 | if (bridgeList.nonEmpty()) { |
mcimadamore@1882 | 169 | error("Redundant bridge annotation found: " + bridgeList.head.value()); |
mcimadamore@1882 | 170 | } |
mcimadamore@1882 | 171 | } catch (Exception e) { |
mcimadamore@1882 | 172 | e.printStackTrace(); |
mcimadamore@1882 | 173 | throw new Error("error reading " + jfo.toUri() +": " + e); |
mcimadamore@1882 | 174 | } |
mcimadamore@1882 | 175 | } |
mcimadamore@1882 | 176 | |
mcimadamore@1882 | 177 | /** |
mcimadamore@1882 | 178 | * Log an error |
mcimadamore@1882 | 179 | */ |
mcimadamore@1882 | 180 | protected void error(String msg) { |
mcimadamore@1882 | 181 | nerrors++; |
mcimadamore@1882 | 182 | System.err.printf("Error occurred while checking file: %s\nreason: %s\n", jfo.getName(), msg); |
mcimadamore@1882 | 183 | } |
mcimadamore@1882 | 184 | |
mcimadamore@1882 | 185 | /** |
mcimadamore@1882 | 186 | * This annotation processor is used to populate the bridge map with the |
mcimadamore@1882 | 187 | * contents of the annotations that are found on the tests being compiled |
mcimadamore@1882 | 188 | */ |
mcimadamore@1882 | 189 | @SupportedAnnotationTypes({"Bridges","Bridge"}) |
mcimadamore@1882 | 190 | class BridgeFinder extends JavacTestingAbstractProcessor { |
mcimadamore@1882 | 191 | @Override |
mcimadamore@1882 | 192 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
mcimadamore@1882 | 193 | if (roundEnv.processingOver()) |
mcimadamore@1882 | 194 | return true; |
mcimadamore@1882 | 195 | |
mcimadamore@1882 | 196 | TypeElement bridgeAnno = elements.getTypeElement("Bridge"); |
mcimadamore@1882 | 197 | TypeElement bridgesAnno = elements.getTypeElement("Bridges"); |
mcimadamore@1882 | 198 | |
mcimadamore@1882 | 199 | //see if there are repeated annos |
mcimadamore@1882 | 200 | for (Element elem: roundEnv.getElementsAnnotatedWith(bridgesAnno)) { |
mcimadamore@1882 | 201 | List<Bridge> bridgeList = List.nil(); |
mcimadamore@1882 | 202 | Bridges bridges = elem.getAnnotation(Bridges.class); |
mcimadamore@1882 | 203 | for (Bridge bridge : bridges.value()) { |
mcimadamore@1882 | 204 | bridgeList = bridgeList.prepend(bridge); |
mcimadamore@1882 | 205 | } |
mcimadamore@1882 | 206 | bridgesMap.put(((ClassSymbol)elem).flatname.toString(), bridgeList); |
mcimadamore@1882 | 207 | } |
mcimadamore@1882 | 208 | |
mcimadamore@1882 | 209 | //see if there are non-repeated annos |
mcimadamore@1882 | 210 | for (Element elem: roundEnv.getElementsAnnotatedWith(bridgeAnno)) { |
mcimadamore@1882 | 211 | Bridge bridge = elem.getAnnotation(Bridge.class); |
mcimadamore@1882 | 212 | bridgesMap.put(((ClassSymbol)elem).flatname.toString(), |
mcimadamore@1882 | 213 | List.of(bridge)); |
mcimadamore@1882 | 214 | } |
mcimadamore@1882 | 215 | |
mcimadamore@1882 | 216 | return true; |
mcimadamore@1882 | 217 | } |
mcimadamore@1882 | 218 | } |
mcimadamore@1882 | 219 | } |