< prev index next >

src/java.base/share/classes/jdk/internal/foreign/Util.java

Print this page




 378             try {
 379                 //address
 380                 return longFunction.apply(p.addr());
 381             } catch (UnsupportedOperationException ex) {
 382                 //heap pointer
 383                 try (Scope sc = Scope.globalScope().fork()) {
 384                     Pointer<?> offheapPtr = sc.allocate(p.type());
 385                     Pointer.copy(p, offheapPtr, p.type().bytesSize());
 386                     Z z = longFunction.apply(offheapPtr.addr());
 387                     Pointer.copy(offheapPtr, p, p.type().bytesSize());
 388                     return z;
 389                 }
 390             }
 391         } catch (Throwable ex) {
 392             throw new IllegalStateException(ex);
 393         }
 394     }
 395 
 396     public static MethodHandle getCallbackMH(Method m) {
 397         try {
 398             MethodHandle mh = MethodHandles.publicLookup().unreflect(m);

 399             Util.checkNoArrays(mh.type());
 400             return mh;
 401         } catch (Throwable ex) {
 402             throw new IllegalStateException(ex);
 403         }
 404     }
 405 
 406     public static Function getResolvedFunction(Class<?> nativeCallback, Method m) {
 407         LayoutResolver resolver = LayoutResolver.get(nativeCallback);
 408         return resolver.resolve(Util.functionof(nativeCallback));
 409     }
 410 
 411     public static NativeMethodType nativeMethodType(Function function, Method method) {
 412         checkCompatible(method, function);
 413 
 414         LayoutType<?> ret = function.returnLayout()
 415                 .<LayoutType<?>>map(l -> makeType(method.getGenericReturnType(), l))
 416                 .orElse(NativeTypes.VOID);
 417 
 418         // Use function argument size and ignore last argument from method for vararg function




 378             try {
 379                 //address
 380                 return longFunction.apply(p.addr());
 381             } catch (UnsupportedOperationException ex) {
 382                 //heap pointer
 383                 try (Scope sc = Scope.globalScope().fork()) {
 384                     Pointer<?> offheapPtr = sc.allocate(p.type());
 385                     Pointer.copy(p, offheapPtr, p.type().bytesSize());
 386                     Z z = longFunction.apply(offheapPtr.addr());
 387                     Pointer.copy(offheapPtr, p, p.type().bytesSize());
 388                     return z;
 389                 }
 390             }
 391         } catch (Throwable ex) {
 392             throw new IllegalStateException(ex);
 393         }
 394     }
 395 
 396     public static MethodHandle getCallbackMH(Method m) {
 397         try {
 398             m.setAccessible(true);
 399             MethodHandle mh = MethodHandles.lookup().unreflect(m);
 400             Util.checkNoArrays(mh.type());
 401             return mh;
 402         } catch (Throwable ex) {
 403             throw new IllegalStateException(ex);
 404         }
 405     }
 406 
 407     public static Function getResolvedFunction(Class<?> nativeCallback, Method m) {
 408         LayoutResolver resolver = LayoutResolver.get(nativeCallback);
 409         return resolver.resolve(Util.functionof(nativeCallback));
 410     }
 411 
 412     public static NativeMethodType nativeMethodType(Function function, Method method) {
 413         checkCompatible(method, function);
 414 
 415         LayoutType<?> ret = function.returnLayout()
 416                 .<LayoutType<?>>map(l -> makeType(method.getGenericReturnType(), l))
 417                 .orElse(NativeTypes.VOID);
 418 
 419         // Use function argument size and ignore last argument from method for vararg function


< prev index next >