Tue, 25 Sep 2012 11:53:18 +0100
7194586: Add back-end support for invokedynamic
Summary: Add support for invokedynamic bytecode instruction; includes suppot for generation of all related classfile attributes
Reviewed-by: jjg
mcimadamore@1336 | 1 | /* |
mcimadamore@1336 | 2 | * Copyright (c) 2012, 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 |
mcimadamore@1336 | 26 | * @bug 7194586 |
mcimadamore@1336 | 27 | * |
mcimadamore@1336 | 28 | * @summary Add back-end support for invokedynamic |
mcimadamore@1336 | 29 | * |
mcimadamore@1336 | 30 | */ |
mcimadamore@1336 | 31 | |
mcimadamore@1336 | 32 | import com.sun.source.tree.MethodInvocationTree; |
mcimadamore@1336 | 33 | import com.sun.source.tree.MethodTree; |
mcimadamore@1336 | 34 | import com.sun.source.util.TaskEvent; |
mcimadamore@1336 | 35 | import com.sun.source.util.TaskListener; |
mcimadamore@1336 | 36 | import com.sun.source.util.TreeScanner; |
mcimadamore@1336 | 37 | |
mcimadamore@1336 | 38 | import com.sun.tools.classfile.Attribute; |
mcimadamore@1336 | 39 | import com.sun.tools.classfile.BootstrapMethods_attribute; |
mcimadamore@1336 | 40 | import com.sun.tools.classfile.ClassFile; |
mcimadamore@1336 | 41 | import com.sun.tools.classfile.Code_attribute; |
mcimadamore@1336 | 42 | import com.sun.tools.classfile.ConstantPool.*; |
mcimadamore@1336 | 43 | import com.sun.tools.classfile.Instruction; |
mcimadamore@1336 | 44 | import com.sun.tools.classfile.Method; |
mcimadamore@1336 | 45 | |
mcimadamore@1336 | 46 | import com.sun.tools.javac.api.JavacTaskImpl; |
mcimadamore@1336 | 47 | import com.sun.tools.javac.api.JavacTool; |
mcimadamore@1336 | 48 | import com.sun.tools.javac.code.Symbol; |
mcimadamore@1336 | 49 | import com.sun.tools.javac.code.Symbol.MethodSymbol; |
mcimadamore@1336 | 50 | import com.sun.tools.javac.code.Symtab; |
mcimadamore@1336 | 51 | import com.sun.tools.javac.jvm.Pool; |
mcimadamore@1336 | 52 | import com.sun.tools.javac.tree.JCTree.JCMethodInvocation; |
mcimadamore@1336 | 53 | import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
mcimadamore@1336 | 54 | import com.sun.tools.javac.tree.JCTree.JCIdent; |
mcimadamore@1336 | 55 | import com.sun.tools.javac.util.Context; |
mcimadamore@1336 | 56 | import com.sun.tools.javac.util.Names; |
mcimadamore@1336 | 57 | |
mcimadamore@1336 | 58 | import java.io.File; |
mcimadamore@1336 | 59 | import java.net.URI; |
mcimadamore@1336 | 60 | import java.util.ArrayList; |
mcimadamore@1336 | 61 | import java.util.Arrays; |
mcimadamore@1336 | 62 | import java.util.Locale; |
mcimadamore@1336 | 63 | |
mcimadamore@1336 | 64 | import javax.tools.Diagnostic; |
mcimadamore@1336 | 65 | import javax.tools.JavaCompiler; |
mcimadamore@1336 | 66 | import javax.tools.JavaFileManager; |
mcimadamore@1336 | 67 | import javax.tools.JavaFileObject; |
mcimadamore@1336 | 68 | import javax.tools.SimpleJavaFileObject; |
mcimadamore@1336 | 69 | import javax.tools.StandardJavaFileManager; |
mcimadamore@1336 | 70 | import javax.tools.ToolProvider; |
mcimadamore@1336 | 71 | |
mcimadamore@1336 | 72 | import static com.sun.tools.javac.jvm.ClassFile.*; |
mcimadamore@1336 | 73 | |
mcimadamore@1336 | 74 | public class TestInvokeDynamic { |
mcimadamore@1336 | 75 | |
mcimadamore@1336 | 76 | static int checkCount = 0; |
mcimadamore@1336 | 77 | |
mcimadamore@1336 | 78 | enum StaticArgumentKind { |
mcimadamore@1336 | 79 | STRING("Hello!", "String", "Ljava/lang/String;") { |
mcimadamore@1336 | 80 | @Override |
mcimadamore@1336 | 81 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 82 | return (cpInfo instanceof CONSTANT_String_info) && |
mcimadamore@1336 | 83 | ((CONSTANT_String_info)cpInfo).getString().equals(value); |
mcimadamore@1336 | 84 | } |
mcimadamore@1336 | 85 | }, |
mcimadamore@1336 | 86 | CLASS(null, "Class<?>", "Ljava/lang/Class;") { |
mcimadamore@1336 | 87 | @Override |
mcimadamore@1336 | 88 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 89 | return (cpInfo instanceof CONSTANT_Class_info) && |
mcimadamore@1336 | 90 | ((CONSTANT_Class_info)cpInfo).getName().equals("java/lang/String"); |
mcimadamore@1336 | 91 | } |
mcimadamore@1336 | 92 | }, |
mcimadamore@1336 | 93 | INTEGER(1, "int", "I") { |
mcimadamore@1336 | 94 | @Override |
mcimadamore@1336 | 95 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 96 | return (cpInfo instanceof CONSTANT_Integer_info) && |
mcimadamore@1336 | 97 | ((CONSTANT_Integer_info)cpInfo).value == ((Integer)value).intValue(); |
mcimadamore@1336 | 98 | } |
mcimadamore@1336 | 99 | }, |
mcimadamore@1336 | 100 | LONG(1L, "long", "J") { |
mcimadamore@1336 | 101 | @Override |
mcimadamore@1336 | 102 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 103 | return (cpInfo instanceof CONSTANT_Long_info) && |
mcimadamore@1336 | 104 | ((CONSTANT_Long_info)cpInfo).value == ((Long)value).longValue(); |
mcimadamore@1336 | 105 | } |
mcimadamore@1336 | 106 | }, |
mcimadamore@1336 | 107 | FLOAT(1.0f, "float", "F") { |
mcimadamore@1336 | 108 | @Override |
mcimadamore@1336 | 109 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 110 | return (cpInfo instanceof CONSTANT_Float_info) && |
mcimadamore@1336 | 111 | ((CONSTANT_Float_info)cpInfo).value == ((Float)value).floatValue(); |
mcimadamore@1336 | 112 | } |
mcimadamore@1336 | 113 | }, |
mcimadamore@1336 | 114 | DOUBLE(1.0, "double","D") { |
mcimadamore@1336 | 115 | @Override |
mcimadamore@1336 | 116 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 117 | return (cpInfo instanceof CONSTANT_Double_info) && |
mcimadamore@1336 | 118 | ((CONSTANT_Double_info)cpInfo).value == ((Double)value).doubleValue(); |
mcimadamore@1336 | 119 | } |
mcimadamore@1336 | 120 | }, |
mcimadamore@1336 | 121 | METHOD_HANDLE(null, "MethodHandle", "Ljava/lang/invoke/MethodHandle;") { |
mcimadamore@1336 | 122 | @Override |
mcimadamore@1336 | 123 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 124 | if (!(cpInfo instanceof CONSTANT_MethodHandle_info)) return false; |
mcimadamore@1336 | 125 | CONSTANT_MethodHandle_info handleInfo = (CONSTANT_MethodHandle_info)cpInfo; |
mcimadamore@1336 | 126 | return handleInfo.getCPRefInfo().getClassName().equals("Array") && |
mcimadamore@1336 | 127 | handleInfo.reference_kind == RefKind.REF_invokeVirtual && |
mcimadamore@1336 | 128 | handleInfo.getCPRefInfo().getNameAndTypeInfo().getName().equals("clone") && |
mcimadamore@1336 | 129 | handleInfo.getCPRefInfo().getNameAndTypeInfo().getType().equals("()Ljava/lang/Object;"); |
mcimadamore@1336 | 130 | } |
mcimadamore@1336 | 131 | }, |
mcimadamore@1336 | 132 | METHOD_TYPE(null, "MethodType", "Ljava/lang/invoke/MethodType;") { |
mcimadamore@1336 | 133 | @Override |
mcimadamore@1336 | 134 | boolean check(CPInfo cpInfo) throws Exception { |
mcimadamore@1336 | 135 | return (cpInfo instanceof CONSTANT_MethodType_info) && |
mcimadamore@1336 | 136 | ((CONSTANT_MethodType_info)cpInfo).getType().equals("()Ljava/lang/Object;"); |
mcimadamore@1336 | 137 | } |
mcimadamore@1336 | 138 | }; |
mcimadamore@1336 | 139 | |
mcimadamore@1336 | 140 | Object value; |
mcimadamore@1336 | 141 | String sourceTypeStr; |
mcimadamore@1336 | 142 | String bytecodeTypeStr; |
mcimadamore@1336 | 143 | |
mcimadamore@1336 | 144 | StaticArgumentKind(Object value, String sourceTypeStr, String bytecodeTypeStr) { |
mcimadamore@1336 | 145 | this.value = value; |
mcimadamore@1336 | 146 | this.sourceTypeStr = sourceTypeStr; |
mcimadamore@1336 | 147 | this.bytecodeTypeStr = bytecodeTypeStr; |
mcimadamore@1336 | 148 | } |
mcimadamore@1336 | 149 | |
mcimadamore@1336 | 150 | abstract boolean check(CPInfo cpInfo) throws Exception; |
mcimadamore@1336 | 151 | |
mcimadamore@1336 | 152 | Object getValue(Symtab syms, Names names) { |
mcimadamore@1336 | 153 | switch (this) { |
mcimadamore@1336 | 154 | case STRING: |
mcimadamore@1336 | 155 | case INTEGER: |
mcimadamore@1336 | 156 | case LONG: |
mcimadamore@1336 | 157 | case FLOAT: |
mcimadamore@1336 | 158 | case DOUBLE: |
mcimadamore@1336 | 159 | return value; |
mcimadamore@1336 | 160 | case CLASS: |
mcimadamore@1336 | 161 | return syms.stringType.tsym; |
mcimadamore@1336 | 162 | case METHOD_HANDLE: |
mcimadamore@1336 | 163 | return new Pool.MethodHandle(REF_invokeVirtual, syms.arrayCloneMethod, names); |
mcimadamore@1336 | 164 | case METHOD_TYPE: |
mcimadamore@1336 | 165 | return syms.arrayCloneMethod.type; |
mcimadamore@1336 | 166 | default: |
mcimadamore@1336 | 167 | throw new AssertionError(); |
mcimadamore@1336 | 168 | } |
mcimadamore@1336 | 169 | } |
mcimadamore@1336 | 170 | } |
mcimadamore@1336 | 171 | |
mcimadamore@1336 | 172 | enum StaticArgumentsArity { |
mcimadamore@1336 | 173 | ZERO(0), |
mcimadamore@1336 | 174 | ONE(1), |
mcimadamore@1336 | 175 | TWO(2), |
mcimadamore@1336 | 176 | THREE(3); |
mcimadamore@1336 | 177 | |
mcimadamore@1336 | 178 | int arity; |
mcimadamore@1336 | 179 | |
mcimadamore@1336 | 180 | StaticArgumentsArity(int arity) { |
mcimadamore@1336 | 181 | this.arity = arity; |
mcimadamore@1336 | 182 | } |
mcimadamore@1336 | 183 | } |
mcimadamore@1336 | 184 | |
mcimadamore@1336 | 185 | public static void main(String... args) throws Exception { |
mcimadamore@1336 | 186 | // Create a single file manager and compiler and reuse it for each compile to save time. |
mcimadamore@1336 | 187 | StandardJavaFileManager fm = JavacTool.create().getStandardFileManager(null, null, null); |
mcimadamore@1336 | 188 | final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@1336 | 189 | for (StaticArgumentsArity arity : StaticArgumentsArity.values()) { |
mcimadamore@1336 | 190 | if (arity.arity == 0) { |
mcimadamore@1336 | 191 | new TestInvokeDynamic(arity).compileAndCheck(fm, tool); |
mcimadamore@1336 | 192 | } else { |
mcimadamore@1336 | 193 | for (StaticArgumentKind sak1 : StaticArgumentKind.values()) { |
mcimadamore@1336 | 194 | if (arity.arity == 1) { |
mcimadamore@1336 | 195 | new TestInvokeDynamic(arity, sak1).compileAndCheck(fm, tool); |
mcimadamore@1336 | 196 | } else { |
mcimadamore@1336 | 197 | for (StaticArgumentKind sak2 : StaticArgumentKind.values()) { |
mcimadamore@1336 | 198 | if (arity.arity == 2) { |
mcimadamore@1336 | 199 | new TestInvokeDynamic(arity, sak1, sak2).compileAndCheck(fm, tool); |
mcimadamore@1336 | 200 | } else { |
mcimadamore@1336 | 201 | for (StaticArgumentKind sak3 : StaticArgumentKind.values()) { |
mcimadamore@1336 | 202 | new TestInvokeDynamic(arity, sak1, sak2, sak3).compileAndCheck(fm, tool); |
mcimadamore@1336 | 203 | } |
mcimadamore@1336 | 204 | } |
mcimadamore@1336 | 205 | } |
mcimadamore@1336 | 206 | } |
mcimadamore@1336 | 207 | } |
mcimadamore@1336 | 208 | } |
mcimadamore@1336 | 209 | } |
mcimadamore@1336 | 210 | |
mcimadamore@1336 | 211 | System.out.println("Total checks made: " + checkCount); |
mcimadamore@1336 | 212 | } |
mcimadamore@1336 | 213 | |
mcimadamore@1336 | 214 | StaticArgumentsArity arity; |
mcimadamore@1336 | 215 | StaticArgumentKind[] saks; |
mcimadamore@1336 | 216 | JavaSource source; |
mcimadamore@1336 | 217 | DiagChecker dc; |
mcimadamore@1336 | 218 | |
mcimadamore@1336 | 219 | TestInvokeDynamic(StaticArgumentsArity arity, StaticArgumentKind... saks) { |
mcimadamore@1336 | 220 | this.arity = arity; |
mcimadamore@1336 | 221 | this.saks = saks; |
mcimadamore@1336 | 222 | source = new JavaSource(); |
mcimadamore@1336 | 223 | dc = new DiagChecker(); |
mcimadamore@1336 | 224 | } |
mcimadamore@1336 | 225 | |
mcimadamore@1336 | 226 | void compileAndCheck(JavaFileManager fm, JavaCompiler tool) throws Exception { |
mcimadamore@1336 | 227 | JavacTaskImpl ct = (JavacTaskImpl)tool.getTask(null, fm, dc, |
mcimadamore@1336 | 228 | null, null, Arrays.asList(source)); |
mcimadamore@1336 | 229 | Context context = ct.getContext(); |
mcimadamore@1336 | 230 | Symtab syms = Symtab.instance(context); |
mcimadamore@1336 | 231 | Names names = Names.instance(context); |
mcimadamore@1336 | 232 | ct.addTaskListener(new Indifier(syms, names)); |
mcimadamore@1336 | 233 | try { |
mcimadamore@1336 | 234 | ct.generate(); |
mcimadamore@1336 | 235 | } catch (Throwable t) { |
mcimadamore@1336 | 236 | t.printStackTrace(); |
mcimadamore@1336 | 237 | throw new AssertionError(String.format("Error thrown when compiling following code\n%s", source.source)); |
mcimadamore@1336 | 238 | } |
mcimadamore@1336 | 239 | if (dc.diagFound) { |
mcimadamore@1336 | 240 | throw new AssertionError(String.format("Diags found when compiling following code\n%s\n\n%s", source.source, dc.printDiags())); |
mcimadamore@1336 | 241 | } |
mcimadamore@1336 | 242 | verifyBytecode(); |
mcimadamore@1336 | 243 | } |
mcimadamore@1336 | 244 | |
mcimadamore@1336 | 245 | void verifyBytecode() { |
mcimadamore@1336 | 246 | File compiledTest = new File("Test.class"); |
mcimadamore@1336 | 247 | try { |
mcimadamore@1336 | 248 | ClassFile cf = ClassFile.read(compiledTest); |
mcimadamore@1336 | 249 | Method testMethod = null; |
mcimadamore@1336 | 250 | for (Method m : cf.methods) { |
mcimadamore@1336 | 251 | if (m.getName(cf.constant_pool).equals("test")) { |
mcimadamore@1336 | 252 | testMethod = m; |
mcimadamore@1336 | 253 | break; |
mcimadamore@1336 | 254 | } |
mcimadamore@1336 | 255 | } |
mcimadamore@1336 | 256 | if (testMethod == null) { |
mcimadamore@1336 | 257 | throw new Error("Test method not found"); |
mcimadamore@1336 | 258 | } |
mcimadamore@1336 | 259 | Code_attribute ea = (Code_attribute)testMethod.attributes.get(Attribute.Code); |
mcimadamore@1336 | 260 | if (testMethod == null) { |
mcimadamore@1336 | 261 | throw new Error("Code attribute for test() method not found"); |
mcimadamore@1336 | 262 | } |
mcimadamore@1336 | 263 | |
mcimadamore@1336 | 264 | int bsmIdx = -1; |
mcimadamore@1336 | 265 | |
mcimadamore@1336 | 266 | for (Instruction i : ea.getInstructions()) { |
mcimadamore@1336 | 267 | if (i.getMnemonic().equals("invokedynamic")) { |
mcimadamore@1336 | 268 | CONSTANT_InvokeDynamic_info indyInfo = |
mcimadamore@1336 | 269 | (CONSTANT_InvokeDynamic_info)cf.constant_pool.get(i.getShort(1)); |
mcimadamore@1336 | 270 | bsmIdx = indyInfo.bootstrap_method_attr_index; |
mcimadamore@1336 | 271 | if (!indyInfo.getNameAndTypeInfo().getType().equals("()V")) { |
mcimadamore@1336 | 272 | throw new AssertionError("type mismatch for CONSTANT_InvokeDynamic_info"); |
mcimadamore@1336 | 273 | } |
mcimadamore@1336 | 274 | } |
mcimadamore@1336 | 275 | } |
mcimadamore@1336 | 276 | if (bsmIdx == -1) { |
mcimadamore@1336 | 277 | throw new Error("Missing invokedynamic in generated code"); |
mcimadamore@1336 | 278 | } |
mcimadamore@1336 | 279 | |
mcimadamore@1336 | 280 | BootstrapMethods_attribute bsm_attr = (BootstrapMethods_attribute)cf.getAttribute(Attribute.BootstrapMethods); |
mcimadamore@1336 | 281 | if (bsm_attr.bootstrap_method_specifiers.length != 1) { |
mcimadamore@1336 | 282 | throw new Error("Bad number of method specifiers in BootstrapMethods attribute"); |
mcimadamore@1336 | 283 | } |
mcimadamore@1336 | 284 | BootstrapMethods_attribute.BootstrapMethodSpecifier bsm_spec = |
mcimadamore@1336 | 285 | bsm_attr.bootstrap_method_specifiers[0]; |
mcimadamore@1336 | 286 | |
mcimadamore@1336 | 287 | if (bsm_spec.bootstrap_arguments.length != arity.arity) { |
mcimadamore@1336 | 288 | throw new Error("Bad number of static invokedynamic args in BootstrapMethod attribute"); |
mcimadamore@1336 | 289 | } |
mcimadamore@1336 | 290 | |
mcimadamore@1336 | 291 | int count = 0; |
mcimadamore@1336 | 292 | for (StaticArgumentKind sak : saks) { |
mcimadamore@1336 | 293 | if (!sak.check(cf.constant_pool.get(bsm_spec.bootstrap_arguments[count]))) { |
mcimadamore@1336 | 294 | throw new Error("Bad static argument value " + sak); |
mcimadamore@1336 | 295 | } |
mcimadamore@1336 | 296 | count++; |
mcimadamore@1336 | 297 | } |
mcimadamore@1336 | 298 | |
mcimadamore@1336 | 299 | CONSTANT_MethodHandle_info bsm_handle = |
mcimadamore@1336 | 300 | (CONSTANT_MethodHandle_info)cf.constant_pool.get(bsm_spec.bootstrap_method_ref); |
mcimadamore@1336 | 301 | |
mcimadamore@1336 | 302 | if (bsm_handle.reference_kind != RefKind.REF_invokeStatic) { |
mcimadamore@1336 | 303 | throw new Error("Bad kind on boostrap method handle"); |
mcimadamore@1336 | 304 | } |
mcimadamore@1336 | 305 | |
mcimadamore@1336 | 306 | CONSTANT_Methodref_info bsm_ref = |
mcimadamore@1336 | 307 | (CONSTANT_Methodref_info)cf.constant_pool.get(bsm_handle.reference_index); |
mcimadamore@1336 | 308 | |
mcimadamore@1336 | 309 | if (!bsm_ref.getClassInfo().getName().equals("Bootstrap")) { |
mcimadamore@1336 | 310 | throw new Error("Bad owner of boostrap method"); |
mcimadamore@1336 | 311 | } |
mcimadamore@1336 | 312 | |
mcimadamore@1336 | 313 | if (!bsm_ref.getNameAndTypeInfo().getName().equals("bsm")) { |
mcimadamore@1336 | 314 | throw new Error("Bad boostrap method name"); |
mcimadamore@1336 | 315 | } |
mcimadamore@1336 | 316 | |
mcimadamore@1336 | 317 | if (!bsm_ref.getNameAndTypeInfo().getType().equals(asBSMSignatureString())) { |
mcimadamore@1336 | 318 | throw new Error("Bad boostrap method type" + bsm_ref.getNameAndTypeInfo().getType() + " " + asBSMSignatureString()); |
mcimadamore@1336 | 319 | } |
mcimadamore@1336 | 320 | } catch (Exception e) { |
mcimadamore@1336 | 321 | e.printStackTrace(); |
mcimadamore@1336 | 322 | throw new Error("error reading " + compiledTest +": " + e); |
mcimadamore@1336 | 323 | } |
mcimadamore@1336 | 324 | } |
mcimadamore@1336 | 325 | |
mcimadamore@1336 | 326 | String asBSMSignatureString() { |
mcimadamore@1336 | 327 | StringBuilder buf = new StringBuilder(); |
mcimadamore@1336 | 328 | buf.append("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;"); |
mcimadamore@1336 | 329 | for (StaticArgumentKind sak : saks) { |
mcimadamore@1336 | 330 | buf.append(sak.bytecodeTypeStr); |
mcimadamore@1336 | 331 | } |
mcimadamore@1336 | 332 | buf.append(")Ljava/lang/invoke/CallSite;"); |
mcimadamore@1336 | 333 | return buf.toString(); |
mcimadamore@1336 | 334 | } |
mcimadamore@1336 | 335 | |
mcimadamore@1336 | 336 | class JavaSource extends SimpleJavaFileObject { |
mcimadamore@1336 | 337 | |
mcimadamore@1336 | 338 | static final String source_template = "import java.lang.invoke.*;\n" + |
mcimadamore@1336 | 339 | "class Bootstrap {\n" + |
mcimadamore@1336 | 340 | " public static CallSite bsm(MethodHandles.Lookup lookup, String name, MethodType methodType #SARGS) {\n" + |
mcimadamore@1336 | 341 | " return null;\n" + |
mcimadamore@1336 | 342 | " }\n" + |
mcimadamore@1336 | 343 | "}\n" + |
mcimadamore@1336 | 344 | "class Test {\n" + |
mcimadamore@1336 | 345 | " void m() { }\n" + |
mcimadamore@1336 | 346 | " void test() { m(); }\n" + |
mcimadamore@1336 | 347 | "}"; |
mcimadamore@1336 | 348 | |
mcimadamore@1336 | 349 | String source; |
mcimadamore@1336 | 350 | |
mcimadamore@1336 | 351 | JavaSource() { |
mcimadamore@1336 | 352 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@1336 | 353 | source = source_template.replace("#SARGS", asSignatureString()); |
mcimadamore@1336 | 354 | } |
mcimadamore@1336 | 355 | |
mcimadamore@1336 | 356 | @Override |
mcimadamore@1336 | 357 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@1336 | 358 | return source; |
mcimadamore@1336 | 359 | } |
mcimadamore@1336 | 360 | |
mcimadamore@1336 | 361 | String asSignatureString() { |
mcimadamore@1336 | 362 | int count = 0; |
mcimadamore@1336 | 363 | StringBuilder buf = new StringBuilder(); |
mcimadamore@1336 | 364 | for (StaticArgumentKind sak : saks) { |
mcimadamore@1336 | 365 | buf.append(","); |
mcimadamore@1336 | 366 | buf.append(sak.sourceTypeStr); |
mcimadamore@1336 | 367 | buf.append(' '); |
mcimadamore@1336 | 368 | buf.append(String.format("x%d", count++)); |
mcimadamore@1336 | 369 | } |
mcimadamore@1336 | 370 | return buf.toString(); |
mcimadamore@1336 | 371 | } |
mcimadamore@1336 | 372 | } |
mcimadamore@1336 | 373 | |
mcimadamore@1336 | 374 | class Indifier extends TreeScanner<Void, Void> implements TaskListener { |
mcimadamore@1336 | 375 | |
mcimadamore@1336 | 376 | MethodSymbol bsm; |
mcimadamore@1336 | 377 | Symtab syms; |
mcimadamore@1336 | 378 | Names names; |
mcimadamore@1336 | 379 | |
mcimadamore@1336 | 380 | Indifier(Symtab syms, Names names) { |
mcimadamore@1336 | 381 | this.syms = syms; |
mcimadamore@1336 | 382 | this.names = names; |
mcimadamore@1336 | 383 | } |
mcimadamore@1336 | 384 | |
mcimadamore@1336 | 385 | @Override |
mcimadamore@1336 | 386 | public void started(TaskEvent e) { |
mcimadamore@1336 | 387 | //do nothing |
mcimadamore@1336 | 388 | } |
mcimadamore@1336 | 389 | |
mcimadamore@1336 | 390 | @Override |
mcimadamore@1336 | 391 | public void finished(TaskEvent e) { |
mcimadamore@1336 | 392 | if (e.getKind() == TaskEvent.Kind.ANALYZE) { |
mcimadamore@1336 | 393 | scan(e.getCompilationUnit(), null); |
mcimadamore@1336 | 394 | } |
mcimadamore@1336 | 395 | } |
mcimadamore@1336 | 396 | |
mcimadamore@1336 | 397 | @Override |
mcimadamore@1336 | 398 | public Void visitMethodInvocation(MethodInvocationTree node, Void p) { |
mcimadamore@1336 | 399 | super.visitMethodInvocation(node, p); |
mcimadamore@1336 | 400 | JCMethodInvocation apply = (JCMethodInvocation)node; |
mcimadamore@1336 | 401 | JCIdent ident = (JCIdent)apply.meth; |
mcimadamore@1336 | 402 | Symbol oldSym = ident.sym; |
mcimadamore@1336 | 403 | if (!oldSym.isConstructor()) { |
mcimadamore@1336 | 404 | Object[] staticArgs = new Object[arity.arity]; |
mcimadamore@1336 | 405 | for (int i = 0; i < arity.arity ; i++) { |
mcimadamore@1336 | 406 | staticArgs[i] = saks[i].getValue(syms, names); |
mcimadamore@1336 | 407 | } |
mcimadamore@1336 | 408 | ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name, oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs); |
mcimadamore@1336 | 409 | } |
mcimadamore@1336 | 410 | return null; |
mcimadamore@1336 | 411 | } |
mcimadamore@1336 | 412 | |
mcimadamore@1336 | 413 | @Override |
mcimadamore@1336 | 414 | public Void visitMethod(MethodTree node, Void p) { |
mcimadamore@1336 | 415 | super.visitMethod(node, p); |
mcimadamore@1336 | 416 | if (node.getName().toString().equals("bsm")) { |
mcimadamore@1336 | 417 | bsm = ((JCMethodDecl)node).sym; |
mcimadamore@1336 | 418 | } |
mcimadamore@1336 | 419 | return null; |
mcimadamore@1336 | 420 | } |
mcimadamore@1336 | 421 | } |
mcimadamore@1336 | 422 | |
mcimadamore@1336 | 423 | static class DiagChecker implements javax.tools.DiagnosticListener<JavaFileObject> { |
mcimadamore@1336 | 424 | |
mcimadamore@1336 | 425 | boolean diagFound; |
mcimadamore@1336 | 426 | ArrayList<String> diags = new ArrayList<>(); |
mcimadamore@1336 | 427 | |
mcimadamore@1336 | 428 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@1336 | 429 | diags.add(diagnostic.getMessage(Locale.getDefault())); |
mcimadamore@1336 | 430 | diagFound = true; |
mcimadamore@1336 | 431 | } |
mcimadamore@1336 | 432 | |
mcimadamore@1336 | 433 | String printDiags() { |
mcimadamore@1336 | 434 | StringBuilder buf = new StringBuilder(); |
mcimadamore@1336 | 435 | for (String s : diags) { |
mcimadamore@1336 | 436 | buf.append(s); |
mcimadamore@1336 | 437 | buf.append("\n"); |
mcimadamore@1336 | 438 | } |
mcimadamore@1336 | 439 | return buf.toString(); |
mcimadamore@1336 | 440 | } |
mcimadamore@1336 | 441 | } |
mcimadamore@1336 | 442 | } |