1 /*
   2  * Copyright (c) 2014, 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 /* @test
  25  * @summary unit tests for java.lang.invoke.LambdaForm
  26  * @run junit/othervm test.java.lang.invoke.LambdaFormTest
  27  */
  28 package test.java.lang.invoke;
  29 
  30 import org.junit.Test;
  31 import java.lang.reflect.Method;
  32 import static org.junit.Assert.*;
  33 
  34 public class LambdaFormTest {
  35     static final Method M_shortenSignature;
  36     static {
  37         try {
  38             Class<?> impl = Class.forName("java.lang.invoke.LambdaForm", false, null);
  39             Method m = impl.getDeclaredMethod("shortenSignature", String.class);
  40             m.setAccessible(true);
  41             M_shortenSignature = m;
  42         } catch(Exception e) {
  43             throw new AssertionError(e);
  44         }
  45     }
  46 
  47     public static String shortenSignature(String signature) throws ReflectiveOperationException {
  48         return (String)M_shortenSignature.invoke(null, signature);
  49     }
  50 
  51     @Test
  52     public void testShortenSignature() throws ReflectiveOperationException {
  53         for (String s : new String[] {
  54                 // invariant strings:
  55                 "L", "LL", "ILL", "LIL", "LLI", "IILL", "ILIL", "ILLI",
  56                 // a few mappings:
  57                 "LLL=L3", "LLLL=L4", "LLLLLLLLLL=L10",
  58                 "IIIDDD=I3D3", "IDDD=ID3", "IIDDD=IID3", "IIID=I3D", "IIIDD=I3DD"
  59             }) {
  60             String s2 = s.substring(s.indexOf('=')+1);
  61             String s1 = s.equals(s2) ? s : s.substring(0, s.length() - s2.length() - 1);
  62             // mix the above cases with before and after reps of Z*
  63             for (int k = -3; k <= 3; k++) {
  64                 String beg = (k < 0 ? "ZZZZ".substring(-k) : "");
  65                 String end = (k > 0 ? "ZZZZ".substring(+k) : "");
  66                 String ks1 = beg+s1+end;
  67                 String ks2 = shortenSignature(beg)+s2+shortenSignature(end);
  68                 String ks3 = shortenSignature(ks1);
  69                 assertEquals(ks2, ks3);
  70             }
  71         }
  72     }
  73 
  74     public static void main(String[] args) throws ReflectiveOperationException {
  75         LambdaFormTest test = new LambdaFormTest();
  76         test.testShortenSignature();
  77     }
  78 }