jdk-24/test/jdk/java/lang/invoke/findVirtual/FindVirtualArrayCloneTest.java
2023-05-31 16:28:26 +00:00

76 lines
2.9 KiB
Java

/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8299505
* @run junit FindVirtualArrayCloneTest
* @summary Test invocation of Object.clone for arrays
*/
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class FindVirtualArrayCloneTest {
/**
* array clone should be bound to the array class (public)
*/
@ValueSource(classes = {Object[].class, int[].class, long[].class, byte[].class, double[].class})
@ParameterizedTest
public void checkArrayCloneReceiver(Class<?> arrayClass) throws Throwable {
var lookup = MethodHandles.lookup();
var cloneMh = lookup.findVirtual(arrayClass, "clone", MethodType.methodType(Object.class));
assertSame(arrayClass, cloneMh.type().parameterType(0));
}
/**
* Object.clone should be bound to the lookup class (protected)
*/
@Test
public void checkProtectedCloneReceiver() throws Throwable {
var lookup = MethodHandles.lookup();
var cloneMh = lookup.findVirtual(Object.class, "clone", MethodType.methodType(Object.class));
assertSame(lookup.lookupClass(), cloneMh.type().parameterType(0));
}
@Test
public void checkArrayClone() throws Throwable {
var array = new String[] {"apple", "pear", "banana"};
var regularClone = array.clone();
var cloneMh = MethodHandles.lookup()
.findVirtual(String[].class, "clone", MethodType.methodType(Object.class))
.asType(MethodType.methodType(String[].class, String[].class));
var mhClone = (String[]) cloneMh.invokeExact((String[]) array);
assertArrayEquals(array, mhClone);
assertArrayEquals(regularClone, mhClone);
}
}