java.lang.invoke.MethodHandle.invokeWithArguments()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(12.6k)|赞(0)|评价(0)|浏览(326)

本文整理了Java中java.lang.invoke.MethodHandle.invokeWithArguments()方法的一些代码示例,展示了MethodHandle.invokeWithArguments()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MethodHandle.invokeWithArguments()方法的具体详情如下:
包路径:java.lang.invoke.MethodHandle
类名称:MethodHandle
方法名:invokeWithArguments

MethodHandle.invokeWithArguments介绍

[英]Performs a variable arity invocation, passing the arguments in the given array to the method handle, as if via an inexact #invoke from a call site which mentions only the type Object, and whose arity is the length of the argument array.

This method is also equivalent to the following code:

  1. invokeWithArguments(arguments.toArray()

[中]执行变量arity调用,将给定数组中的参数传递给方法句柄,就像通过调用站点的不精确#调用一样,调用站点只提到类型对象,其arity是参数数组的长度。
此方法也相当于以下代码:

  1. invokeWithArguments(arguments.toArray()

代码示例

代码示例来源:origin: org.codehaus.groovy/groovy

  1. @Override
  2. public Object invokeHandle(Object handle, Object[] args) throws Throwable {
  3. MethodHandle mh = (MethodHandle) handle;
  4. return mh.invokeWithArguments(args);
  5. }
  6. }

代码示例来源:origin: square/retrofit

  1. @Override Object invokeDefaultMethod(Method method, Class<?> declaringClass, Object object,
  2. @Nullable Object... args) throws Throwable {
  3. // Because the service interface might not be public, we need to use a MethodHandle lookup
  4. // that ignores the visibility of the declaringClass.
  5. Constructor<Lookup> constructor = Lookup.class.getDeclaredConstructor(Class.class, int.class);
  6. constructor.setAccessible(true);
  7. return constructor.newInstance(declaringClass, -1 /* trusted */)
  8. .unreflectSpecial(method, declaringClass)
  9. .bindTo(object)
  10. .invokeWithArguments(args);
  11. }

代码示例来源:origin: org.objenesis/objenesis

  1. @Override
  2. Class<?> defineClass(String className, byte[] b, int off, int len, Class<?> neighbor, ClassLoader loader, ProtectionDomain protectionDomain) {
  3. try {
  4. Object module = getModule.invokeWithArguments(DefineClassHelper.class);
  5. Object neighborModule = getModule.invokeWithArguments(neighbor);
  6. addReads.invokeWithArguments(module, neighborModule);
  7. MethodHandles.Lookup prvlookup = (MethodHandles.Lookup) privateLookupIn.invokeExact(neighbor, lookup);
  8. return (Class<?>) defineClass.invokeExact(prvlookup, b);
  9. } catch (Throwable e) {
  10. throw new ObjenesisException(neighbor.getName() + " has no permission to define the class", e);
  11. }
  12. }
  13. }

代码示例来源:origin: baomidou/mybatis-plus

  1. private Object invokeDefaultMethod(Object proxy, Method method, Object[] args)
  2. throws Throwable {
  3. final Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class
  4. .getDeclaredConstructor(Class.class, int.class);
  5. if (!constructor.isAccessible()) {
  6. constructor.setAccessible(true);
  7. }
  8. final Class<?> declaringClass = method.getDeclaringClass();
  9. return constructor
  10. .newInstance(declaringClass,
  11. MethodHandles.Lookup.PRIVATE | MethodHandles.Lookup.PROTECTED
  12. | MethodHandles.Lookup.PACKAGE | MethodHandles.Lookup.PUBLIC)
  13. .unreflectSpecial(method, declaringClass).bindTo(proxy).invokeWithArguments(args);
  14. }

代码示例来源:origin: runelite/runelite

  1. static Object callDefaultMethod(Object proxy, Method method, Object[] args) throws Throwable
  2. {
  3. // Call the default method implementation - https://rmannibucau.wordpress.com/2014/03/27/java-8-default-interface-methods-and-jdk-dynamic-proxies/
  4. Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class.getDeclaredConstructor(Class.class, int.class);
  5. constructor.setAccessible(true);
  6. Class<?> declaringClass = method.getDeclaringClass();
  7. return constructor.newInstance(declaringClass, MethodHandles.Lookup.PUBLIC | MethodHandles.Lookup.PRIVATE)
  8. .unreflectSpecial(method, declaringClass)
  9. .bindTo(proxy)
  10. .invokeWithArguments(args);
  11. }
  12. }

