aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.HashMap; aoqi@0: import java.util.List; aoqi@0: import java.util.Map; aoqi@0: import java.util.StringJoiner; aoqi@0: aoqi@0: import org.testng.annotations.BeforeMethod; aoqi@0: import org.testng.annotations.Test; aoqi@0: aoqi@0: import tools.javac.combo.*; aoqi@0: aoqi@0: import static org.testng.Assert.fail; aoqi@0: aoqi@0: /** aoqi@0: * BridgeMethodTestCase -- used for asserting linkage to bridges under separate compilation. aoqi@0: * aoqi@0: * Example test case: aoqi@0: * public void test1() throws IOException, ReflectiveOperationException { aoqi@0: * compileSpec("C(Bc1(A))"); aoqi@0: * assertLinkage("C", LINKAGE_ERROR, "B1"); aoqi@0: * recompileSpec("C(Bc1(Ac0))", "A"); aoqi@0: * assertLinkage("C", "A0", "B1"); aoqi@0: * } aoqi@0: * aoqi@0: * This compiles A, B, and C, asserts that C.m()Object does not exist, asserts aoqi@0: * that C.m()Number eventually invokes B.m()Number, recompiles B, and then asserts aoqi@0: * that the result of calling C.m()Object now arrives at A. aoqi@0: * aoqi@0: * @author Brian Goetz aoqi@0: */ aoqi@0: aoqi@0: @Test aoqi@0: public abstract class BridgeMethodTestCase extends JavacTemplateTestBase { aoqi@0: aoqi@0: private static final String TYPE_LETTERS = "ABCDIJK"; aoqi@0: aoqi@0: private static final String BASE_INDEX_CLASS = "class C0 {\n" + aoqi@0: " int val;\n" + aoqi@0: " C0(int val) { this.val = val; }\n" + aoqi@0: " public int getVal() { return val; }\n" + aoqi@0: "}\n"; aoqi@0: private static final String INDEX_CLASS_TEMPLATE = "class C#ID extends C#PREV {\n" + aoqi@0: " C#ID(int val) { super(val); }\n" + aoqi@0: "}\n"; aoqi@0: aoqi@0: aoqi@0: aoqi@0: protected static String LINKAGE_ERROR = "-1"; aoqi@0: aoqi@0: private List compileDirs = new ArrayList<>(); aoqi@0: aoqi@0: /** aoqi@0: * Compile all the classes in a class spec, and put them on the classpath. aoqi@0: * aoqi@0: * The spec is the specification for a nest of classes, using the following notation aoqi@0: * A, B represent abstract classes aoqi@0: * C represents a concrete class aoqi@0: * I, J, K represent interfaces aoqi@0: * Lowercase 'c' following a class means that the method m() is concrete aoqi@0: * Lowercase 'a' following a class or interface means that the method m() is abstract aoqi@0: * Lowercase 'd' following an interface means that the method m() is default aoqi@0: * A number 0, 1, or 2 following the lowercase letter indicates the return type of that method aoqi@0: * 0 = Object, 1 = Number, 2 = Integer (these form an inheritance chain so bridges are generated) aoqi@0: * A classes supertypes follow its method spec, in parentheses aoqi@0: * Examples: aoqi@0: * C(Ia0, Jd0) -- C extends I and J, I has abstract m()Object, J has default m()Object aoqi@0: * Cc1(Ia0) -- C has concrete m()Number, extends I with abstract m()Object aoqi@0: * If a type must appear multiple times, its full spec must be in the first occurrence aoqi@0: * Example: aoqi@0: * C(I(Kd0), J(K)) aoqi@0: */ aoqi@0: protected void compileSpec(String spec) throws IOException { aoqi@0: compileSpec(spec, false); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Compile all the classes in a class spec, and assert that there were compilation errors. aoqi@0: */ aoqi@0: protected void compileSpec(String spec, String... errorKeys) throws IOException { aoqi@0: compileSpec(spec, false, errorKeys); aoqi@0: } aoqi@0: aoqi@0: protected void compileSpec(String spec, boolean debug, String... errorKeys) throws IOException { aoqi@0: ClassModel cm = new Parser(spec).parseClassModel(); aoqi@0: for (int i = 0; i <= cm.maxIndex() ; i++) { aoqi@0: if (debug) System.out.println(indexClass(i)); aoqi@0: addSourceFile(String.format("C%d.java", i), new StringTemplate(indexClass(i))); aoqi@0: } aoqi@0: for (Map.Entry e : classes(cm).entrySet()) { aoqi@0: if (debug) System.out.println(e.getValue().toSource()); aoqi@0: addSourceFile(e.getKey() + ".java", new StringTemplate(e.getValue().toSource())); aoqi@0: } aoqi@0: compileDirs.add(compile(true)); aoqi@0: resetSourceFiles(); aoqi@0: if (errorKeys.length == 0) aoqi@0: assertCompileSucceeded(); aoqi@0: else aoqi@0: assertCompileErrors(errorKeys); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Recompile only a subset of classes in the class spec, as named by names, aoqi@0: * and put them on the classpath such that they shadow earlier versions of that class. aoqi@0: */ aoqi@0: protected void recompileSpec(String spec, String... names) throws IOException { aoqi@0: List nameList = Arrays.asList(names); aoqi@0: ClassModel cm = new Parser(spec).parseClassModel(); aoqi@0: for (int i = 0; i <= cm.maxIndex() ; i++) { aoqi@0: addSourceFile(String.format("C%d.java", i), new StringTemplate(indexClass(i))); aoqi@0: } aoqi@0: for (Map.Entry e : classes(cm).entrySet()) aoqi@0: if (nameList.contains(e.getKey())) aoqi@0: addSourceFile(e.getKey() + ".java", new StringTemplate(e.getValue().toSource())); aoqi@0: compileDirs.add(compile(Arrays.asList(classPaths()), true)); aoqi@0: resetSourceFiles(); aoqi@0: assertCompileSucceeded(); aoqi@0: } aoqi@0: aoqi@0: protected void assertLinkage(String name, String... expected) throws ReflectiveOperationException { aoqi@0: for (int i=0; i classes(ClassModel cm) { aoqi@0: HashMap m = new HashMap<>(); aoqi@0: classesHelper(cm, m); aoqi@0: return m; aoqi@0: } aoqi@0: aoqi@0: private String indexClass(int index) { aoqi@0: if (index == 0) { aoqi@0: return BASE_INDEX_CLASS; aoqi@0: } else { aoqi@0: return INDEX_CLASS_TEMPLATE aoqi@0: .replace("#ID", String.valueOf(index)) aoqi@0: .replace("#PREV", String.valueOf(index - 1)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static String overrideName(int index) { aoqi@0: return "C" + index; aoqi@0: } aoqi@0: aoqi@0: private void classesHelper(ClassModel cm, Map m) { aoqi@0: if (!m.containsKey(cm.name)) aoqi@0: m.put(cm.name, cm); aoqi@0: for (ClassModel s : cm.supertypes) aoqi@0: classesHelper(s, m); aoqi@0: } aoqi@0: aoqi@0: private static String fromNum(int num) { aoqi@0: return String.format("%c%d", TYPE_LETTERS.charAt(num / 10), num % 10); aoqi@0: } aoqi@0: aoqi@0: private static int toNum(String name, int index) { aoqi@0: return 10*(TYPE_LETTERS.indexOf(name.charAt(0))) + index; aoqi@0: } aoqi@0: aoqi@0: private static int toNum(String string) { aoqi@0: return 10*(TYPE_LETTERS.indexOf(string.charAt(0))) + Integer.parseInt(string.substring(1, 2)); aoqi@0: } aoqi@0: aoqi@0: private int invoke(String name, int index) throws ReflectiveOperationException { aoqi@0: File[] files = classPaths(); aoqi@0: Class clazz = loadClass(name, files); aoqi@0: Method[] ms = clazz.getMethods(); aoqi@0: for (Method m : ms) { aoqi@0: if (m.getName().equals("m") && m.getReturnType().getName().equals(overrideName(index))) { aoqi@0: m.setAccessible(true); aoqi@0: Object instance = clazz.newInstance(); aoqi@0: Object c0 = m.invoke(instance); aoqi@0: Method getVal = c0.getClass().getMethod("getVal"); aoqi@0: getVal.setAccessible(true); aoqi@0: return (int)getVal.invoke(c0); aoqi@0: } aoqi@0: } aoqi@0: throw new NoSuchMethodError("cannot find method m()" + index + " in class " + name); aoqi@0: } aoqi@0: aoqi@0: private File[] classPaths() { aoqi@0: File[] files = new File[compileDirs.size()]; aoqi@0: for (int i=0; i supertypes; aoqi@0: private final MethodType methodType; aoqi@0: private final int methodIndex; aoqi@0: aoqi@0: private ClassModel(String name, aoqi@0: boolean anInterface, aoqi@0: List supertypes, aoqi@0: MethodType methodType, aoqi@0: int methodIndex) { aoqi@0: this.name = name; aoqi@0: isInterface = anInterface; aoqi@0: this.supertypes = supertypes; aoqi@0: this.methodType = methodType; aoqi@0: this.methodIndex = methodIndex; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public String toString() { aoqi@0: StringBuilder sb = new StringBuilder(); aoqi@0: sb.append(name); aoqi@0: if (methodType != null) { aoqi@0: sb.append(methodType.designator); aoqi@0: sb.append(methodIndex); aoqi@0: } aoqi@0: if (!supertypes.isEmpty()) { aoqi@0: sb.append("("); aoqi@0: for (int i=0; i 0) aoqi@0: sb.append(","); aoqi@0: sb.append(supertypes.get(i).toString()); aoqi@0: } aoqi@0: sb.append(")"); aoqi@0: } aoqi@0: return sb.toString(); aoqi@0: } aoqi@0: aoqi@0: int maxIndex() { aoqi@0: int maxSoFar = methodIndex; aoqi@0: for (ClassModel cm : supertypes) { aoqi@0: maxSoFar = Math.max(cm.maxIndex(), maxSoFar); aoqi@0: } aoqi@0: return maxSoFar; aoqi@0: } aoqi@0: aoqi@0: public String toSource() { aoqi@0: String extendsClause = ""; aoqi@0: String implementsClause = ""; aoqi@0: String methodBody = ""; aoqi@0: boolean isAbstract = "AB".contains(name); aoqi@0: aoqi@0: for (ClassModel s : supertypes) { aoqi@0: if (!s.isInterface) { aoqi@0: extendsClause = String.format("extends %s", s.name); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: StringJoiner sj = new StringJoiner(", "); aoqi@0: for (ClassModel s : supertypes) aoqi@0: if (s.isInterface) aoqi@0: sj.add(s.name); aoqi@0: if (sj.length() > 0) { aoqi@0: if (isInterface) aoqi@0: implementsClause = "extends " + sj.toString(); aoqi@0: else aoqi@0: implementsClause = "implements " + sj.toString(); aoqi@0: } aoqi@0: if (methodType != null) { aoqi@0: switch (methodType) { aoqi@0: case ABSTRACT: aoqi@0: methodBody = String.format("public abstract %s m();", overrideName(methodIndex)); aoqi@0: break; aoqi@0: case CONCRETE: aoqi@0: methodBody = String.format("public %s m() { return new %s(%d); };", aoqi@0: overrideName(methodIndex), overrideName(methodIndex), toNum(name, methodIndex)); aoqi@0: break; aoqi@0: case DEFAULT: aoqi@0: methodBody = String.format("public default %s m() { return new %s(%d); };", aoqi@0: overrideName(methodIndex), overrideName(methodIndex), toNum(name, methodIndex)); aoqi@0: break; aoqi@0: aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return String.format("public %s %s %s %s %s { %s }", isAbstract ? "abstract" : "", aoqi@0: isInterface ? "interface" : "class", aoqi@0: name, extendsClause, implementsClause, methodBody); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static class Parser { aoqi@0: private final String input; aoqi@0: private final char[] chars; aoqi@0: private int index; aoqi@0: aoqi@0: private Parser(String s) { aoqi@0: input = s; aoqi@0: chars = s.toCharArray(); aoqi@0: } aoqi@0: aoqi@0: private char peek() { aoqi@0: return index < chars.length ? chars[index] : 0; aoqi@0: } aoqi@0: aoqi@0: private boolean peek(String validChars) { aoqi@0: return validChars.indexOf(peek()) >= 0; aoqi@0: } aoqi@0: aoqi@0: private char advanceIf(String validChars) { aoqi@0: if (peek(validChars)) aoqi@0: return chars[index++]; aoqi@0: else aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: private char advanceIfDigit() { aoqi@0: return advanceIf("0123456789"); aoqi@0: } aoqi@0: aoqi@0: private int index() { aoqi@0: StringBuilder buf = new StringBuilder(); aoqi@0: char c = advanceIfDigit(); aoqi@0: while (c != 0) { aoqi@0: buf.append(c); aoqi@0: c = advanceIfDigit(); aoqi@0: } aoqi@0: return Integer.valueOf(buf.toString()); aoqi@0: } aoqi@0: aoqi@0: private char advance() { aoqi@0: return chars[index++]; aoqi@0: } aoqi@0: aoqi@0: private char expect(String validChars) { aoqi@0: char c = advanceIf(validChars); aoqi@0: if (c == 0) aoqi@0: throw new IllegalArgumentException(String.format("Expecting %s at position %d of %s", validChars, index, input)); aoqi@0: return c; aoqi@0: } aoqi@0: aoqi@0: public ClassModel parseClassModel() { aoqi@0: List supers = new ArrayList<>(); aoqi@0: char name = expect(TYPE_LETTERS); aoqi@0: boolean isInterface = "IJK".indexOf(name) >= 0; aoqi@0: ClassModel.MethodType methodType = peek(isInterface ? "ad" : "ac") ? ClassModel.MethodType.find(advance()) : null; aoqi@0: int methodIndex = 0; aoqi@0: if (methodType != null) { aoqi@0: methodIndex = index(); aoqi@0: } aoqi@0: if (peek() == '(') { aoqi@0: advance(); aoqi@0: supers.add(parseClassModel()); aoqi@0: while (peek() == ',') { aoqi@0: advance(); aoqi@0: supers.add(parseClassModel()); aoqi@0: } aoqi@0: expect(")"); aoqi@0: } aoqi@0: return new ClassModel(new String(new char[]{ name }), isInterface, supers, methodType, methodIndex); aoqi@0: } aoqi@0: } aoqi@0: }