Thu, 20 Mar 2014 10:42:00 -0700
Merge
mcimadamore@1336 | 1 | /* |
vromero@1482 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1336 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1336 | 4 | * |
mcimadamore@1336 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1336 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1336 | 7 | * published by the Free Software Foundation. |
mcimadamore@1336 | 8 | * |
mcimadamore@1336 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1336 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1336 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1336 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1336 | 13 | * accompanied this code). |
mcimadamore@1336 | 14 | * |
mcimadamore@1336 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1336 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1336 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1336 | 18 | * |
mcimadamore@1336 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1336 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1336 | 21 | * questions. |
mcimadamore@1336 | 22 | */ |
mcimadamore@1336 | 23 | |
mcimadamore@1336 | 24 | /* |
mcimadamore@1336 | 25 | * @test |
vromero@1774 | 26 | * @bug 7194586 8003280 8006694 8010404 |
mcimadamore@1415 | 27 | * @summary Add lambda tests |
mcimadamore@1415 | 28 | * Add back-end support for invokedynamic |
vromero@1520 | 29 | * temporarily workaround combo tests are causing time out in several platforms |
vromero@1482 | 30 | * @library ../lib |
vromero@1482 | 31 | * @build JavacTestingAbstractThreadedTest |
vromero@1520 | 32 | * @run main/othervm TestInvokeDynamic |
mcimadamore@1336 | 33 | */ |
mcimadamore@1336 | 34 | |
vromero@1520 | 35 | // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047) |
vromero@1520 | 36 | // see JDK-8006746 |
vromero@1520 | 37 | |
vromero@1774 | 38 | import java.io.File; |
vromero@1774 | 39 | import java.net.URI; |
vromero@1774 | 40 | import java.util.ArrayList; |
vromero@1774 | 41 | import java.util.Arrays; |
vromero@1774 | 42 | import java.util.Locale; |
vromero@1774 | 43 | |
vromero@1774 | 44 | import javax.tools.Diagnostic; |
vromero@1774 | 45 | import javax.tools.JavaFileObject; |
vromero@1774 | 46 | import javax.tools.SimpleJavaFileObject; |
vromero@1774 | 47 | |
mcimadamore@1336 | 48 | import com.sun.source.tree.MethodInvocationTree; |
mcimadamore@1336 | 49 | import com.sun.source.tree.MethodTree; |
mcimadamore@1336 | 50 | import com.sun.source.util.TaskEvent; |
mcimadamore@1336 | 51 | import com.sun.source.util.TaskListener; |
mcimadamore@1336 | 52 | import com.sun.source.util.TreeScanner; |
mcimadamore@1336 | 53 | |
mcimadamore@1336 | 54 | import com.sun.tools.classfile.Attribute; |
mcimadamore@1336 | 55 | import com.sun.tools.classfile.BootstrapMethods_attribute; |
mcimadamore@1336 | 56 | import com.sun.tools.classfile.ClassFile; |
mcimadamore@1336 | 57 | import com.sun.tools.classfile.Code_attribute; |
mcimadamore@1336 | 58 | import com.sun.tools.classfile.ConstantPool.*; |
mcimadamore@1336 | 59 | import com.sun.tools.classfile.Instruction; |
mcimadamore@1676 | 60 | import com.sun.tools.classfile.LineNumberTable_attribute; |
mcimadamore@1336 | 61 | import com.sun.tools.classfile.Method; |
mcimadamore@1336 | 62 | |
mcimadamore@1336 | 63 | import com.sun.tools.javac.api.JavacTaskImpl; |
mcimadamore@1336 | 64 | import com.sun.tools.javac.code.Symbol; |
mcimadamore@1336 | 65 | import com.sun.tools.javac.code.Symbol.MethodSymbol; |
mcimadamore@1336 | 66 | import com.sun.tools.javac.code.Symtab; |
vromero@1452 | 67 | import com.sun.tools.javac.code.Types; |
mcimadamore@1336 | 68 | import com.sun.tools.javac.jvm.Pool; |
mcimadamore@1336 | 69 | import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; |
mcimadamore@1336 | 70 | import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
mcimadamore@1336 | 71 | import com.sun.tools.javac.tree.JCTree.JCIdent; |
mcimadamore@1336 | 72 | import com.sun.tools.javac.util.Context; |
mcimadamore@1336 | 73 | import com.sun.tools.javac.util.Names; |
mcimadamore@1336 | 74 | |
mcimadamore@1336 | 75 | import static com.sun.tools.javac.jvm.ClassFile.*; |
mcimadamore@1336 | 76 | |
vromero@1482 | 77 | public class TestInvokeDynamic |
vromero@1482 | 78 | extends JavacTestingAbstractThreadedTest |
vromero@1482 | 79 | implements Runnable { |
mcimadamore@1336 | 80 | |
mcimadamore@1336 | 81 | enum StaticArgumentKind { |
mcimadamore@1336 | 82 | STRING("Hello!", "String", "Ljava/lang/String;") { |
mcimadamore@1336 | 83 | @Override |
mcimadamore@1336 | 84 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 85 | return (cpInfo instanceof CONSTANT_String_info) && |
vromero@1482 | 86 | ((CONSTANT_String_info)cpInfo).getString() |
vromero@1482 | 87 | .equals(value); |
mcimadamore@1336 | 88 | } |
mcimadamore@1336 | 89 | }, |
mcimadamore@1336 | 90 | CLASS(null, "Class<?>", "Ljava/lang/Class;") { |
mcimadamore@1336 | 91 | @Override |
mcimadamore@1336 | 92 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 93 | return (cpInfo instanceof CONSTANT_Class_info) && |
vromero@1482 | 94 | ((CONSTANT_Class_info)cpInfo).getName() |
vromero@1482 | 95 | .equals("java/lang/String"); |
mcimadamore@1336 | 96 | } |
mcimadamore@1336 | 97 | }, |
mcimadamore@1336 | 98 | INTEGER(1, "int", "I") { |
mcimadamore@1336 | 99 | @Override |
mcimadamore@1336 | 100 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 101 | return (cpInfo instanceof CONSTANT_Integer_info) && |
vromero@1482 | 102 | ((CONSTANT_Integer_info)cpInfo).value == |
vromero@1482 | 103 | ((Integer)value).intValue(); |
mcimadamore@1336 | 104 | } |
mcimadamore@1336 | 105 | }, |
mcimadamore@1336 | 106 | LONG(1L, "long", "J") { |
mcimadamore@1336 | 107 | @Override |
mcimadamore@1336 | 108 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 109 | return (cpInfo instanceof CONSTANT_Long_info) && |
vromero@1482 | 110 | ((CONSTANT_Long_info)cpInfo).value == |
vromero@1482 | 111 | ((Long)value).longValue(); |
mcimadamore@1336 | 112 | } |
mcimadamore@1336 | 113 | }, |
mcimadamore@1336 | 114 | FLOAT(1.0f, "float", "F") { |
mcimadamore@1336 | 115 | @Override |
mcimadamore@1336 | 116 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 117 | return (cpInfo instanceof CONSTANT_Float_info) && |
vromero@1482 | 118 | ((CONSTANT_Float_info)cpInfo).value == |
vromero@1482 | 119 | ((Float)value).floatValue(); |
mcimadamore@1336 | 120 | } |
mcimadamore@1336 | 121 | }, |
mcimadamore@1336 | 122 | DOUBLE(1.0, "double","D") { |
mcimadamore@1336 | 123 | @Override |
mcimadamore@1336 | 124 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 125 | return (cpInfo instanceof CONSTANT_Double_info) && |
vromero@1482 | 126 | ((CONSTANT_Double_info)cpInfo).value == |
vromero@1482 | 127 | ((Double)value).doubleValue(); |
mcimadamore@1336 | 128 | } |
mcimadamore@1336 | 129 | }, |
mcimadamore@1336 | 130 | METHOD_HANDLE(null, "MethodHandle", "Ljava/lang/invoke/MethodHandle;") { |
mcimadamore@1336 | 131 | @Override |
mcimadamore@1336 | 132 | boolean check(CPInfo cpInfo) throws Exception { |
vromero@1482 | 133 | if (!(cpInfo instanceof CONSTANT_MethodHandle_info)) |
vromero@1482 | 134 | return false; |
vromero@1482 | 135 | CONSTANT_MethodHandle_info handleInfo = |
vromero@1482 | 136 | (CONSTANT_MethodHandle_info)cpInfo; |
mcimadamore@1336 | 137 | return handleInfo.getCPRefInfo().getClassName().equals("Array") && |
mcimadamore@1336 | 138 | handleInfo.reference_kind == RefKind.REF_invokeVirtual && |
vromero@1482 | 139 | handleInfo.getCPRefInfo() |
vromero@1482 | 140 | .getNameAndTypeInfo().getName().equals("clone") && |
vromero@1482 | 141 | handleInfo.getCPRefInfo() |
vromero@1482 | 142 | .getNameAndTypeInfo().getType().equals("()Ljava/lang/Object;"); |
mcimadamore@1336 | 143 | } |
mcimadamore@1336 | 144 | }, |
mcimadamore@1336 | 145 | METHOD_TYPE(null, "MethodType", "Ljava/lang/invoke/MethodType;") { |
mcimadamore@1336 | 146 | @Override |
mcimadamore@1336 | 147 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 148 | return (cpInfo instanceof CONSTANT_MethodType_info) && |
vromero@1482 | 149 | ((CONSTANT_MethodType_info)cpInfo).getType() |
vromero@1482 | 150 | .equals("()Ljava/lang/Object;"); |
mcimadamore@1336 | 151 | } |
mcimadamore@1336 | 152 | }; |
mcimadamore@1336 | 153 | |
mcimadamore@1336 | 154 | Object value; |
mcimadamore@1336 | 155 | String sourceTypeStr; |
mcimadamore@1336 | 156 | String bytecodeTypeStr; |
mcimadamore@1336 | 157 | |
vromero@1482 | 158 | StaticArgumentKind(Object value, String sourceTypeStr, |
vromero@1482 | 159 | String bytecodeTypeStr) { |
mcimadamore@1336 | 160 | this.value = value; |
mcimadamore@1336 | 161 | this.sourceTypeStr = sourceTypeStr; |
mcimadamore@1336 | 162 | this.bytecodeTypeStr = bytecodeTypeStr; |
mcimadamore@1336 | 163 | } |
mcimadamore@1336 | 164 | |
mcimadamore@1336 | 165 | abstract boolean check(CPInfo cpInfo) throws Exception; |
mcimadamore@1336 | 166 | |
vromero@1452 | 167 | Object getValue(Symtab syms, Names names, Types types) { |
mcimadamore@1336 | 168 | switch (this) { |
mcimadamore@1336 | 169 | case STRING: |
mcimadamore@1336 | 170 | case INTEGER: |
mcimadamore@1336 | 171 | case LONG: |
mcimadamore@1336 | 172 | case FLOAT: |
mcimadamore@1336 | 173 | case DOUBLE: |
mcimadamore@1336 | 174 | return value; |
mcimadamore@1336 | 175 | case CLASS: |
mcimadamore@1336 | 176 | return syms.stringType.tsym; |
mcimadamore@1336 | 177 | case METHOD_HANDLE: |
vromero@1482 | 178 | return new Pool.MethodHandle(REF_invokeVirtual, |
vromero@1482 | 179 | syms.arrayCloneMethod, types); |
mcimadamore@1336 | 180 | case METHOD_TYPE: |
mcimadamore@1336 | 181 | return syms.arrayCloneMethod.type; |
mcimadamore@1336 | 182 | default: |
mcimadamore@1336 | 183 | throw new AssertionError(); |
mcimadamore@1336 | 184 | } |
mcimadamore@1336 | 185 | } |
mcimadamore@1336 | 186 | } |
mcimadamore@1336 | 187 | |
mcimadamore@1336 | 188 | enum StaticArgumentsArity { |
mcimadamore@1336 | 189 | ZERO(0), |
mcimadamore@1336 | 190 | ONE(1), |
mcimadamore@1336 | 191 | TWO(2), |
mcimadamore@1336 | 192 | THREE(3); |
mcimadamore@1336 | 193 | |
mcimadamore@1336 | 194 | int arity; |
mcimadamore@1336 | 195 | |
mcimadamore@1336 | 196 | StaticArgumentsArity(int arity) { |
mcimadamore@1336 | 197 | this.arity = arity; |
mcimadamore@1336 | 198 | } |
mcimadamore@1336 | 199 | } |
mcimadamore@1336 | 200 | |
mcimadamore@1336 | 201 | public static void main(String... args) throws Exception { |
mcimadamore@1336 | 202 | for (StaticArgumentsArity arity : StaticArgumentsArity.values()) { |
mcimadamore@1336 | 203 | if (arity.arity == 0) { |
vromero@1482 | 204 | pool.execute(new TestInvokeDynamic(arity)); |
mcimadamore@1336 | 205 | } else { |
mcimadamore@1336 | 206 | for (StaticArgumentKind sak1 : StaticArgumentKind.values()) { |
mcimadamore@1336 | 207 | if (arity.arity == 1) { |
vromero@1482 | 208 | pool.execute(new TestInvokeDynamic(arity, sak1)); |
mcimadamore@1336 | 209 | } else { |
mcimadamore@1336 | 210 | for (StaticArgumentKind sak2 : StaticArgumentKind.values()) { |
mcimadamore@1336 | 211 | if (arity.arity == 2) { |
vromero@1482 | 212 | pool.execute(new TestInvokeDynamic(arity, sak1, sak2)); |
mcimadamore@1336 | 213 | } else { |
mcimadamore@1336 | 214 | for (StaticArgumentKind sak3 : StaticArgumentKind.values()) { |
vromero@1482 | 215 | pool.execute( |
vromero@1482 | 216 | new TestInvokeDynamic(arity, sak1, sak2, sak3)); |
mcimadamore@1336 | 217 | } |
mcimadamore@1336 | 218 | } |
mcimadamore@1336 | 219 | } |
mcimadamore@1336 | 220 | } |
mcimadamore@1336 | 221 | } |
mcimadamore@1336 | 222 | } |
mcimadamore@1336 | 223 | } |
mcimadamore@1336 | 224 | |
vromero@1482 | 225 | checkAfterExec(); |
mcimadamore@1336 | 226 | } |
mcimadamore@1336 | 227 | |
mcimadamore@1336 | 228 | StaticArgumentsArity arity; |
mcimadamore@1336 | 229 | StaticArgumentKind[] saks; |
mcimadamore@1336 | 230 | DiagChecker dc; |
mcimadamore@1336 | 231 | |
mcimadamore@1336 | 232 | TestInvokeDynamic(StaticArgumentsArity arity, StaticArgumentKind... saks) { |
mcimadamore@1336 | 233 | this.arity = arity; |
mcimadamore@1336 | 234 | this.saks = saks; |
mcimadamore@1336 | 235 | dc = new DiagChecker(); |
mcimadamore@1336 | 236 | } |
mcimadamore@1336 | 237 | |
vromero@1482 | 238 | public void run() { |
vromero@1482 | 239 | int id = checkCount.incrementAndGet(); |
vromero@1482 | 240 | JavaSource source = new JavaSource(id); |
vromero@1482 | 241 | JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc, |
mcimadamore@1676 | 242 | Arrays.asList("-g"), null, Arrays.asList(source)); |
mcimadamore@1336 | 243 | Context context = ct.getContext(); |
mcimadamore@1336 | 244 | Symtab syms = Symtab.instance(context); |
mcimadamore@1336 | 245 | Names names = Names.instance(context); |
vromero@1452 | 246 | Types types = Types.instance(context); |
vromero@1452 | 247 | ct.addTaskListener(new Indifier(syms, names, types)); |
mcimadamore@1336 | 248 | try { |
mcimadamore@1336 | 249 | ct.generate(); |
mcimadamore@1336 | 250 | } catch (Throwable t) { |
mcimadamore@1336 | 251 | t.printStackTrace(); |
vromero@1482 | 252 | throw new AssertionError( |
vromero@1482 | 253 | String.format("Error thrown when compiling following code\n%s", |
vromero@1482 | 254 | source.source)); |
mcimadamore@1336 | 255 | } |
mcimadamore@1336 | 256 | if (dc.diagFound) { |
vromero@1482 | 257 | throw new AssertionError( |
vromero@1482 | 258 | String.format("Diags found when compiling following code\n%s\n\n%s", |
vromero@1482 | 259 | source.source, dc.printDiags())); |
mcimadamore@1336 | 260 | } |
vromero@1482 | 261 | verifyBytecode(id); |
mcimadamore@1336 | 262 | } |
mcimadamore@1336 | 263 | |
vromero@1482 | 264 | void verifyBytecode(int id) { |
vromero@1482 | 265 | File compiledTest = new File(String.format("Test%d.class", id)); |
mcimadamore@1336 | 266 | try { |
mcimadamore@1336 | 267 | ClassFile cf = ClassFile.read(compiledTest); |
mcimadamore@1336 | 268 | Method testMethod = null; |
mcimadamore@1336 | 269 | for (Method m : cf.methods) { |
mcimadamore@1336 | 270 | if (m.getName(cf.constant_pool).equals("test")) { |
mcimadamore@1336 | 271 | testMethod = m; |
mcimadamore@1336 | 272 | break; |
mcimadamore@1336 | 273 | } |
mcimadamore@1336 | 274 | } |
mcimadamore@1336 | 275 | if (testMethod == null) { |
mcimadamore@1336 | 276 | throw new Error("Test method not found"); |
mcimadamore@1336 | 277 | } |
vromero@1482 | 278 | Code_attribute ea = |
vromero@1482 | 279 | (Code_attribute)testMethod.attributes.get(Attribute.Code); |
mcimadamore@1336 | 280 | if (testMethod == null) { |
mcimadamore@1336 | 281 | throw new Error("Code attribute for test() method not found"); |
mcimadamore@1336 | 282 | } |
mcimadamore@1336 | 283 | |
mcimadamore@1336 | 284 | int bsmIdx = -1; |
mcimadamore@1336 | 285 | |
mcimadamore@1336 | 286 | for (Instruction i : ea.getInstructions()) { |
mcimadamore@1336 | 287 | if (i.getMnemonic().equals("invokedynamic")) { |
mcimadamore@1336 | 288 | CONSTANT_InvokeDynamic_info indyInfo = |
vromero@1482 | 289 | (CONSTANT_InvokeDynamic_info)cf |
vromero@1482 | 290 | .constant_pool.get(i.getShort(1)); |
mcimadamore@1336 | 291 | bsmIdx = indyInfo.bootstrap_method_attr_index; |
mcimadamore@1336 | 292 | if (!indyInfo.getNameAndTypeInfo().getType().equals("()V")) { |
vromero@1482 | 293 | throw new |
vromero@1482 | 294 | AssertionError("type mismatch for CONSTANT_InvokeDynamic_info"); |
mcimadamore@1336 | 295 | } |
mcimadamore@1336 | 296 | } |
mcimadamore@1336 | 297 | } |
mcimadamore@1336 | 298 | if (bsmIdx == -1) { |
mcimadamore@1336 | 299 | throw new Error("Missing invokedynamic in generated code"); |
mcimadamore@1336 | 300 | } |
mcimadamore@1336 | 301 | |
vromero@1482 | 302 | BootstrapMethods_attribute bsm_attr = |
vromero@1482 | 303 | (BootstrapMethods_attribute)cf |
vromero@1482 | 304 | .getAttribute(Attribute.BootstrapMethods); |
mcimadamore@1336 | 305 | if (bsm_attr.bootstrap_method_specifiers.length != 1) { |
vromero@1482 | 306 | throw new Error("Bad number of method specifiers " + |
vromero@1482 | 307 | "in BootstrapMethods attribute"); |
mcimadamore@1336 | 308 | } |
mcimadamore@1336 | 309 | BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec = |
mcimadamore@1336 | 310 | bsm_attr.bootstrap_method_specifiers[0]; |
mcimadamore@1336 | 311 | |
mcimadamore@1336 | 312 | if (bsm_spec.bootstrap_arguments.length != arity.arity) { |
vromero@1482 | 313 | throw new Error("Bad number of static invokedynamic args " + |
vromero@1482 | 314 | "in BootstrapMethod attribute"); |
mcimadamore@1336 | 315 | } |
mcimadamore@1336 | 316 | |
mcimadamore@1336 | 317 | int count = 0; |
mcimadamore@1336 | 318 | for (StaticArgumentKind sak : saks) { |
vromero@1482 | 319 | if (!sak.check(cf.constant_pool |
vromero@1482 | 320 | .get(bsm_spec.bootstrap_arguments[count]))) { |
mcimadamore@1336 | 321 | throw new Error("Bad static argument value " + sak); |
mcimadamore@1336 | 322 | } |
mcimadamore@1336 | 323 | count++; |
mcimadamore@1336 | 324 | } |
mcimadamore@1336 | 325 | |
mcimadamore@1336 | 326 | CONSTANT_MethodHandle_info bsm_handle = |
vromero@1482 | 327 | (CONSTANT_MethodHandle_info)cf.constant_pool |
vromero@1482 | 328 | .get(bsm_spec.bootstrap_method_ref); |
mcimadamore@1336 | 329 | |
mcimadamore@1336 | 330 | if (bsm_handle.reference_kind != RefKind.REF_invokeStatic) { |
mcimadamore@1336 | 331 | throw new Error("Bad kind on boostrap method handle"); |
mcimadamore@1336 | 332 | } |
mcimadamore@1336 | 333 | |
mcimadamore@1336 | 334 | CONSTANT_Methodref_info bsm_ref = |
vromero@1482 | 335 | (CONSTANT_Methodref_info)cf.constant_pool |
vromero@1482 | 336 | .get(bsm_handle.reference_index); |
mcimadamore@1336 | 337 | |
mcimadamore@1336 | 338 | if (!bsm_ref.getClassInfo().getName().equals("Bootstrap")) { |
mcimadamore@1336 | 339 | throw new Error("Bad owner of boostrap method"); |
mcimadamore@1336 | 340 | } |
mcimadamore@1336 | 341 | |
mcimadamore@1336 | 342 | if (!bsm_ref.getNameAndTypeInfo().getName().equals("bsm")) { |
mcimadamore@1336 | 343 | throw new Error("Bad boostrap method name"); |
mcimadamore@1336 | 344 | } |
mcimadamore@1336 | 345 | |
vromero@1482 | 346 | if (!bsm_ref.getNameAndTypeInfo() |
vromero@1482 | 347 | .getType().equals(asBSMSignatureString())) { |
vromero@1482 | 348 | throw new Error("Bad boostrap method type" + |
vromero@1482 | 349 | bsm_ref.getNameAndTypeInfo().getType() + " " + |
vromero@1482 | 350 | asBSMSignatureString()); |
mcimadamore@1336 | 351 | } |
mcimadamore@1676 | 352 | |
mcimadamore@1676 | 353 | LineNumberTable_attribute lnt = |
mcimadamore@1676 | 354 | (LineNumberTable_attribute)ea.attributes.get(Attribute.LineNumberTable); |
mcimadamore@1676 | 355 | |
mcimadamore@1676 | 356 | if (lnt == null) { |
mcimadamore@1676 | 357 | throw new Error("No LineNumberTable attribute"); |
mcimadamore@1676 | 358 | } |
jlahoda@2186 | 359 | if (lnt.line_number_table_length != 3) { |
mcimadamore@1676 | 360 | throw new Error("Wrong number of entries in LineNumberTable"); |
mcimadamore@1676 | 361 | } |
mcimadamore@1336 | 362 | } catch (Exception e) { |
mcimadamore@1336 | 363 | e.printStackTrace(); |
mcimadamore@1336 | 364 | throw new Error("error reading " + compiledTest +": " + e); |
mcimadamore@1336 | 365 | } |
mcimadamore@1336 | 366 | } |
mcimadamore@1336 | 367 | |
mcimadamore@1336 | 368 | String asBSMSignatureString() { |
mcimadamore@1336 | 369 | StringBuilder buf = new StringBuilder(); |
mcimadamore@1336 | 370 | buf.append("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;"); |
mcimadamore@1336 | 371 | for (StaticArgumentKind sak : saks) { |
mcimadamore@1336 | 372 | buf.append(sak.bytecodeTypeStr); |
mcimadamore@1336 | 373 | } |
mcimadamore@1336 | 374 | buf.append(")Ljava/lang/invoke/CallSite;"); |
mcimadamore@1336 | 375 | return buf.toString(); |
mcimadamore@1336 | 376 | } |
mcimadamore@1336 | 377 | |
mcimadamore@1336 | 378 | class JavaSource extends SimpleJavaFileObject { |
mcimadamore@1336 | 379 | |
mcimadamore@1336 | 380 | static final String source_template = "import java.lang.invoke.*;\n" + |
mcimadamore@1336 | 381 | "class Bootstrap {\n" + |
vromero@1482 | 382 | " public static CallSite bsm(MethodHandles.Lookup lookup, " + |
vromero@1482 | 383 | "String name, MethodType methodType #SARGS) {\n" + |
mcimadamore@1336 | 384 | " return null;\n" + |
mcimadamore@1336 | 385 | " }\n" + |
mcimadamore@1336 | 386 | "}\n" + |
vromero@1482 | 387 | "class Test#ID {\n" + |
mcimadamore@1336 | 388 | " void m() { }\n" + |
mcimadamore@1676 | 389 | " void test() {\n" + |
mcimadamore@1676 | 390 | " Object o = this; // marker statement \n" + |
mcimadamore@1676 | 391 | " m();\n" + |
mcimadamore@1676 | 392 | " }\n" + |
mcimadamore@1336 | 393 | "}"; |
mcimadamore@1336 | 394 | |
mcimadamore@1336 | 395 | String source; |
mcimadamore@1336 | 396 | |
vromero@1482 | 397 | JavaSource(int id) { |
mcimadamore@1336 | 398 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
vromero@1482 | 399 | source = source_template.replace("#SARGS", asSignatureString()) |
vromero@1482 | 400 | .replace("#ID", String.valueOf(id)); |
mcimadamore@1336 | 401 | } |
mcimadamore@1336 | 402 | |
mcimadamore@1336 | 403 | @Override |
mcimadamore@1336 | 404 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@1336 | 405 | return source; |
mcimadamore@1336 | 406 | } |
mcimadamore@1336 | 407 | |
mcimadamore@1336 | 408 | String asSignatureString() { |
mcimadamore@1336 | 409 | int count = 0; |
mcimadamore@1336 | 410 | StringBuilder buf = new StringBuilder(); |
mcimadamore@1336 | 411 | for (StaticArgumentKind sak : saks) { |
mcimadamore@1336 | 412 | buf.append(","); |
mcimadamore@1336 | 413 | buf.append(sak.sourceTypeStr); |
mcimadamore@1336 | 414 | buf.append(' '); |
mcimadamore@1336 | 415 | buf.append(String.format("x%d", count++)); |
mcimadamore@1336 | 416 | } |
mcimadamore@1336 | 417 | return buf.toString(); |
mcimadamore@1336 | 418 | } |
mcimadamore@1336 | 419 | } |
mcimadamore@1336 | 420 | |
mcimadamore@1336 | 421 | class Indifier extends TreeScanner<Void, Void> implements TaskListener { |
mcimadamore@1336 | 422 | |
mcimadamore@1336 | 423 | MethodSymbol bsm; |
mcimadamore@1336 | 424 | Symtab syms; |
mcimadamore@1336 | 425 | Names names; |
vromero@1452 | 426 | Types types; |
mcimadamore@1336 | 427 | |
vromero@1452 | 428 | Indifier(Symtab syms, Names names, Types types) { |
mcimadamore@1336 | 429 | this.syms = syms; |
mcimadamore@1336 | 430 | this.names = names; |
vromero@1452 | 431 | this.types = types; |
mcimadamore@1336 | 432 | } |
mcimadamore@1336 | 433 | |
mcimadamore@1336 | 434 | @Override |
mcimadamore@1336 | 435 | public void started(TaskEvent e) { |
mcimadamore@1336 | 436 | //do nothing |
mcimadamore@1336 | 437 | } |
mcimadamore@1336 | 438 | |
mcimadamore@1336 | 439 | @Override |
mcimadamore@1336 | 440 | public void finished(TaskEvent e) { |
mcimadamore@1336 | 441 | if (e.getKind() == TaskEvent.Kind.ANALYZE) { |
mcimadamore@1336 | 442 | scan(e.getCompilationUnit(), null); |
mcimadamore@1336 | 443 | } |
mcimadamore@1336 | 444 | } |
mcimadamore@1336 | 445 | |
mcimadamore@1336 | 446 | @Override |
mcimadamore@1336 | 447 | public Void visitMethodInvocation(MethodInvocationTree node, Void p) { |
mcimadamore@1336 | 448 | super.visitMethodInvocation(node, p); |
mcimadamore@1336 | 449 | JCMethodInvocation apply = (JCMethodInvocation)node; |
mcimadamore@1336 | 450 | JCIdent ident = (JCIdent)apply.meth; |
mcimadamore@1336 | 451 | Symbol oldSym = ident.sym; |
mcimadamore@1336 | 452 | if (!oldSym.isConstructor()) { |
mcimadamore@1336 | 453 | Object[] staticArgs = new Object[arity.arity]; |
mcimadamore@1336 | 454 | for (int i = 0; i < arity.arity ; i++) { |
vromero@1452 | 455 | staticArgs[i] = saks[i].getValue(syms, names, types); |
mcimadamore@1336 | 456 | } |
vromero@1482 | 457 | ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name, |
vromero@1482 | 458 | oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs); |
mcimadamore@1336 | 459 | } |
mcimadamore@1336 | 460 | return null; |
mcimadamore@1336 | 461 | } |
mcimadamore@1336 | 462 | |
mcimadamore@1336 | 463 | @Override |
mcimadamore@1336 | 464 | public Void visitMethod(MethodTree node, Void p) { |
mcimadamore@1336 | 465 | super.visitMethod(node, p); |
mcimadamore@1336 | 466 | if (node.getName().toString().equals("bsm")) { |
mcimadamore@1336 | 467 | bsm = ((JCMethodDecl)node).sym; |
mcimadamore@1336 | 468 | } |
mcimadamore@1336 | 469 | return null; |
mcimadamore@1336 | 470 | } |
mcimadamore@1336 | 471 | } |
mcimadamore@1336 | 472 | |
vromero@1482 | 473 | static class DiagChecker |
vromero@1482 | 474 | implements javax.tools.DiagnosticListener<JavaFileObject> { |
mcimadamore@1336 | 475 | |
mcimadamore@1336 | 476 | boolean diagFound; |
mcimadamore@1336 | 477 | ArrayList<String> diags = new ArrayList<>(); |
mcimadamore@1336 | 478 | |
mcimadamore@1336 | 479 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@1336 | 480 | diags.add(diagnostic.getMessage(Locale.getDefault())); |
mcimadamore@1336 | 481 | diagFound = true; |
mcimadamore@1336 | 482 | } |
mcimadamore@1336 | 483 | |
mcimadamore@1336 | 484 | String printDiags() { |
mcimadamore@1336 | 485 | StringBuilder buf = new StringBuilder(); |
mcimadamore@1336 | 486 | for (String s : diags) { |
mcimadamore@1336 | 487 | buf.append(s); |
mcimadamore@1336 | 488 | buf.append("\n"); |
mcimadamore@1336 | 489 | } |
mcimadamore@1336 | 490 | return buf.toString(); |
mcimadamore@1336 | 491 | } |
mcimadamore@1336 | 492 | } |
vromero@1482 | 493 | |
mcimadamore@1336 | 494 | } |