代码示例来源:origin: com.squareup.retrofit2/retrofit

  1. @Override Object invokeDefaultMethod(Method method, Class<?> declaringClass, Object object,
  2. @Nullable Object... args) throws Throwable {
  3. // Because the service interface might not be public, we need to use a MethodHandle lookup
  4. // that ignores the visibility of the declaringClass.
  5. Constructor<Lookup> constructor = Lookup.class.getDeclaredConstructor(Class.class, int.class);
  6. constructor.setAccessible(true);
  7. return constructor.newInstance(declaringClass, -1 /* trusted */)
  8. .unreflectSpecial(method, declaringClass)
  9. .bindTo(object)
  10. .invokeWithArguments(args);
  11. }

代码示例来源:origin: org.javassist/javassist

  1. @Override
  2. Package definePackage(ClassLoader loader, String name, String specTitle,
  3. String specVersion, String specVendor, String implTitle, String implVersion,
  4. String implVendor, URL sealBase)
  5. throws IllegalArgumentException
  6. {
  7. if (stack.getCallerClass() != DefinePackageHelper.class)
  8. throw new IllegalAccessError("Access denied for caller.");
  9. try {
  10. return (Package) definePackage.invokeWithArguments(loader, name, specTitle,
  11. specVersion, specVendor, implTitle, implVersion, implVendor, sealBase);
  12. } catch (Throwable e) {
  13. if (e instanceof IllegalArgumentException) throw (IllegalArgumentException) e;
  14. if (e instanceof RuntimeException) throw (RuntimeException) e;
  15. }
  16. return null;
  17. }
  18. }

代码示例来源:origin: scouter-project/scouter

  1. @Override
  2. Package definePackage(ClassLoader loader, String name, String specTitle,
  3. String specVersion, String specVendor, String implTitle, String implVersion,
  4. String implVendor, URL sealBase)
  5. throws IllegalArgumentException
  6. {
  7. if (stack.getCallerClass() != DefinePackageHelper.class)
  8. throw new IllegalAccessError("Access denied for caller.");
  9. try {
  10. return (Package) definePackage.invokeWithArguments(loader, name, specTitle,
  11. specVersion, specVendor, implTitle, implVersion, implVendor, sealBase);
  12. } catch (Throwable e) {
  13. if (e instanceof IllegalArgumentException) throw (IllegalArgumentException) e;
  14. if (e instanceof RuntimeException) throw (RuntimeException) e;
  15. }
  16. return null;
  17. }
  18. }

代码示例来源:origin: jdbi/jdbi

  1. private static <T extends SqlParser> Supplier<T> tryConstructor(Class<T> clazz, Object... args) {
  2. return () -> {
  3. try {
  4. Object[] nonNullArgs = Arrays.stream(args).filter(Objects::nonNull).toArray(Object[]::new);
  5. Class[] argClasses = Arrays.stream(nonNullArgs).map(Object::getClass).toArray(Class[]::new);
  6. MethodType type = MethodType.methodType(void.class, argClasses);
  7. return (T) MethodHandles.lookup().findConstructor(clazz, type).invokeWithArguments(nonNullArgs);
  8. } catch (NoSuchMethodException ignored) {
  9. return null;
  10. } catch (Throwable t) {
  11. throw Sneaky.throwAnyway(t);
  12. }
  13. };
  14. }
  15. }

代码示例来源:origin: org.javassist/javassist

  1. Class<?> defineClass(String name, byte[] b, int off, int len,
  2. ClassLoader loader, ProtectionDomain protectionDomain)
  3. throws ClassFormatError
  4. {
  5. try {
  6. if (getCallerClass.invoke(stack) != Java9.class)
  7. throw new IllegalAccessError("Access denied for caller.");
  8. } catch (Exception e) {
  9. throw new RuntimeException("cannot initialize", e);
  10. }
  11. try {
  12. return (Class<?>) defineClass.invokeWithArguments(
  13. sunMiscUnsafeTheUnsafe.theUnsafe,
  14. name, b, off, len, loader, protectionDomain);
  15. } catch (Throwable e) {
  16. if (e instanceof RuntimeException) throw (RuntimeException) e;
  17. if (e instanceof ClassFormatError) throw (ClassFormatError) e;
  18. throw new ClassFormatError(e.getMessage());
  19. }
  20. }
  21. }

