Refine search
/** * <p>Invokes a named method whose parameter type matches the object type.</p> * * <p>This method supports calls to methods taking primitive parameters * via passing in wrapping classes. So, for example, a {@code Boolean} object * would match a {@code boolean} primitive.</p> * * <p>This is a convenient wrapper for * {@link #invokeMethod(Object object,boolean forceAccess,String methodName, Object[] args, Class[] parameterTypes)}. * </p> * * @param object invoke method on this object * @param forceAccess force access to invoke method even if it's not accessible * @param methodName get method with this name * @param args use these arguments - treat null as empty array * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the method invoked * @throws IllegalAccessException if the requested method is not accessible via reflection * * @since 3.5 */ public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeMethod(object, forceAccess, methodName, args, parameterTypes); }
MemberUtils.isMatchingMethod(method, parameterTypes)) { final Method accessibleMethod = getAccessibleMethod(method); if (accessibleMethod != null && (bestMatch == null || MemberUtils.compareMethodFit( accessibleMethod, final Class<?>[] methodParameterTypes = bestMatch.getParameterTypes(); final Class<?> methodParameterComponentType = methodParameterTypes[methodParameterTypes.length - 1].getComponentType(); final String methodParameterComponentTypeName = ClassUtils.primitiveToWrapper(methodParameterComponentType).getName(); final String parameterTypeName = parameterTypes[parameterTypes.length - 1].getName(); final String parameterTypeSuperClassName = parameterTypes[parameterTypes.length - 1].getSuperclass().getName();
Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); final Class<?>[] parameterTypes = ClassUtils.toClass(args); return invokeStaticMethod(cls, methodName, args, parameterTypes);
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); parameterTypes = ArrayUtils.nullToEmpty(parameterTypes); final Method method = getMatchingAccessibleMethod(cls, methodName, parameterTypes); if (method == null) { + methodName + "() on class: " + cls.getName()); args = toVarArgs(method, args); return method.invoke(null, args);
Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { parameterTypes = ArrayUtils.nullToEmpty(parameterTypes); args = ArrayUtils.nullToEmpty(args); method = getMatchingMethod(object.getClass(), methodName, parameterTypes); if (method != null && !method.isAccessible()) { method = getMatchingAccessibleMethod(object.getClass(), methodName, parameterTypes); + object.getClass().getName()); args = toVarArgs(method, args);
/** * 匹配模块中复合HTTP请求路径的方法 * 匹配方法的方式是:HttpMethod和HttpPath全匹配 * * @param path HTTP请求路径 * @param httpMethod HTTP请求方法 * @param uniqueId 模块ID * @param classOfModule 模块类 * @return 返回匹配上的方法,如果没有找到匹配方法则返回null */ private Method matchingModuleMethod(final String path, final Http.Method httpMethod, final String uniqueId, final Class<?> classOfModule) { for (final Method method : MethodUtils.getMethodsListWithAnnotation(classOfModule, Http.class)) { final Http httpAnnotation = method.getAnnotation(Http.class); if(null == httpAnnotation) { continue; } final String pathPattern = "/"+uniqueId+httpAnnotation.value(); if (ArrayUtils.contains(httpAnnotation.method(), httpMethod) && matching(path, pathPattern)) { return method; } } // 找不到匹配方法,返回null return null; }
/** * <p>Invokes a named method without parameters.</p> * * <p>This is a convenient wrapper for * {@link #invokeMethod(Object object,boolean forceAccess,String methodName, Object[] args, Class[] parameterTypes)}. * </p> * * @param object invoke method on this object * @param forceAccess force access to invoke method even if it's not accessible * @param methodName get method with this name * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the method invoked * @throws IllegalAccessException if the requested method is not accessible via reflection * * @since 3.5 */ public static Object invokeMethod(final Object object, final boolean forceAccess, final String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return invokeMethod(object, forceAccess, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY, null); }
/** * Checks whether a method for the passed in parameters can be found. * * @param method the listener method invoked * @return a flag whether the parameters could be matched */ private boolean hasMatchingParametersMethod(final Method method) { return MethodUtils.getAccessibleMethod(target.getClass(), methodName, method.getParameterTypes()) != null; } }
private void cleanSpy() throws ClassNotFoundException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { final String namespace = configInfo.getNamespace(); MethodUtils.invokeStaticMethod( getClass().getClassLoader().loadClass("java.com.alibaba.jvm.sandbox.spy.Spy"), "clean", namespace ); logger.info("clean Spy's method from jvm-sandbox[{}] success, for shutdown.", namespace); }
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { args = ArrayUtils.nullToEmpty(args); parameterTypes = ArrayUtils.nullToEmpty(parameterTypes); final Method method = getAccessibleMethod(cls, methodName, parameterTypes); if (method == null) { throw new NoSuchMethodException("No such accessible method: "
/** * Gets all class level public methods of the given class that are annotated with the given annotation. * @param cls * the {@link Class} to query * @param annotationCls * the {@link java.lang.annotation.Annotation} that must be present on a method to be matched * @return an array of Methods (possibly empty). * @throws IllegalArgumentException * if the class or annotation are {@code null} * @since 3.4 */ public static Method[] getMethodsWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) { return getMethodsWithAnnotation(cls, annotationCls, false, false); }
@Test public void testDistance() throws Exception { final Method distanceMethod = MethodUtils.getMatchingMethod(MethodUtils.class, "distance", Class[].class, Class[].class); distanceMethod.setAccessible(true); assertEquals(-1, distanceMethod.invoke(null, new Class[]{String.class}, new Class[]{Date.class})); assertEquals(0, distanceMethod.invoke(null, new Class[]{Date.class}, new Class[]{Date.class})); assertEquals(1, distanceMethod.invoke(null, new Class[]{Integer.class}, new Class[]{ClassUtils.wrapperToPrimitive(Integer.class)})); assertEquals(2, distanceMethod.invoke(null, new Class[]{Integer.class}, new Class[]{Object.class})); distanceMethod.setAccessible(false); } }
@Test public void testGetOverrideHierarchyExcludingInterfaces() { final Method method = MethodUtils.getAccessibleMethod(StringParameterizedChild.class, "consume", String.class); final Iterator<MethodDescriptor> expected = Arrays.asList(new MethodDescriptor(StringParameterizedChild.class, "consume", String.class), new MethodDescriptor(GenericParent.class, "consume", GenericParent.class.getTypeParameters()[0])) .iterator(); for (final Method m : MethodUtils.getOverrideHierarchy(method, Interfaces.EXCLUDE)) { assertTrue(expected.hasNext()); final MethodDescriptor md = expected.next(); assertEquals(md.declaringClass, m.getDeclaringClass()); assertEquals(md.name, m.getName()); assertEquals(md.parameterTypes.length, m.getParameterTypes().length); for (int i = 0; i < md.parameterTypes.length; i++) { assertTrue(TypeUtils.equals(md.parameterTypes[i], m.getGenericParameterTypes()[i])); } } assertFalse(expected.hasNext()); }
/** * Gets all class level public methods of the given class that are annotated with the given annotation. * @param cls * the {@link Class} to query * @param annotationCls * the {@link Annotation} that must be present on a method to be matched * @return a list of Methods (possibly empty). * @throws IllegalArgumentException * if the class or annotation are {@code null} * @since 3.4 */ public static List<Method> getMethodsListWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) { return getMethodsListWithAnnotation(cls, annotationCls, false, false); }
final Iterator<Class<?>> hierarchy = ClassUtils.hierarchy(declaringClass, interfacesBehavior).iterator(); final Method m = getMatchingAccessibleMethod(c, method.getName(), parameterTypes); if (m == null) { continue;
BuiltInMethod(Class<?> clazz, String methodName, Class<?>... argumentTypes) { this.method = MethodUtils.getMatchingAccessibleMethod(clazz, methodName, argumentTypes); }
/** * 循环向上转型, 获取对象的DeclaredMethod, 并强制设置为可访问. * * 如向上转型到Object仍无法找到, 返回null. * * 匹配函数名+参数类型. * * 方法需要被多次调用时,先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args) * * 因为getMethod() 不能获取父类的private函数, 因此采用循环向上的getDeclaredMethod(); */ public static Method getMethod(final Class<?> clazz, final String methodName, Class<?>... parameterTypes) { Method method = MethodUtils.getMatchingMethod(clazz, methodName, parameterTypes); if (method != null) { makeAccessible(method); } return method; }
/** * <p>Invokes a named method without parameters.</p> * * <p>This method delegates the method search to {@link #getMatchingAccessibleMethod(Class, String, Class[])}.</p> * * <p>This is a convenient wrapper for * {@link #invokeMethod(Object object,String methodName, Object[] args, Class[] parameterTypes)}. * </p> * * @param object invoke method on this object * @param methodName get method with this name * @return The value returned by the invoked method * * @throws NoSuchMethodException if there is no such accessible method * @throws InvocationTargetException wraps an exception thrown by the method invoked * @throws IllegalAccessException if the requested method is not accessible via reflection * * @since 3.4 */ public static Object invokeMethod(final Object object, final String methodName) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { return invokeMethod(object, methodName, ArrayUtils.EMPTY_OBJECT_ARRAY, null); }
Object[] args, Class<?>[] parameterTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { parameterTypes = ArrayUtils.nullToEmpty(parameterTypes); args = ArrayUtils.nullToEmpty(args); method = getMatchingMethod(object.getClass(), methodName, parameterTypes); if (method != null && !method.isAccessible()) { method = getMatchingAccessibleMethod(object.getClass(), methodName, parameterTypes); + object.getClass().getName()); args = toVarArgs(method, args);
/** * <p>Returns an accessible method (that is, one that can be invoked via * reflection) with given name and parameters. If no such method * can be found, return {@code null}. * This is just a convenience wrapper for * {@link #getAccessibleMethod(Method)}.</p> * * @param cls get method from this class * @param methodName get method with this name * @param parameterTypes with these parameters types * @return The accessible method */ public static Method getAccessibleMethod(final Class<?> cls, final String methodName, final Class<?>... parameterTypes) { try { return getAccessibleMethod(cls.getMethod(methodName, parameterTypes)); } catch (final NoSuchMethodException e) { return null; } }