Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/invoke/ProtectedMemberDifferentPackage/p1/T2.java
48035 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/23package p1;2425import p2.T3;2627import java.lang.invoke.MethodHandle;28import java.lang.invoke.MethodHandles;29import java.lang.invoke.MethodHandles.Lookup;30import java.lang.invoke.MethodType;31import java.util.concurrent.Callable;3233class T1 {34protected void m1() {}35protected static void m2() {}36}3738public class T2 extends T1 {39public static void main(String[] args) throws Throwable {40Lookup LOOKUP = T3.lookup();41Class<IllegalAccessException> IAE = IllegalAccessException.class;4243assertFailure(IAE, () -> LOOKUP.findVirtual(T1.class, "m1", MethodType.methodType(void.class)));44assertFailure(IAE, () -> LOOKUP.findStatic(T1.class, "m2", MethodType.methodType(void.class)));4546assertSuccess(() -> LOOKUP.findVirtual(T2.class, "m1", MethodType.methodType(void.class)));47assertSuccess(() -> LOOKUP.findVirtual(T3.class, "m1", MethodType.methodType(void.class)));4849assertSuccess(() -> LOOKUP.findStatic(T2.class, "m2", MethodType.methodType(void.class)));50assertSuccess(() -> LOOKUP.findStatic(T3.class, "m2", MethodType.methodType(void.class)));5152assertFailure(IAE, () -> LOOKUP.unreflect(T1.class.getDeclaredMethod("m1")));53assertFailure(IAE, () -> LOOKUP.unreflect(T1.class.getDeclaredMethod("m2")));5455System.out.println("TEST PASSED");56}5758public static void assertFailure(Class<? extends Throwable> expectedError, Callable r) {59try {60r.call();61} catch(Throwable e) {62if (expectedError.isAssignableFrom(e.getClass())) {63return; // expected error64} else {65throw new Error("Unexpected error type: "+e.getClass()+"; expected type: "+expectedError, e);66}67}68throw new Error("No error");69}7071public static void assertSuccess(Callable r) {72try {73r.call();74} catch(Throwable e) {75throw new Error("Unexpected error", e);76}77}78}798081