代码示例来源:origin: jdbi/jdbi

  1. private static <T extends TemplateEngine> Supplier<T> tryConstructor(Class<T> clazz, Object... args) {
  2. return () -> {
  3. try {
  4. Object[] nonNullArgs = Arrays.stream(args).filter(Objects::nonNull).toArray(Object[]::new);
  5. Class[] argClasses = Arrays.stream(nonNullArgs).map(Object::getClass).toArray(Class[]::new);
  6. MethodType type = MethodType.methodType(void.class, argClasses);
  7. return (T) MethodHandles.lookup().findConstructor(clazz, type).invokeWithArguments(nonNullArgs);
  8. } catch (NoSuchMethodException ignored) {
  9. return null;
  10. } catch (Throwable t) {
  11. throw Sneaky.throwAnyway(t);
  12. }
  13. };
  14. }
  15. }

代码示例来源:origin: oracle/helidon

  1. public T create(Config configNode) {
  2. List<Object> args = createArguments(configNode);
  3. try {
  4. Object obj = handle.invokeWithArguments(args);
  5. return type.cast(obj);
  6. } catch (ConfigException ex) {
  7. throw ex;
  8. } catch (Throwable throwable) {
  9. throw new ConfigException("Unable to create '" + type.getName() + "' instance.", throwable);
  10. }
  11. }

代码示例来源:origin: org.javassist/javassist

  1. @Override
  2. Class<?> defineClass(String name, byte[] b, int off, int len, Class<?> neighbor,
  3. ClassLoader loader, ProtectionDomain protectionDomain)
  4. throws ClassFormatError
  5. {
  6. if (stack.getCallerClass() != DefineClassHelper.class)
  7. throw new IllegalAccessError("Access denied for caller.");
  8. try {
  9. return (Class<?>) defineClass.invokeWithArguments(
  10. loader, name, b, off, len, protectionDomain);
  11. } catch (Throwable e) {
  12. if (e instanceof RuntimeException) throw (RuntimeException) e;
  13. if (e instanceof ClassFormatError) throw (ClassFormatError) e;
  14. throw new ClassFormatError(e.getMessage());
  15. }
  16. }
  17. }

代码示例来源:origin: scouter-project/scouter

  1. @Override
  2. Class<?> defineClass(String name, byte[] b, int off, int len, Class<?> neighbor,
  3. ClassLoader loader, ProtectionDomain protectionDomain)
  4. throws ClassFormatError
  5. {
  6. if (stack.getCallerClass() != DefineClassHelper.class)
  7. throw new IllegalAccessError("Access denied for caller.");
  8. try {
  9. return (Class<?>) defineClass.invokeWithArguments(
  10. loader, name, b, off, len, protectionDomain);
  11. } catch (Throwable e) {
  12. if (e instanceof RuntimeException) throw (RuntimeException) e;
  13. if (e instanceof ClassFormatError) throw (ClassFormatError) e;
  14. throw new ClassFormatError(e.getMessage());
  15. }
  16. }
  17. }

代码示例来源:origin: prestodb/presto

  1. private static long getReferenceCountMapOverhead(TestComplexState state)
  2. {
  3. long overhead = 0;
  4. // reflection is necessary because TestComplexState implementation is generated
  5. Field[] stateFields = state.getClass().getDeclaredFields();
  6. try {
  7. for (Field stateField : stateFields) {
  8. if (stateField.getType() != BlockBigArray.class && stateField.getType() != SliceBigArray.class) {
  9. continue;
  10. }
  11. stateField.setAccessible(true);
  12. Field[] bigArrayFields = stateField.getType().getDeclaredFields();
  13. for (Field bigArrayField : bigArrayFields) {
  14. if (bigArrayField.getType() != ReferenceCountMap.class) {
  15. continue;
  16. }
  17. bigArrayField.setAccessible(true);
  18. MethodHandle sizeOf = Reflection.methodHandle(bigArrayField.getType(), "sizeOf");
  19. overhead += (long) sizeOf.invokeWithArguments(bigArrayField.get(stateField.get(state)));
  20. }
  21. }
  22. }
  23. catch (Throwable t) {
  24. throw new RuntimeException(t);
  25. }
  26. return overhead;
  27. }

