1 /*
 2  * Copyright (c) 2021, 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.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package jdk.internal.reflect;
26 
27 import jdk.internal.vm.annotation.ForceInline;
28 import jdk.internal.vm.annotation.Hidden;
29 
30 import java.lang.reflect.InvocationTargetException;
31 import java.lang.reflect.Method;
32 
33 /**
34  * MethodAccessor adapter for caller-sensitive methods which have
35  * an alternate non-CSM method with the same method name but an additional
36  * caller class argument.
37  *
38  * When a caller-sensitive method is called,
39  * Method::invoke(Object target, Object[] args, Class<?> caller) will
40  * be invoked with the caller class.  If an adapter is present,
41  * the adapter method with the caller class parameter will be called
42  * instead.
43  */
44 class CsMethodAccessorAdapter extends MethodAccessorImpl {
45     private final Method csmAdapter;
46     private final MethodAccessor accessor;
47 
48     CsMethodAccessorAdapter(Method method, Method csmAdapter, MethodAccessor accessor) {
49         assert Reflection.isCallerSensitive(method) && !Reflection.isCallerSensitive(csmAdapter);
50         this.csmAdapter = csmAdapter;
51         this.accessor = accessor;
52     }
53 
54     @Override
55     public Object invoke(Object obj, Object[] args)
56             throws IllegalArgumentException, InvocationTargetException {
57         throw new InternalError("caller-sensitive method invoked without explicit caller: " + csmAdapter);
58     }
59 
60     @Override
61     @ForceInline
62     @Hidden
63     public Object invoke(Object obj, Object[] args, Class<?> caller)
64             throws IllegalArgumentException, InvocationTargetException {
65         Object[] newArgs = new Object[args == null ? 1 : args.length + 1];
66         newArgs[0] = caller;
67         if (args != null) {
68             System.arraycopy(args, 0, newArgs, 1, args.length);
69         }
70         return accessor.invoke(obj, newArgs);
71     }
72 }