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