代码示例来源:origin: robolectric/robolectric

  1. @SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})
  2. private static <T> T invokeDynamic(
  3. Class<?> cls, String methodName, Class<?> returnType, ClassParameter<?>... params)
  4. throws Throwable {
  5. MethodType methodType =
  6. MethodType.methodType(
  7. returnType, Arrays.stream(params).map(param -> param.clazz).toArray(Class[]::new));
  8. CallSite callsite =
  9. InvokeDynamicSupport.bootstrapIntrinsic(
  10. MethodHandles.lookup(), methodName, methodType, cls.getName());
  11. return (T)
  12. callsite
  13. .dynamicInvoker()
  14. .invokeWithArguments(
  15. Arrays.stream(params).map(param -> param.val).collect(Collectors.toList()));
  16. }
  17. }

代码示例来源:origin: prestodb/presto

  1. private long getComplexStateRetainedSize(TestComplexState state)
  2. {
  3. long retainedSize = ClassLayout.parseClass(state.getClass()).instanceSize();
  4. // reflection is necessary because TestComplexState implementation is generated
  5. Field[] fields = state.getClass().getDeclaredFields();
  6. try {
  7. for (Field field : fields) {
  8. Class type = field.getType();
  9. field.setAccessible(true);
  10. if (type == BlockBigArray.class || type == BooleanBigArray.class || type == SliceBigArray.class ||
  11. type == ByteBigArray.class || type == DoubleBigArray.class || type == LongBigArray.class || type == IntBigArray.class) {
  12. MethodHandle sizeOf = Reflection.methodHandle(type, "sizeOf");
  13. retainedSize += (long) sizeOf.invokeWithArguments(field.get(state));
  14. }
  15. }
  16. }
  17. catch (Throwable t) {
  18. throw new RuntimeException(t);
  19. }
  20. return retainedSize;
  21. }

代码示例来源:origin: lettuce-io/lettuce-core

  1. @Override
  2. public Object invoke(MethodInvocation invocation) throws Throwable {
  3. Method method = invocation.getMethod();
  4. if (!method.isDefault()) {
  5. return invocation.proceed();
  6. }
  7. LettuceAssert.isTrue(invocation instanceof InvocationTargetProvider,
  8. "Invocation must provide a target object via InvocationTargetProvider");
  9. InvocationTargetProvider targetProvider = (InvocationTargetProvider) invocation;
  10. return methodHandleCache.computeIfAbsent(method, DefaultMethodInvokingInterceptor::lookupMethodHandle)
  11. .bindTo(targetProvider.getInvocationTarget()).invokeWithArguments(invocation.getArguments());
  12. }

代码示例来源:origin: prestodb/presto

  1. return method.invokeWithArguments(actualArguments);

代码示例来源:origin: prestodb/presto

  1. @Override
  2. protected Object visitArithmeticUnary(ArithmeticUnaryExpression node, Object context)
  3. {
  4. Object value = process(node.getValue(), context);
  5. if (value == null) {
  6. return null;
  7. }
  8. if (value instanceof Expression) {
  9. return new ArithmeticUnaryExpression(node.getSign(), toExpression(value, type(node.getValue())));
  10. }
  11. switch (node.getSign()) {
  12. case PLUS:
  13. return value;
  14. case MINUS:
  15. Signature operatorSignature = metadata.getFunctionRegistry().resolveOperator(OperatorType.NEGATION, types(node.getValue()));
  16. MethodHandle handle = metadata.getFunctionRegistry().getScalarFunctionImplementation(operatorSignature).getMethodHandle();
  17. if (handle.type().parameterCount() > 0 && handle.type().parameterType(0) == ConnectorSession.class) {
  18. handle = handle.bindTo(session);
  19. }
  20. try {
  21. return handle.invokeWithArguments(value);
  22. }
  23. catch (Throwable throwable) {
  24. throwIfInstanceOf(throwable, RuntimeException.class);
  25. throwIfInstanceOf(throwable, Error.class);
  26. throw new RuntimeException(throwable.getMessage(), throwable);
  27. }
  28. }
  29. throw new UnsupportedOperationException("Unsupported unary operator: " + node.getSign());
  30. }

相关文章