1 /*
   2  * Copyright (c) 2018, 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 import java.lang.invoke.TypeDescriptor;
  25 import java.lang.constant.ClassDesc;
  26 
  27 import org.testng.annotations.Test;
  28 
  29 import static java.lang.constant.ConstantDescs.*;
  30 import static org.testng.Assert.assertEquals;
  31 import static org.testng.Assert.assertFalse;
  32 import static org.testng.Assert.assertNull;
  33 import static org.testng.Assert.assertTrue;
  34 
  35 /**
  36  * @test
  37  * @compile TypeDescriptorTest.java
  38  * @run testng TypeDescriptorTest
  39  * @summary unit tests for implementations of java.lang.invoke.TypeDescriptor
  40  */
  41 @Test
  42 public class TypeDescriptorTest {
  43     private<F extends TypeDescriptor.OfField<F>> void testArray(F f, boolean isArray, F component, F array) {
  44         if (isArray) {
  45             assertTrue(f.isArray());
  46             assertEquals(f.arrayType(), array);
  47             assertEquals(f.componentType(), component);
  48         }
  49         else {
  50             assertFalse(f.isArray());
  51             assertEquals(f.arrayType(), array);
  52             assertNull(f.componentType());
  53         }
  54     }
  55 
  56     public void testClass() {
  57         testArray(int.class, false, null, int[].class);
  58         testArray(int[].class, true, int.class, int[][].class);
  59         testArray(int[][].class, true, int[].class, int[][][].class);
  60         testArray(String.class, false, null, String[].class);
  61         testArray(String[].class, true, String.class, String[][].class);
  62         testArray(String[][].class, true, String[].class, String[][][].class);
  63 
  64         assertTrue(int.class.isPrimitive());
  65         assertFalse(int[].class.isPrimitive());
  66         assertFalse(String.class.isPrimitive());
  67         assertFalse(String[].class.isPrimitive());
  68     }
  69 
  70     public void testClassDesc() {
  71 
  72         testArray(CD_int, false, null, CD_int.arrayType());
  73         testArray(CD_int.arrayType(), true, CD_int, CD_int.arrayType(2));
  74         testArray(CD_int.arrayType(2), true, CD_int.arrayType(), CD_int.arrayType(3));
  75         testArray(CD_String, false, null, CD_String.arrayType());
  76         testArray(CD_String.arrayType(), true, CD_String, CD_String.arrayType(2));
  77         testArray(CD_String.arrayType(2), true, CD_String.arrayType(), CD_String.arrayType(3));
  78 
  79         assertTrue(CD_int.isPrimitive());
  80         assertFalse(CD_int.arrayType().isPrimitive());
  81         assertFalse(CD_String.isPrimitive());
  82         assertFalse(CD_String.arrayType().isPrimitive());
  83     }
  84 
  85 }