mcimadamore@1882: /* mcimadamore@1882: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. mcimadamore@1882: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. mcimadamore@1882: * mcimadamore@1882: * This code is free software; you can redistribute it and/or modify it mcimadamore@1882: * under the terms of the GNU General Public License version 2 only, as mcimadamore@1882: * published by the Free Software Foundation. mcimadamore@1882: * mcimadamore@1882: * This code is distributed in the hope that it will be useful, but WITHOUT mcimadamore@1882: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or mcimadamore@1882: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License mcimadamore@1882: * version 2 for more details (a copy is included in the LICENSE file that mcimadamore@1882: * accompanied this code). mcimadamore@1882: * mcimadamore@1882: * You should have received a copy of the GNU General Public License version mcimadamore@1882: * 2 along with this work; if not, write to the Free Software Foundation, mcimadamore@1882: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. mcimadamore@1882: * mcimadamore@1882: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA mcimadamore@1882: * or visit www.oracle.com if you need additional information or have any mcimadamore@1882: * questions. mcimadamore@1882: */ mcimadamore@1882: mcimadamore@1882: /* mcimadamore@1882: * @test mcimadamore@1882: * @bug 8013789 mcimadamore@1882: * @summary Compiler should emit bridges in interfaces mcimadamore@1882: * @library /tools/javac/lib mcimadamore@1882: * @build JavacTestingAbstractProcessor BridgeHarness mcimadamore@1882: * @run main BridgeHarness mcimadamore@1882: */ mcimadamore@1882: mcimadamore@1882: import com.sun.source.util.JavacTask; mcimadamore@1882: import com.sun.tools.classfile.AccessFlags; mcimadamore@1882: import com.sun.tools.classfile.ClassFile; mcimadamore@1882: import com.sun.tools.classfile.ConstantPool; mcimadamore@1882: import com.sun.tools.classfile.ConstantPoolException; mcimadamore@1882: import com.sun.tools.classfile.Method; mcimadamore@1882: import com.sun.tools.javac.code.Symbol.ClassSymbol; mcimadamore@1882: import com.sun.tools.javac.util.List; mcimadamore@1882: mcimadamore@1882: import java.io.File; mcimadamore@1883: import java.io.InputStream; mcimadamore@1882: import java.util.Arrays; mcimadamore@1882: import java.util.Collections; mcimadamore@1882: import java.util.HashMap; mcimadamore@1882: import java.util.Map; mcimadamore@1882: import java.util.Set; mcimadamore@1882: mcimadamore@1882: import javax.annotation.processing.RoundEnvironment; mcimadamore@1882: import javax.annotation.processing.SupportedAnnotationTypes; mcimadamore@1882: import javax.lang.model.element.Element; mcimadamore@1882: import javax.lang.model.element.TypeElement; mcimadamore@1882: import javax.tools.JavaCompiler; mcimadamore@1882: import javax.tools.JavaFileObject; mcimadamore@1882: import javax.tools.StandardJavaFileManager; mcimadamore@1882: import javax.tools.ToolProvider; mcimadamore@1882: mcimadamore@1882: import static javax.tools.StandardLocation.*; mcimadamore@1882: mcimadamore@1882: public class BridgeHarness { mcimadamore@1882: mcimadamore@1882: /** number of errors found (must be zero for the test to pass) */ mcimadamore@1882: static int nerrors = 0; mcimadamore@1882: mcimadamore@1882: /** the (shared) Java compiler used for compiling the tests */ mcimadamore@1882: static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); mcimadamore@1882: mcimadamore@1882: /** the (shared) file manager used by the compiler */ mcimadamore@1882: static final StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); mcimadamore@1882: mcimadamore@1882: public static void main(String[] args) throws Exception { mcimadamore@1882: //set sourcepath mcimadamore@1882: fm.setLocation(SOURCE_PATH, mcimadamore@1882: Arrays.asList(new File(System.getProperty("test.src"), "tests"))); mcimadamore@1882: //set output (-d) mcimadamore@1882: fm.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, mcimadamore@1882: Arrays.asList(new File(System.getProperty("user.dir")))); mcimadamore@1882: for (JavaFileObject jfo : fm.list(SOURCE_PATH, "", Collections.singleton(JavaFileObject.Kind.SOURCE), true)) { mcimadamore@1882: //for each source, compile and check against annotations mcimadamore@1882: new BridgeHarness(jfo).compileAndCheck(); mcimadamore@1882: } mcimadamore@1882: //if there were errors, fail mcimadamore@1882: if (nerrors > 0) { mcimadamore@1882: throw new AssertionError("Errors were found"); mcimadamore@1882: } mcimadamore@1882: } mcimadamore@1882: mcimadamore@1882: /* utility methods */ mcimadamore@1882: mcimadamore@1882: /** mcimadamore@1882: * Remove an element from a list mcimadamore@1882: */ mcimadamore@1882: static List drop(List lz, Z z) { mcimadamore@1882: if (lz.head == z) { mcimadamore@1882: return drop(lz.tail, z); mcimadamore@1882: } else if (lz.isEmpty()) { mcimadamore@1882: return lz; mcimadamore@1882: } else { mcimadamore@1882: return drop(lz.tail, z).prepend(lz.head); mcimadamore@1882: } mcimadamore@1882: } mcimadamore@1882: mcimadamore@1882: /** mcimadamore@1882: * return a string representation of a bytecode method mcimadamore@1882: */ mcimadamore@1882: static String descriptor(Method m, ConstantPool cp) throws ConstantPoolException { mcimadamore@1882: return m.getName(cp) + m.descriptor.getValue(cp); mcimadamore@1882: } mcimadamore@1882: mcimadamore@1882: /* test harness */ mcimadamore@1882: mcimadamore@1882: /** Test file to be compiled */ mcimadamore@1882: JavaFileObject jfo; mcimadamore@1882: mcimadamore@1882: /** Mapping between class name and list of bridges in class with that name */ mcimadamore@1882: Map> bridgesMap = new HashMap>(); mcimadamore@1882: mcimadamore@1882: protected BridgeHarness(JavaFileObject jfo) { mcimadamore@1882: this.jfo = jfo; mcimadamore@1882: } mcimadamore@1882: mcimadamore@1882: /** mcimadamore@1882: * Compile a test using a custom annotation processor and check the generated mcimadamore@1882: * bytecode against discovered annotations. mcimadamore@1882: */ mcimadamore@1882: protected void compileAndCheck() throws Exception { mcimadamore@1882: JavacTask ct = (JavacTask)comp.getTask(null, fm, null, null, null, Arrays.asList(jfo)); mcimadamore@1882: ct.setProcessors(Collections.singleton(new BridgeFinder())); mcimadamore@1882: mcimadamore@1882: for (JavaFileObject jfo : ct.generate()) { mcimadamore@1882: checkBridges(jfo); mcimadamore@1882: } mcimadamore@1882: } mcimadamore@1882: mcimadamore@1882: /** mcimadamore@1882: * Check that every bridge in the generated classfile has a matching bridge mcimadamore@1882: * annotation in the bridge map mcimadamore@1882: */ mcimadamore@1882: protected void checkBridges(JavaFileObject jfo) { mcimadamore@1883: try (InputStream is = jfo.openInputStream()) { mcimadamore@1883: ClassFile cf = ClassFile.read(is); mcimadamore@1882: System.err.println("checking: " + cf.getName()); mcimadamore@1882: mcimadamore@1882: List bridgeList = bridgesMap.get(cf.getName()); mcimadamore@1882: if (bridgeList == null) { mcimadamore@1882: //no bridges - nothing to check; mcimadamore@1882: bridgeList = List.nil(); mcimadamore@1882: } mcimadamore@1882: mcimadamore@1882: for (Method m : cf.methods) { mcimadamore@1882: if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) { mcimadamore@1882: //this is a bridge - see if there's a match in the bridge list mcimadamore@1882: Bridge match = null; mcimadamore@1882: for (Bridge b : bridgeList) { mcimadamore@1882: if (b.value().equals(descriptor(m, cf.constant_pool))) { mcimadamore@1882: match = b; mcimadamore@1882: break; mcimadamore@1882: } mcimadamore@1882: } mcimadamore@1882: if (match == null) { mcimadamore@1882: error("No annotation for bridge method: " + descriptor(m, cf.constant_pool)); mcimadamore@1882: } else { mcimadamore@1882: bridgeList = drop(bridgeList, match); mcimadamore@1882: } mcimadamore@1882: } mcimadamore@1882: } mcimadamore@1882: if (bridgeList.nonEmpty()) { mcimadamore@1882: error("Redundant bridge annotation found: " + bridgeList.head.value()); mcimadamore@1882: } mcimadamore@1882: } catch (Exception e) { mcimadamore@1882: e.printStackTrace(); mcimadamore@1882: throw new Error("error reading " + jfo.toUri() +": " + e); mcimadamore@1882: } mcimadamore@1882: } mcimadamore@1882: mcimadamore@1882: /** mcimadamore@1882: * Log an error mcimadamore@1882: */ mcimadamore@1882: protected void error(String msg) { mcimadamore@1882: nerrors++; mcimadamore@1882: System.err.printf("Error occurred while checking file: %s\nreason: %s\n", jfo.getName(), msg); mcimadamore@1882: } mcimadamore@1882: mcimadamore@1882: /** mcimadamore@1882: * This annotation processor is used to populate the bridge map with the mcimadamore@1882: * contents of the annotations that are found on the tests being compiled mcimadamore@1882: */ mcimadamore@1882: @SupportedAnnotationTypes({"Bridges","Bridge"}) mcimadamore@1882: class BridgeFinder extends JavacTestingAbstractProcessor { mcimadamore@1882: @Override mcimadamore@1882: public boolean process(Set annotations, RoundEnvironment roundEnv) { mcimadamore@1882: if (roundEnv.processingOver()) mcimadamore@1882: return true; mcimadamore@1882: mcimadamore@1882: TypeElement bridgeAnno = elements.getTypeElement("Bridge"); mcimadamore@1882: TypeElement bridgesAnno = elements.getTypeElement("Bridges"); mcimadamore@1882: mcimadamore@1882: //see if there are repeated annos mcimadamore@1882: for (Element elem: roundEnv.getElementsAnnotatedWith(bridgesAnno)) { mcimadamore@1882: List bridgeList = List.nil(); mcimadamore@1882: Bridges bridges = elem.getAnnotation(Bridges.class); mcimadamore@1882: for (Bridge bridge : bridges.value()) { mcimadamore@1882: bridgeList = bridgeList.prepend(bridge); mcimadamore@1882: } mcimadamore@1882: bridgesMap.put(((ClassSymbol)elem).flatname.toString(), bridgeList); mcimadamore@1882: } mcimadamore@1882: mcimadamore@1882: //see if there are non-repeated annos mcimadamore@1882: for (Element elem: roundEnv.getElementsAnnotatedWith(bridgeAnno)) { mcimadamore@1882: Bridge bridge = elem.getAnnotation(Bridge.class); mcimadamore@1882: bridgesMap.put(((ClassSymbol)elem).flatname.toString(), mcimadamore@1882: List.of(bridge)); mcimadamore@1882: } mcimadamore@1882: mcimadamore@1882: return true; mcimadamore@1882: } mcimadamore@1882: } mcimadamore@1882: }