< prev index next >

test/jdk/java/lang/invoke/condy/CondyBSMInvocation.java

Print this page




   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 8199875
  27  * @summary Test basic invocation of bootstrap methods
  28  * @library /lib/testlibrary/bytecode /java/lang/invoke/common
  29  * @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper
  30  * @run testng CondyBSMInvocation
  31  * @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 CondyBSMInvocation
  32  */
  33 
  34 
  35 import org.testng.Assert;
  36 import org.testng.annotations.Test;
  37 import test.java.lang.invoke.lib.InstructionHelper;
  38 

  39 import java.lang.invoke.MethodHandle;
  40 import java.lang.invoke.MethodHandles;
  41 import java.lang.invoke.MethodType;
  42 import java.lang.invoke.WrongMethodTypeException;
  43 import java.util.Arrays;
  44 import java.util.Collections;
  45 import java.util.stream.IntStream;
  46 import java.util.stream.Stream;
  47 
  48 import static java.lang.invoke.MethodType.methodType;
  49 
  50 public class CondyBSMInvocation {
  51     static final MethodHandles.Lookup L = MethodHandles.lookup();
  52 
  53 
  54     @Test
  55     public void testNonexistent() throws Throwable {
  56         MethodHandle mh = InstructionHelper.ldcDynamicConstant(
  57                 L, "name", Object.class,
  58                 "bsm", methodType(Object.class),
  59                 S -> {});
  60 
  61         try {
  62             mh.invoke();
  63             Assert.fail("NoSuchMethodError expected to be thrown");
  64         } catch (NoSuchMethodError e) {
  65         }
  66     }
  67 
  68     static MethodHandle[] bsms(String bsmName) {
  69         return Stream.of(CondyBSMInvocation.class.getDeclaredMethods()).
  70                 filter(m -> m.getName().equals(bsmName)).
  71                 map(m -> {
  72                     try {
  73                         return MethodHandles.lookup().unreflect(m);
  74                     } catch (IllegalAccessException e) {
  75                         throw new RuntimeException();
  76                     }
  77                 }).toArray(MethodHandle[]::new);
  78     }
  79 
  80     public static Object shape_bsm() {
  81         return "0";
  82     }
  83 
  84     public static Object shape_bsm(Object a1) {
  85         return "0";
  86     }
  87 
  88     public static Object shape_bsm(Object... args) {
  89         return "0";
  90     }
  91 
  92     public static Object shape_bsm(Object a1, Object a2) {
  93         return "0";
  94     }
  95 
  96     public static Object shape_bsm(Object a1, Object... args) {
  97         return "0";
  98     }
  99 
 100     public static Object shape_bsm(Object a1, Object a2, Object a3) {
 101         return "0";
 102     }
 103 
 104     public static Object shape_bsm(MethodHandles.Lookup a1) {
 105         return "0";
 106     }
 107 
 108     @Test
 109     public void testWrongShape() throws Throwable {
 110         for (MethodHandle bsm : bsms("shape_bsm")) {
 111             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 112                     L, "name", Object.class,
 113                     "shape_bsm", bsm.type(),
 114                     S -> {}
 115             );
 116 
 117             try {
 118                 Object r = mh.invoke();
 119                 Assert.fail("BootstrapMethodError expected to be thrown for " + bsm);
 120             } catch (BootstrapMethodError e) {
 121             }
 122         }
 123     }
 124 
 125 
 126     public static Object sig_bsm(MethodHandles.Lookup a1, String[] a2) {
 127         return "0";
 128     }
 129 

 130     public static Object sig_bsm(MethodHandles.Lookup a1, String a2, String a3) {
 131         return "0";
 132     }
 133 
 134     @Test
 135     public void testWrongSignature() throws Throwable {
 136         for (MethodHandle bsm : bsms("sig_bsm")) {
 137             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 138                     L, "name", Object.class,
 139                     "sig_bsm", bsm.type(),
 140                     S -> {}
 141             );
 142 
 143             try {
 144                 Object r = mh.invoke();
 145                 Assert.fail("BootstrapMethodError expected to be thrown for " + bsm);
 146             } catch (BootstrapMethodError e) {
 147             }
 148         }
 149     }
 150 
 151 
 152     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type) {
 153         return "0";
 154     }



 155 
 156     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 157                              Object a1) {
 158         assertAll(a1);
 159         return "1";
 160     }
 161 
 162     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 163                              Object a1, Object a2) {




 164         assertAll(a1, a2);
 165         return "2";
 166     }
 167 
 168     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 169                              Object a1, Object a2, Object a3) {




 170         assertAll(a1, a2, a3);
 171         return "3";
 172     }
 173 
 174     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 175                              Object a1, Object a2, Object a3, Object a4) {




 176         assertAll(a1, a2, a3, a4);
 177         return "4";
 178     }
 179 
 180     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 181                              Object a1, Object a2, Object a3, Object a4, Object a5) {




 182         assertAll(a1, a2, a3, a4, a5);
 183         return "5";
 184     }
 185 
 186     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 187                              Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) {




 188         assertAll(a1, a2, a3, a4, a5, a6);
 189         return "6";
 190     }
 191 
 192     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 193                              Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) {




 194         assertAll(a1, a2, a3, a4, a5, a6, a7);
 195         return "7";
 196     }




 197 




 198     public static Object bsm(MethodHandles.Lookup l, Object... args) {
 199         Object[] staticArgs = Arrays.copyOfRange(args, 2, args.length);
 200         assertAll(staticArgs);
 201         return Integer.toString(staticArgs.length);
 202     }
 203 
 204     static void assertAll(Object... as) {
 205         for (int i = 0; i < as.length; i++) {
 206             Assert.assertEquals(as[i], i);
 207         }
 208     }
 209 
 210     @Test
 211     public void testArity() throws Throwable {
 212         for (int i = 0; i < 8; i++) {
 213             final int n = i;
 214             MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, String.class, Class.class)
 215                     .appendParameterTypes(Collections.nCopies(n, Object.class));
 216             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 217                     L, "name", Object.class,
 218                     "bsm", mt,
 219                     S -> IntStream.range(0, n).forEach(S::add)
 220                     );
 221 
 222             Object r = mh.invoke();
 223             Assert.assertEquals(r, Integer.toString(n));
 224         }
 225 
 226         {
 227             MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, Object[].class);
 228             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 229                     L, "name", Object.class,
 230                     "bsm", mt,
 231                     S -> IntStream.range(0, 9).forEach(S::add)
 232             );
 233 
 234             Object r = mh.invoke();
 235             Assert.assertEquals(r, Integer.toString(9));














 236 














 237         }
 238     }
 239 
 240     @Test
 241     public void testWrongNumberOfStaticArguments() throws Throwable {
 242         for (int i = 1; i < 8; i++) {
 243             final int n = i;
 244             MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, String.class, Class.class)
 245                     .appendParameterTypes(Collections.nCopies(n, Object.class));
 246             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 247                     L, "name", Object.class,
 248                     "bsm", mt,
 249                     S -> IntStream.range(0, n - 1).forEach(S::add)
 250             );
 251 
 252             try {
 253                 Object r = mh.invoke();
 254                 Assert.fail("BootstrapMethodError expected to be thrown for arrity " + n);
 255             } catch (BootstrapMethodError e) {
 256                 Throwable t = e.getCause();
 257                 Assert.assertTrue(WrongMethodTypeException.class.isAssignableFrom(t.getClass()));
 258             }












































 259         }
 260     }
 261 }


   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 8199875 8206177
  27  * @summary Test basic invocation of bootstrap methods
  28  * @library /lib/testlibrary/bytecode /java/lang/invoke/common
  29  * @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper
  30  * @run testng CondyBSMInvocation
  31  * @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 CondyBSMInvocation
  32  */
  33 

  34 import org.testng.Assert;
  35 import org.testng.annotations.Test;
  36 import test.java.lang.invoke.lib.InstructionHelper;
  37 
  38 import java.lang.invoke.BootstrapCallInfo;
  39 import java.lang.invoke.MethodHandle;
  40 import java.lang.invoke.MethodHandles;
  41 import java.lang.invoke.MethodType;
  42 import java.lang.invoke.WrongMethodTypeException;
  43 import java.util.Arrays;
  44 import java.util.Collections;
  45 import java.util.stream.IntStream;
  46 import java.util.stream.Stream;
  47 
  48 import static java.lang.invoke.MethodType.methodType;
  49 
  50 public class CondyBSMInvocation {
  51     static final MethodHandles.Lookup L = MethodHandles.lookup();
  52 
  53 
  54     @Test
  55     public void testNonexistent() throws Throwable {
  56         MethodHandle mh = InstructionHelper.ldcDynamicConstant(
  57                 L, "name", Object.class,
  58                 "bsm_non_existent", methodType(Object.class),
  59                 S -> {});
  60 
  61         try {
  62             mh.invoke();
  63             Assert.fail("NoSuchMethodError expected to be thrown");
  64         } catch (NoSuchMethodError e) {
  65         }
  66     }
  67 
  68     static MethodHandle[] bsms(String bsmName) {
  69         return Stream.of(CondyBSMInvocation.class.getDeclaredMethods()).
  70                 filter(m -> m.getName().equals(bsmName)).
  71                 map(m -> {
  72                     try {
  73                         return MethodHandles.lookup().unreflect(m);
  74                     } catch (IllegalAccessException e) {
  75                         throw new RuntimeException();
  76                     }
  77                 }).toArray(MethodHandle[]::new);
  78     }
  79 
  80     // No name and type
  81     public static Object sig_bsm(MethodHandles.Lookup a1) {























  82         return "0";
  83     }
  84 
  85     // No type

















  86     public static Object sig_bsm(MethodHandles.Lookup a1, String[] a2) {
  87         return "0";
  88     }
  89 
  90     // Wrong class for type
  91     public static Object sig_bsm(MethodHandles.Lookup a1, String a2, String a3) {
  92         return "0";
  93     }
  94 
  95     @Test
  96     public void testWrongSignature() throws Throwable {
  97         for (MethodHandle bsm : bsms("sig_bsm")) {
  98             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
  99                     L, "name", Object.class,
 100                     "sig_bsm", bsm.type(),
 101                     S -> {}
 102             );
 103 
 104             try {
 105                 Object r = mh.invoke();
 106                 Assert.fail("BootstrapMethodError expected to be thrown for " + bsm);
 107             } catch (BootstrapMethodError e) {
 108             }
 109         }
 110     }
 111 
 112 
 113     public static Object bsm() {
 114         return "0";
 115     }
 116     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type) {
 117         return bsm();
 118     }
 119 
 120     public static Object bsm(Object a1) {

 121         assertAll(a1);
 122         return "1";
 123     }

 124     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 125                              Object a1) {
 126         return bsm(a1);
 127     }
 128 
 129     public static Object bsm(Object a1, Object a2) {
 130         assertAll(a1, a2);
 131         return "2";
 132     }

 133     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 134                              Object a1, Object a2) {
 135         return bsm(a1, a2);
 136     }
 137 
 138     public static Object bsm(Object a1, Object a2, Object a3) {
 139         assertAll(a1, a2, a3);
 140         return "3";
 141     }

 142     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 143                              Object a1, Object a2, Object a3) {
 144         return bsm(a1, a2, a3);
 145     }
 146 
 147     public static Object bsm(Object a1, Object a2, Object a3, Object a4) {
 148         assertAll(a1, a2, a3, a4);
 149         return "4";
 150     }

 151     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 152                              Object a1, Object a2, Object a3, Object a4) {
 153         return bsm(a1, a2, a3, a4);
 154     }
 155 
 156     public static Object bsm(Object a1, Object a2, Object a3, Object a4, Object a5) {
 157         assertAll(a1, a2, a3, a4, a5);
 158         return "5";
 159     }

 160     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 161                              Object a1, Object a2, Object a3, Object a4, Object a5) {
 162         return bsm(a1, a2, a3, a4, a5);
 163     }
 164 
 165     public static Object bsm(Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) {
 166         assertAll(a1, a2, a3, a4, a5, a6);
 167         return "6";
 168     }

 169     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 170                              Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) {
 171         return bsm(a1, a2, a3, a4, a5, a6);
 172     }
 173 
 174     public static Object bsm(Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) {
 175         assertAll(a1, a2, a3, a4, a5, a6, a7);
 176         return "7";
 177     }
 178     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type,
 179                              Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) {
 180         return bsm(a1, a2, a3, a4, a5, a6, a7);
 181     }
 182 
 183     public static Object bsm(Object... args) {
 184         assertAll(args);
 185         return Integer.toString(args.length);
 186     }
 187     public static Object bsm(MethodHandles.Lookup l, Object... args) {
 188         Object[] staticArgs = Arrays.copyOfRange(args, 2, args.length);
 189         assertAll(staticArgs);
 190         return Integer.toString(staticArgs.length);
 191     }
 192 
 193     static void assertAll(Object... as) {
 194         for (int i = 0; i < as.length; i++) {
 195             Assert.assertEquals(as[i], i);
 196         }
 197     }
 198 
 199     @Test
 200     public void testArityWithMetadata() throws Throwable {
 201         for (int i = 0; i < 8; i++) {
 202             final int n = i;
 203             MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, String.class, Class.class)
 204                     .appendParameterTypes(Collections.nCopies(n, Object.class));
 205             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 206                     L, "name", Object.class,
 207                     "bsm", mt,
 208                     S -> IntStream.range(0, n).forEach(S::add)
 209                     );
 210 
 211             Object r = mh.invoke();
 212             Assert.assertEquals(r, Integer.toString(n));
 213         }
 214 
 215         {
 216             MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, Object[].class);
 217             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 218                     L, "name", Object.class,
 219                     "bsm", mt,
 220                     S -> IntStream.range(0, 9).forEach(S::add)
 221             );
 222 
 223             Object r = mh.invoke();
 224             Assert.assertEquals(r, Integer.toString(9));
 225         }
 226     }
 227 
 228     @Test
 229     public void testArityWithoutMetadata() throws Throwable {
 230         for (int i = 0; i < 8; i++) {
 231             final int n = i;
 232             MethodType mt = methodType(Object.class)
 233                     .appendParameterTypes(Collections.nCopies(n, Object.class));
 234             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 235                     L, "name", Object.class,
 236                     "bsm", mt,
 237                     S -> IntStream.range(0, n).forEach(S::add)
 238             );
 239 
 240             Object r = mh.invoke();
 241             Assert.assertEquals(r, Integer.toString(n));
 242         }
 243 
 244         {
 245             MethodType mt = methodType(Object.class, Object[].class);
 246             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 247                     L, "name", Object.class,
 248                     "bsm", mt,
 249                     S -> IntStream.range(0, 9).forEach(S::add)
 250             );
 251 
 252             Object r = mh.invoke();
 253             Assert.assertEquals(r, Integer.toString(9));
 254         }
 255     }
 256 
 257     @Test
 258     public void testWrongNumberOfStaticArgumentsWithMetaData() throws Throwable {
 259         for (int i = 1; i < 8; i++) {
 260             final int n = i;
 261             MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, String.class, Class.class)
 262                     .appendParameterTypes(Collections.nCopies(n, Object.class));
 263             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 264                     L, "name", Object.class,
 265                     "bsm", mt,
 266                     S -> IntStream.range(0, n - 1).forEach(S::add)
 267             );
 268 
 269             try {
 270                 Object r = mh.invoke();
 271                 Assert.fail("BootstrapMethodError expected to be thrown for arrity " + n);
 272             } catch (BootstrapMethodError e) {
 273                 Throwable t = e.getCause();
 274                 Assert.assertTrue(WrongMethodTypeException.class.isAssignableFrom(t.getClass()));
 275             }
 276         }
 277     }
 278 
 279     @Test
 280     public void testWrongNumberOfStaticArgumentsWithoutMetaData() throws Throwable {
 281         for (int i = 1; i < 8; i++) {
 282             final int n = i;
 283             MethodType mt = methodType(Object.class)
 284                     .appendParameterTypes(Collections.nCopies(n, Object.class));
 285             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 286                     L, "name", Object.class,
 287                     "bsm", mt,
 288                     S -> IntStream.range(0, n - 1).forEach(S::add)
 289             );
 290 
 291             try {
 292                 Object r = mh.invoke();
 293                 Assert.fail("BootstrapMethodError expected to be thrown for arrity " + n);
 294             } catch (BootstrapMethodError e) {
 295                 Throwable t = e.getCause();
 296                 Assert.assertTrue(WrongMethodTypeException.class.isAssignableFrom(t.getClass()));
 297             }
 298         }
 299     }
 300 
 301     public static Object bsm(MethodHandles.Lookup l, BootstrapCallInfo<Class<?>> bci) {
 302         assertAll(bci.asList().toArray());
 303         return Integer.toString(bci.size());
 304     }
 305 
 306     @Test
 307     public void testArityWithBootstrapCallDescriptor() throws Throwable {
 308         MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, BootstrapCallInfo.class);
 309         MethodHandle bsm = L.findStatic(CondyBSMInvocation.class, "bsm", mt);
 310         for (int i = 0; i < 8; i++) {
 311             final int n = i;
 312             MethodHandle mh = InstructionHelper.ldcDynamicConstant(
 313                     L, "name", Object.class,
 314                     "bsm", mt,
 315                     S -> IntStream.range(0, n).forEach(S::add)
 316             );
 317 
 318             Object r = mh.invoke();
 319             Assert.assertEquals(r, Integer.toString(n));
 320         }
 321     }
 322 }
< prev index next >