test/tools/javac/lambda/TestInvokeDynamic.java

Fri, 30 Nov 2012 15:14:25 +0000

author
mcimadamore
date
Fri, 30 Nov 2012 15:14:25 +0000
changeset 1434
34d1ebaf4645
parent 1415
01c9d4161882
child 1452
de1ec6fc93fe
permissions
-rw-r--r--

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

mercurial