1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8186046
  27  * @summary Test bootstrap methods returning the wrong type
  28  * @library /lib/testlibrary/bytecode /java/lang/invoke/common
  29  * @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper
  30  * @run testng CondyWrongType
  31  */
  32 
  33 import org.testng.Assert;
  34 import org.testng.annotations.DataProvider;
  35 import org.testng.annotations.Test;
  36 import test.java.lang.invoke.lib.InstructionHelper;
  37 
  38 import java.lang.invoke.MethodHandle;
  39 import java.lang.invoke.MethodHandles;
  40 import java.lang.invoke.MethodType;
  41 import java.lang.invoke.WrongMethodTypeException;
  42 import java.math.BigDecimal;
  43 import java.util.ArrayList;
  44 import java.util.List;
  45 import java.util.Map;
  46 
  47 import static java.lang.invoke.MethodType.methodType;
  48 
  49 public class CondyWrongType {
  50 
  51     @DataProvider
  52     public Object[][] primitivesProvider() throws Exception {
  53         Map<String, Class<?>> typeMap = Map.of(
  54                 "B", byte.class,
  55                 "C", char.class,
  56                 "D", double.class,
  57                 "F", float.class,
  58                 "I", int.class,
  59                 "J", long.class,
  60                 "S", short.class,
  61                 "Z", boolean.class
  62                 );
  63 
  64         List<Object[]> cases = new ArrayList<>();
  65         for (String name : typeMap.keySet()) {
  66             MethodHandle zero = MethodHandles.zero(typeMap.get(name));
  67             for (String type : typeMap.keySet()) {
  68                 // Use asType transformation to detect if primitive conversion
  69                 // is supported from the BSM value type to the dynamic constant type
  70                 boolean pass = true;
  71                 try {
  72                     zero.asType(MethodType.methodType(typeMap.get(type)));
  73                 }
  74                 catch (WrongMethodTypeException e) {
  75                     pass = false;
  76                 }
  77                 cases.add(new Object[] { name, type, pass});
  78             }
  79         }
  80 
  81         return cases.stream().toArray(Object[][]::new);
  82     }
  83 
  84     @Test(dataProvider = "primitivesProvider")
  85     public void testPrimitives(String name, String type, boolean pass) {
  86         test(name, type, pass);
  87     }
  88 
  89     @Test
  90     public void testReferences() {
  91         test("String", "Ljava/math/BigDecimal;", false);
  92         test("BigDecimal", "Ljava/lang/String;", false);
  93     }
  94 
  95     @Test
  96     public void testReferenceAndPrimitives() {
  97         test("String", "B", false);
  98         test("String", "C", false);
  99         test("String", "D", false);
 100         test("String", "F", false);
 101         test("String", "I", false);
 102         test("String", "J", false);
 103         test("String", "S", false);
 104         test("String", "Z", false);
 105     }
 106 
 107     static void test(String name, String type, boolean pass) {
 108         MethodHandle mh = caster(name, type);
 109         Throwable caught = null;
 110         try {
 111             mh.invoke();
 112         }
 113         catch (Throwable t) {
 114             caught = t;
 115         }
 116 
 117         if (caught == null) {
 118             if (pass) {
 119                 return;
 120             }
 121             else {
 122                 Assert.fail("Throwable expected");
 123             }
 124         }
 125         else if (pass) {
 126             Assert.fail("Throwable not expected");
 127         }
 128 
 129         Assert.assertTrue(BootstrapMethodError.class.isAssignableFrom(caught.getClass()));
 130         caught = caught.getCause();
 131         Assert.assertNotNull(caught);
 132         Assert.assertTrue(ClassCastException.class.isAssignableFrom(caught.getClass()));
 133     }
 134 
 135     static Object bsm(MethodHandles.Lookup l, String name, Class<?> type) {
 136         switch (name) {
 137             case "B":
 138                 return (byte) 1;
 139             case "C":
 140                 return 'A';
 141             case "D":
 142                 return 1.0;
 143             case "F":
 144                 return 1.0f;
 145             case "I":
 146                 return 1;
 147             case "J":
 148                 return 1L;
 149             case "S":
 150                 return (short) 1;
 151             case "Z":
 152                 return true;
 153             case "String":
 154                 return "string";
 155             case "BigDecimal":
 156                 return BigDecimal.ONE;
 157             default:
 158                 throw new UnsupportedOperationException();
 159         }
 160     }
 161 
 162     static MethodHandle caster(String name, String type) {
 163         try {
 164             return InstructionHelper.ldcDynamicConstant(
 165                     MethodHandles.lookup(),
 166                     name, type,
 167                     "bsm", methodType(Object.class, MethodHandles.Lookup.class, String.class, Class.class).toMethodDescriptorString(),
 168                     S -> { });
 169         } catch (Exception e) {
 170             throw new Error(e);
 171         }
 172     }
 173 }