jdk/test/java/lang/invoke/LambdaFormTest.java
author alanb
Fri, 10 Feb 2017 09:04:39 +0000
changeset 43712 5dfd0950317c
parent 42338 a60f280f803c
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: dfuchs, psandoz, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, alex.buckley@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
     1
/*
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
     4
 *
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
     8
 *
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    13
 * accompanied this code).
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    14
 *
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    18
 *
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    21
 * questions.
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    22
 */
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    23
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    24
/* @test
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    25
 * @summary unit tests for java.lang.invoke.LambdaForm
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 24572
diff changeset
    26
 * @modules java.base/java.lang.invoke:open
24572
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    27
 * @run junit/othervm test.java.lang.invoke.LambdaFormTest
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    28
 */
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    29
package test.java.lang.invoke;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    30
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    31
import org.junit.Test;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    32
import java.lang.reflect.Method;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    33
import static org.junit.Assert.*;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    34
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    35
public class LambdaFormTest {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    36
    static final Method M_shortenSignature;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    37
    static {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    38
        try {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    39
            Class<?> impl = Class.forName("java.lang.invoke.LambdaForm", false, null);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    40
            Method m = impl.getDeclaredMethod("shortenSignature", String.class);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    41
            m.setAccessible(true);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    42
            M_shortenSignature = m;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    43
        } catch(Exception e) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    44
            throw new AssertionError(e);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    45
        }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    46
    }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    47
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    48
    public static String shortenSignature(String signature) throws ReflectiveOperationException {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    49
        return (String)M_shortenSignature.invoke(null, signature);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    50
    }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    51
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    52
    @Test
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    53
    public void testShortenSignature() throws ReflectiveOperationException {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    54
        for (String s : new String[] {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    55
                // invariant strings:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    56
                "L", "LL", "ILL", "LIL", "LLI", "IILL", "ILIL", "ILLI",
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    57
                // a few mappings:
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    58
                "LLL=L3", "LLLL=L4", "LLLLLLLLLL=L10",
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    59
                "IIIDDD=I3D3", "IDDD=ID3", "IIDDD=IID3", "IIID=I3D", "IIIDD=I3DD"
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    60
            }) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    61
            String s2 = s.substring(s.indexOf('=')+1);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    62
            String s1 = s.equals(s2) ? s : s.substring(0, s.length() - s2.length() - 1);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    63
            // mix the above cases with before and after reps of Z*
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    64
            for (int k = -3; k <= 3; k++) {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    65
                String beg = (k < 0 ? "ZZZZ".substring(-k) : "");
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    66
                String end = (k > 0 ? "ZZZZ".substring(+k) : "");
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    67
                String ks1 = beg+s1+end;
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    68
                String ks2 = shortenSignature(beg)+s2+shortenSignature(end);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    69
                String ks3 = shortenSignature(ks1);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    70
                assertEquals(ks2, ks3);
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    71
            }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    72
        }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    73
    }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    74
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    75
    public static void main(String[] args) throws ReflectiveOperationException {
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    76
        LambdaFormTest test = new LambdaFormTest();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    77
        test.testShortenSignature();
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    78
    }
5c9e5961d21c 8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
diff changeset
    79
}