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