test/tools/javac/lambda/TestInvokeDynamic.java

Tue, 12 Mar 2013 16:02:43 +0000

author
mcimadamore
date
Tue, 12 Mar 2013 16:02:43 +0000
changeset 1628
5ddecb91d843
parent 1520
5c956be64b9e
child 1676
e9d986381414
permissions
-rw-r--r--

8009545: Graph inference: dependencies between inference variables should be set during incorporation
Summary: Move all transitivity checks into the incorporation round
Reviewed-by: jjg

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
mcimadamore@1336 26 * @bug 7194586
vromero@1520 27 * @bug 8003280 8006694
mcimadamore@1415 28 * @summary Add lambda tests
mcimadamore@1415 29 * Add back-end support for invokedynamic
vromero@1520 30 * temporarily workaround combo tests are causing time out in several platforms
vromero@1482 31 * @library ../lib
vromero@1482 32 * @build JavacTestingAbstractThreadedTest
vromero@1520 33 * @run main/othervm TestInvokeDynamic
mcimadamore@1336 34 */
mcimadamore@1336 35
vromero@1520 36 // use /othervm to avoid jtreg timeout issues (CODETOOLS-7900047)
vromero@1520 37 // see JDK-8006746
vromero@1520 38
mcimadamore@1336 39 import com.sun.source.tree.MethodInvocationTree;
mcimadamore@1336 40 import com.sun.source.tree.MethodTree;
mcimadamore@1336 41 import com.sun.source.util.TaskEvent;
mcimadamore@1336 42 import com.sun.source.util.TaskListener;
mcimadamore@1336 43 import com.sun.source.util.TreeScanner;
mcimadamore@1336 44
mcimadamore@1336 45 import com.sun.tools.classfile.Attribute;
mcimadamore@1336 46 import com.sun.tools.classfile.BootstrapMethods_attribute;
mcimadamore@1336 47 import com.sun.tools.classfile.ClassFile;
mcimadamore@1336 48 import com.sun.tools.classfile.Code_attribute;
mcimadamore@1336 49 import com.sun.tools.classfile.ConstantPool.*;
mcimadamore@1336 50 import com.sun.tools.classfile.Instruction;
mcimadamore@1336 51 import com.sun.tools.classfile.Method;
mcimadamore@1336 52
mcimadamore@1336 53 import com.sun.tools.javac.api.JavacTaskImpl;
mcimadamore@1336 54 import com.sun.tools.javac.code.Symbol;
mcimadamore@1336 55 import com.sun.tools.javac.code.Symbol.MethodSymbol;
mcimadamore@1336 56 import com.sun.tools.javac.code.Symtab;
vromero@1452 57 import com.sun.tools.javac.code.Types;
mcimadamore@1336 58 import com.sun.tools.javac.jvm.Pool;
mcimadamore@1336 59 import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
mcimadamore@1336 60 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
mcimadamore@1336 61 import com.sun.tools.javac.tree.JCTree.JCIdent;
mcimadamore@1336 62 import com.sun.tools.javac.util.Context;
mcimadamore@1336 63 import com.sun.tools.javac.util.Names;
mcimadamore@1336 64
mcimadamore@1336 65 import java.io.File;
mcimadamore@1336 66 import java.net.URI;
mcimadamore@1336 67 import java.util.ArrayList;
mcimadamore@1336 68 import java.util.Arrays;
mcimadamore@1336 69 import java.util.Locale;
mcimadamore@1336 70
mcimadamore@1336 71 import javax.tools.Diagnostic;
mcimadamore@1336 72 import javax.tools.JavaFileObject;
mcimadamore@1336 73 import javax.tools.SimpleJavaFileObject;
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@1336 242 null, 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@1336 352 } catch (Exception e) {
mcimadamore@1336 353 e.printStackTrace();
mcimadamore@1336 354 throw new Error("error reading " + compiledTest +": " + e);
mcimadamore@1336 355 }
mcimadamore@1336 356 }
mcimadamore@1336 357
mcimadamore@1336 358 String asBSMSignatureString() {
mcimadamore@1336 359 StringBuilder buf = new StringBuilder();
mcimadamore@1336 360 buf.append("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;");
mcimadamore@1336 361 for (StaticArgumentKind sak : saks) {
mcimadamore@1336 362 buf.append(sak.bytecodeTypeStr);
mcimadamore@1336 363 }
mcimadamore@1336 364 buf.append(")Ljava/lang/invoke/CallSite;");
mcimadamore@1336 365 return buf.toString();
mcimadamore@1336 366 }
mcimadamore@1336 367
mcimadamore@1336 368 class JavaSource extends SimpleJavaFileObject {
mcimadamore@1336 369
mcimadamore@1336 370 static final String source_template = "import java.lang.invoke.*;\n" +
mcimadamore@1336 371 "class Bootstrap {\n" +
vromero@1482 372 " public static CallSite bsm(MethodHandles.Lookup lookup, " +
vromero@1482 373 "String name, MethodType methodType #SARGS) {\n" +
mcimadamore@1336 374 " return null;\n" +
mcimadamore@1336 375 " }\n" +
mcimadamore@1336 376 "}\n" +
vromero@1482 377 "class Test#ID {\n" +
mcimadamore@1336 378 " void m() { }\n" +
mcimadamore@1336 379 " void test() { m(); }\n" +
mcimadamore@1336 380 "}";
mcimadamore@1336 381
mcimadamore@1336 382 String source;
mcimadamore@1336 383
vromero@1482 384 JavaSource(int id) {
mcimadamore@1336 385 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
vromero@1482 386 source = source_template.replace("#SARGS", asSignatureString())
vromero@1482 387 .replace("#ID", String.valueOf(id));
mcimadamore@1336 388 }
mcimadamore@1336 389
mcimadamore@1336 390 @Override
mcimadamore@1336 391 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1336 392 return source;
mcimadamore@1336 393 }
mcimadamore@1336 394
mcimadamore@1336 395 String asSignatureString() {
mcimadamore@1336 396 int count = 0;
mcimadamore@1336 397 StringBuilder buf = new StringBuilder();
mcimadamore@1336 398 for (StaticArgumentKind sak : saks) {
mcimadamore@1336 399 buf.append(",");
mcimadamore@1336 400 buf.append(sak.sourceTypeStr);
mcimadamore@1336 401 buf.append(' ');
mcimadamore@1336 402 buf.append(String.format("x%d", count++));
mcimadamore@1336 403 }
mcimadamore@1336 404 return buf.toString();
mcimadamore@1336 405 }
mcimadamore@1336 406 }
mcimadamore@1336 407
mcimadamore@1336 408 class Indifier extends TreeScanner<Void, Void> implements TaskListener {
mcimadamore@1336 409
mcimadamore@1336 410 MethodSymbol bsm;
mcimadamore@1336 411 Symtab syms;
mcimadamore@1336 412 Names names;
vromero@1452 413 Types types;
mcimadamore@1336 414
vromero@1452 415 Indifier(Symtab syms, Names names, Types types) {
mcimadamore@1336 416 this.syms = syms;
mcimadamore@1336 417 this.names = names;
vromero@1452 418 this.types = types;
mcimadamore@1336 419 }
mcimadamore@1336 420
mcimadamore@1336 421 @Override
mcimadamore@1336 422 public void started(TaskEvent e) {
mcimadamore@1336 423 //do nothing
mcimadamore@1336 424 }
mcimadamore@1336 425
mcimadamore@1336 426 @Override
mcimadamore@1336 427 public void finished(TaskEvent e) {
mcimadamore@1336 428 if (e.getKind() == TaskEvent.Kind.ANALYZE) {
mcimadamore@1336 429 scan(e.getCompilationUnit(), null);
mcimadamore@1336 430 }
mcimadamore@1336 431 }
mcimadamore@1336 432
mcimadamore@1336 433 @Override
mcimadamore@1336 434 public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
mcimadamore@1336 435 super.visitMethodInvocation(node, p);
mcimadamore@1336 436 JCMethodInvocation apply = (JCMethodInvocation)node;
mcimadamore@1336 437 JCIdent ident = (JCIdent)apply.meth;
mcimadamore@1336 438 Symbol oldSym = ident.sym;
mcimadamore@1336 439 if (!oldSym.isConstructor()) {
mcimadamore@1336 440 Object[] staticArgs = new Object[arity.arity];
mcimadamore@1336 441 for (int i = 0; i < arity.arity ; i++) {
vromero@1452 442 staticArgs[i] = saks[i].getValue(syms, names, types);
mcimadamore@1336 443 }
vromero@1482 444 ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name,
vromero@1482 445 oldSym.owner, REF_invokeStatic, bsm, oldSym.type, staticArgs);
mcimadamore@1336 446 }
mcimadamore@1336 447 return null;
mcimadamore@1336 448 }
mcimadamore@1336 449
mcimadamore@1336 450 @Override
mcimadamore@1336 451 public Void visitMethod(MethodTree node, Void p) {
mcimadamore@1336 452 super.visitMethod(node, p);
mcimadamore@1336 453 if (node.getName().toString().equals("bsm")) {
mcimadamore@1336 454 bsm = ((JCMethodDecl)node).sym;
mcimadamore@1336 455 }
mcimadamore@1336 456 return null;
mcimadamore@1336 457 }
mcimadamore@1336 458 }
mcimadamore@1336 459
vromero@1482 460 static class DiagChecker
vromero@1482 461 implements javax.tools.DiagnosticListener<JavaFileObject> {
mcimadamore@1336 462
mcimadamore@1336 463 boolean diagFound;
mcimadamore@1336 464 ArrayList<String> diags = new ArrayList<>();
mcimadamore@1336 465
mcimadamore@1336 466 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@1336 467 diags.add(diagnostic.getMessage(Locale.getDefault()));
mcimadamore@1336 468 diagFound = true;
mcimadamore@1336 469 }
mcimadamore@1336 470
mcimadamore@1336 471 String printDiags() {
mcimadamore@1336 472 StringBuilder buf = new StringBuilder();
mcimadamore@1336 473 for (String s : diags) {
mcimadamore@1336 474 buf.append(s);
mcimadamore@1336 475 buf.append("\n");
mcimadamore@1336 476 }
mcimadamore@1336 477 return buf.toString();
mcimadamore@1336 478 }
mcimadamore@1336 479 }
vromero@1482 480
mcimadamore@1336 481 }

mercurial