langtools/test/tools/javac/meth/TestCP.java
changeset 38537 ba5503adca64
parent 30846 2b3f379840f0
equal deleted inserted replaced
38536:42569f7fe4e6 38537:ba5503adca64
     1 /*
     1 /*
     2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    39 import java.lang.invoke.*;
    39 import java.lang.invoke.*;
    40 import java.io.*;
    40 import java.io.*;
    41 
    41 
    42 public class TestCP {
    42 public class TestCP {
    43 
    43 
    44     static class TestClass {
    44     static class TestMethodHandleInvokeExact {
       
    45         static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;";
       
    46 
    45         void test(MethodHandle mh) throws Throwable {
    47         void test(MethodHandle mh) throws Throwable {
    46             Number n = (Number)mh.invokeExact("daddy",1,'n');
    48             Number n = (Number)mh.invokeExact("daddy",1,'n');
    47             n = (Number)mh.invokeExact("bunny",1,'d');
    49             n = (Number)mh.invokeExact("bunny",1,'d');
    48             n = (Number)(mh.invokeExact("foo",1,'d'));
    50             n = (Number)(mh.invokeExact("foo",1,'d'));
    49             n = (Number)((mh.invokeExact("bar",1,'d')));
    51             n = (Number)((mh.invokeExact("bar",1,'d')));
    50         }
    52         }
    51     }
    53     }
    52 
    54 
    53     static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;";
    55     static class TestVarHandleGet {
       
    56         static final String PS_TYPE = "(Ljava/lang/String;IC)Ljava/lang/Number;";
       
    57 
       
    58         // Test with sig-poly return type
       
    59         void test(VarHandle vh) throws Throwable {
       
    60             Number n = (Number)vh.get("daddy",1,'n');
       
    61             n = (Number)vh.get("bunny",1,'d');
       
    62             n = (Number)(vh.get("foo",1,'d'));
       
    63             n = (Number)((vh.get("bar",1,'d')));
       
    64         }
       
    65     }
       
    66 
       
    67     static class TestVarHandleSet {
       
    68         static final String PS_TYPE = "(Ljava/lang/String;IC)V";
       
    69 
       
    70         // Test with non-sig-poly void return type
       
    71         void test(VarHandle vh) throws Throwable {
       
    72             vh.set("daddy",1,'n');
       
    73             vh.set("bunny",1,'d');
       
    74             vh.set("foo",1,'d');
       
    75             vh.set("bar",1,'d');
       
    76         }
       
    77     }
       
    78 
       
    79     static class TestVarHandleCompareAndSet {
       
    80         static final String PS_TYPE = "(Ljava/lang/String;IC)Z";
       
    81 
       
    82         // Test with non-sig-poly boolean return type
       
    83         void test(VarHandle vh) throws Throwable {
       
    84             boolean r = vh.compareAndSet("daddy",1,'n');
       
    85             r = vh.compareAndSet("bunny",1,'d');
       
    86             r = (vh.compareAndSet("foo",1,'d'));
       
    87             r = ((vh.compareAndSet("bar",1,'d')));
       
    88         }
       
    89     }
       
    90 
    54     static final int PS_CALLS_COUNT = 4;
    91     static final int PS_CALLS_COUNT = 4;
    55     static final String SUBTEST_NAME = TestClass.class.getName() + ".class";
       
    56     static final String TEST_METHOD_NAME = "test";
    92     static final String TEST_METHOD_NAME = "test";
    57 
    93 
    58     public static void main(String... args) throws Exception {
    94     public static void main(String... args) throws Exception {
    59         new TestCP().run();
    95         new TestCP().run();
    60     }
    96     }
    61 
    97 
    62     public void run() throws Exception {
    98     public void run() throws Exception {
    63         String workDir = System.getProperty("test.classes");
    99         verifySigPolyInvokeVirtual(
    64         File compiledTest = new File(workDir, SUBTEST_NAME);
   100                 getTestFile(TestMethodHandleInvokeExact.class),
    65         verifyMethodHandleInvocationDescriptors(compiledTest);
   101                 TestMethodHandleInvokeExact.PS_TYPE);
       
   102 
       
   103         verifySigPolyInvokeVirtual(
       
   104                 getTestFile(TestVarHandleGet.class),
       
   105                 TestVarHandleGet.PS_TYPE);
       
   106 
       
   107         verifySigPolyInvokeVirtual(
       
   108                 getTestFile(TestVarHandleSet.class),
       
   109                 TestVarHandleSet.PS_TYPE);
       
   110 
       
   111         verifySigPolyInvokeVirtual(
       
   112                 getTestFile(TestVarHandleCompareAndSet.class),
       
   113                 TestVarHandleCompareAndSet.PS_TYPE);
    66     }
   114     }
    67 
   115 
    68     void verifyMethodHandleInvocationDescriptors(File f) {
   116     static File getTestFile(Class<?> c) {
       
   117         String workDir = System.getProperty("test.classes");
       
   118         return new File(workDir, getTestName(c));
       
   119     }
       
   120     static String getTestName(Class<?> c) {
       
   121         return c.getName() + ".class";
       
   122     }
       
   123 
       
   124     void verifySigPolyInvokeVirtual(File f, String psType) {
    69         System.err.println("verify: " + f);
   125         System.err.println("verify: " + f);
    70         try {
   126         try {
    71             int count = 0;
   127             int count = 0;
    72             ClassFile cf = ClassFile.read(f);
   128             ClassFile cf = ClassFile.read(f);
    73             Method testMethod = null;
   129             Method testMethod = null;
    96                         throw new Error("Unexpected CP entry in polymorphic signature call");
   152                         throw new Error("Unexpected CP entry in polymorphic signature call");
    97                     }
   153                     }
    98                     CONSTANT_Methodref_info methRef =
   154                     CONSTANT_Methodref_info methRef =
    99                             (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry);
   155                             (CONSTANT_Methodref_info)cf.constant_pool.get(cp_entry);
   100                     String type = methRef.getNameAndTypeInfo().getType();
   156                     String type = methRef.getNameAndTypeInfo().getType();
   101                     if (!type.equals(PS_TYPE)) {
   157                     if (!type.equals(psType)) {
   102                         throw new Error("Unexpected type in polymorphic signature call: " + type);
   158                         throw new Error("Unexpected type in polymorphic signature call: " + type);
   103                     }
   159                     }
   104                 }
   160                 }
   105             }
   161             }
   106             if (instr_count != PS_CALLS_COUNT) {
   162             if (instr_count != PS_CALLS_COUNT) {