test/jdk/java/lang/invoke/lookup/LookupClassTest.java
author mchung
Tue, 29 Oct 2019 12:52:03 -0700
changeset 58849 67a3f50b14ae
permissions -rw-r--r--
8173975: Lookup::in should not allow target class be primitive or array class Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58849
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
     1
/*
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
     4
 *
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
     8
 *
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    13
 * accompanied this code).
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    14
 *
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    18
 *
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    21
 * questions.
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    22
 */
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    23
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    24
/*
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    25
 * @test
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    26
 * @bug 8173975
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    27
 * @summary Lookup::in throws IAE if the target class is a primitive class or array class
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    28
 * @run testng/othervm LookupClassTest
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    29
 */
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    30
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    31
import java.lang.invoke.MethodHandles;
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    32
import java.lang.invoke.MethodHandles.Lookup;
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    33
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    34
import org.testng.annotations.BeforeTest;
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    35
import org.testng.annotations.Test;
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    36
import static org.testng.Assert.*;
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    37
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    38
public class LookupClassTest {
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    39
    private static final LookupClassTest[] ARRAY = new LookupClassTest[0];
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    40
    @BeforeTest
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    41
    public void test() {
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    42
        assertTrue(ARRAY.getClass().isArray());
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    43
        assertSamePackage(MethodHandles.lookup(), ARRAY.getClass());
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    44
        assertSamePackage(MethodHandles.publicLookup(), int.class);
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    45
    }
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    46
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    47
    private void assertSamePackage(Lookup lookup, Class<?> targetClass) {
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    48
        assertEquals(lookup.lookupClass().getPackageName(), targetClass.getPackageName());
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    49
    }
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    50
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    51
    @Test(expectedExceptions = IllegalArgumentException.class)
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    52
    public void arrayLookupClass() {
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    53
        Lookup lookup = MethodHandles.lookup();
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    54
        lookup.in(ARRAY.getClass());
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    55
    }
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    56
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    57
    @Test(expectedExceptions = IllegalArgumentException.class)
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    58
    public void primitiveLookupClass() {
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    59
        Lookup lookup = MethodHandles.publicLookup();
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    60
        lookup.in(int.class);
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    61
    }
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    62
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    63
    @Test(expectedExceptions = IllegalArgumentException.class)
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    64
    public void voidLookupClass() {
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    65
        Lookup lookup = MethodHandles.publicLookup();
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    66
        lookup.in(void.class);
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    67
    }
67a3f50b14ae 8173975: Lookup::in should not allow target class be primitive or array class
mchung
parents:
diff changeset
    68
}