1 /*
  2  * Copyright (c) 2015, 2025, 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 
 26 package jdk.internal.access;
 27 
 28 import jdk.internal.foreign.abi.NativeEntryPoint;
 29 
 30 import java.lang.classfile.ClassBuilder;
 31 import java.lang.foreign.MemoryLayout;
 32 import java.lang.invoke.CallSite;
 33 import java.lang.invoke.LambdaConversionException;
 34 import java.lang.invoke.MethodHandle;
 35 import java.lang.invoke.MethodHandles;
 36 import java.lang.invoke.MethodType;
 37 import java.lang.invoke.VarHandle;
 38 import java.lang.reflect.Constructor;
 39 import java.lang.reflect.Field;
 40 import java.nio.ByteOrder;
 41 import java.util.List;
 42 import java.util.Map;
 43 import java.util.function.Function;
 44 import java.util.stream.Stream;
 45 
 46 public interface JavaLangInvokeAccess {
 47     /**
 48      * Returns the declaring class for the given ResolvedMethodName.
 49      * Used by {@code StackFrameInfo}.
 50      */
 51     Class<?> getDeclaringClass(Object rmname);
 52 
 53     /**
 54      * Returns the {@code MethodType} for the given method descriptor
 55      * and class loader.
 56      * Used by {@code StackFrameInfo}.
 57      */
 58     MethodType getMethodType(String descriptor, ClassLoader loader);
 59 
 60     /**
 61      * Returns true if the given flags has MN_CALLER_SENSITIVE flag set.
 62      */
 63     boolean isCallerSensitive(int flags);
 64 
 65     /**
 66      * Returns true if the given flags has MN_HIDDEN_MEMBER flag set.
 67      */
 68     boolean isHiddenMember(int flags);
 69 
 70     /**
 71      * Returns a map of class name in internal forms to its corresponding
 72      * class bytes per the given stream of LF_RESOLVE and SPECIES_RESOLVE
 73      * trace logs. Used by GenerateJLIClassesPlugin to enable generation
 74      * of such classes during the jlink phase.
 75      */
 76     Map<String, byte[]> generateHolderClasses(Stream<String> traces);
 77 
 78     /**
 79      * Returns a var handle view of a given memory segment.
 80      * Used by {@code jdk.internal.foreign.LayoutPath} and
 81      * {@code java.lang.invoke.MethodHandles}.
 82      */
 83     VarHandle memorySegmentViewHandle(Class<?> carrier, MemoryLayout enclosing, long alignmentMask, ByteOrder order, boolean constantOffset, long offset);
 84 
 85     /**
 86      * Var handle carrier combinator.
 87      * Used by {@code java.lang.invoke.MethodHandles}.
 88      */
 89     VarHandle filterValue(VarHandle target, MethodHandle filterToTarget, MethodHandle filterFromTarget);
 90 
 91     /**
 92      * Var handle filter coordinates combinator.
 93      * Used by {@code java.lang.invoke.MethodHandles}.
 94      */
 95     VarHandle filterCoordinates(VarHandle target, int pos, MethodHandle... filters);
 96 
 97     /**
 98      * Var handle drop coordinates combinator.
 99      * Used by {@code java.lang.invoke.MethodHandles}.
100      */
101     VarHandle dropCoordinates(VarHandle target, int pos, Class<?>... valueTypes);
102 
103     /**
104      * Var handle permute coordinates combinator.
105      * Used by {@code java.lang.invoke.MethodHandles}.
106      */
107     VarHandle permuteCoordinates(VarHandle target, List<Class<?>> newCoordinates, int... reorder);
108 
109     /**
110      * Var handle collect coordinates combinator.
111      * Used by {@code java.lang.invoke.MethodHandles}.
112      */
113     VarHandle collectCoordinates(VarHandle target, int pos, MethodHandle filter);
114 
115     /**
116      * Var handle insert coordinates combinator.
117      * Used by {@code java.lang.invoke.MethodHandles}.
118      */
119     VarHandle insertCoordinates(VarHandle target, int pos, Object... values);
120 
121     /**
122      * Returns a native method handle with given arguments as fallback and steering info.
123      *
124      * Will allow JIT to intrinsify.
125      *
126      * @param nep the native entry point
127      * @return the native method handle
128      */
129     MethodHandle nativeMethodHandle(NativeEntryPoint nep);
130 
131     /**
132      * Produces a method handle unreflecting from a {@code Constructor} with
133      * the trusted lookup
134      */
135     MethodHandle unreflectConstructor(Constructor<?> ctor) throws IllegalAccessException;
136 
137     /**
138      * Produces a method handle unreflecting from a {@code Field} with
139      * the trusted lookup
140      */
141     MethodHandle unreflectField(Field field, boolean isSetter) throws IllegalAccessException;
142 
143     /**
144      * Produces a method handle of a virtual method with the trusted lookup.
145      */
146     MethodHandle findVirtual(Class<?> defc, String name, MethodType type) throws IllegalAccessException;
147 
148     /**
149      * Produces a method handle of a static method with the trusted lookup.
150      */
151     MethodHandle findStatic(Class<?> defc, String name, MethodType type) throws IllegalAccessException;
152 
153     /**
154      * Returns a method handle of an invoker class injected for core reflection
155      * implementation with the following signature:
156      *     reflect_invoke_V(MethodHandle mh, Object target, Object[] args)
157      *
158      * The invoker class is a hidden class which has the same
159      * defining class loader, runtime package, and protection domain
160      * as the given caller class.
161      */
162     MethodHandle reflectiveInvoker(Class<?> caller);
163 
164     /**
165      * A best-effort method that tries to find any exceptions thrown by the given method handle.
166      * @param handle the handle to check
167      * @return an array of exceptions, or {@code null}.
168      */
169     Class<?>[] exceptionTypes(MethodHandle handle);
170 
171     /**
172      * Returns a method handle that allocates an instance of the given class
173      * and then invoke the given constructor of one of its superclasses.
174      *
175      * This method should only be used by ReflectionFactory::newConstructorForSerialization.
176      */
177     MethodHandle serializableConstructor(Class<?> decl, Constructor<?> ctorToCall) throws IllegalAccessException;
178 
179     CallSite metafactoryInternal(MethodHandles.Lookup caller,
180                                  String interfaceMethodName,
181                                  MethodType factoryType,
182                                  MethodType interfaceMethodType,
183                                  MethodHandle implementation,
184                                  MethodType dynamicMethodType,
185                                  Function<ClassBuilder, Object> finisher) throws LambdaConversionException;
186 
187     CallSite altMetafactoryInternal(MethodHandles.Lookup caller,
188                                     String interfaceMethodName,
189                                     MethodType factoryType,
190                                     Function<ClassBuilder, Object> finisher,
191                                     Object... args) throws LambdaConversionException;
192 }