/** * Determine whether the given class has a public method with the given signature. * <p>Essentially translates {@code NoSuchMethodException} to "false". * @param clazz the clazz to analyze * @param methodName the name of the method * @param paramTypes the parameter types of the method * @return whether the class has a corresponding method * @see Class#getMethod */ public static boolean hasMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) { return (getMethodIfAvailable(clazz, methodName, paramTypes) != null); }
/** * Determine whether the given class has a public method with the given signature. * <p>Essentially translates {@code NoSuchMethodException} to "false". * @param clazz the clazz to analyze * @param methodName the name of the method * @param paramTypes the parameter types of the method * @return whether the class has a corresponding method * @see Class#getMethod */ public static boolean hasMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) { return (getMethodIfAvailable(clazz, methodName, paramTypes) != null); }
@Nullable private static Method determineToMethod(Class<?> targetClass, Class<?> sourceClass) { if (String.class == targetClass || String.class == sourceClass) { // Do not accept a toString() method or any to methods on String itself return null; } Method method = ClassUtils.getMethodIfAvailable(sourceClass, "to" + targetClass.getSimpleName()); return (method != null && !Modifier.isStatic(method.getModifiers()) && ClassUtils.isAssignable(targetClass, method.getReturnType()) ? method : null); }
/** * Create a new SimpleLoadTimeWeaver for the given class loader. * @param classLoader the {@code ClassLoader} to delegate to for * weaving (<i>must</i> support the required weaving methods). * @throws IllegalStateException if the supplied {@code ClassLoader} * does not support the required weaving methods */ public ReflectiveLoadTimeWeaver(@Nullable ClassLoader classLoader) { Assert.notNull(classLoader, "ClassLoader must not be null"); this.classLoader = classLoader; Method addTransformerMethod = ClassUtils.getMethodIfAvailable( this.classLoader.getClass(), ADD_TRANSFORMER_METHOD_NAME, ClassFileTransformer.class); if (addTransformerMethod == null) { throw new IllegalStateException( "ClassLoader [" + classLoader.getClass().getName() + "] does NOT provide an " + "'addTransformer(ClassFileTransformer)' method."); } this.addTransformerMethod = addTransformerMethod; Method getThrowawayClassLoaderMethod = ClassUtils.getMethodIfAvailable( this.classLoader.getClass(), GET_THROWAWAY_CLASS_LOADER_METHOD_NAME); // getThrowawayClassLoader method is optional if (getThrowawayClassLoaderMethod == null) { if (logger.isDebugEnabled()) { logger.debug("The ClassLoader [" + classLoader.getClass().getName() + "] does NOT provide a " + "'getThrowawayClassLoader()' method; SimpleThrowawayClassLoader will be used instead."); } } this.getThrowawayClassLoaderMethod = getThrowawayClassLoaderMethod; }
this.addTransformerMethod = instrumentableLoaderClass.getMethod("addTransformer", ClassFileTransformer.class); Method copyMethod = ClassUtils.getMethodIfAvailable(instrumentableLoaderClass, "copyWithoutTransformers"); if (copyMethod == null) {
@Nullable private static Method determineToMethod(Class<?> targetClass, Class<?> sourceClass) { if (String.class == targetClass || String.class == sourceClass) { // Do not accept a toString() method or any to methods on String itself return null; } Method method = ClassUtils.getMethodIfAvailable(sourceClass, "to" + targetClass.getSimpleName()); return (method != null && !Modifier.isStatic(method.getModifiers()) && ClassUtils.isAssignable(targetClass, method.getReturnType()) ? method : null); }
public SimpleMethodInvoker(Object object, String methodName, Class<?>... paramTypes) { Assert.notNull(object, "Object to invoke must not be null"); this.method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, paramTypes); if (this.method == null) { // try with no params this.method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName); } if (this.method == null) { throw new IllegalArgumentException("No methods found for name: [" + methodName + "] in class: [" + object.getClass() + "] with arguments of type: [" + Arrays.toString(paramTypes) + "]"); } this.object = object; }
public SimpleMethodInvoker(Object object, String methodName, Class<?>... paramTypes) { Assert.notNull(object, "Object to invoke must not be null"); Method method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, paramTypes); if (method == null) { // try with no params method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, new Class[] {}); } Assert.notNull(method, "No methods found for name: [" + methodName + "] in class: [" + object.getClass() + "] with arguments of type: [" + Arrays.toString(paramTypes) + "]"); this.object = object; this.method = method; method.setAccessible(true); this.parameterTypes = method.getParameterTypes(); }
/** * Create a new SimpleLoadTimeWeaver for the given class loader. * @param classLoader the {@code ClassLoader} to delegate to for * weaving (<i>must</i> support the required weaving methods). * @throws IllegalStateException if the supplied {@code ClassLoader} * does not support the required weaving methods */ public ReflectiveLoadTimeWeaver(@Nullable ClassLoader classLoader) { Assert.notNull(classLoader, "ClassLoader must not be null"); this.classLoader = classLoader; Method addTransformerMethod = ClassUtils.getMethodIfAvailable( this.classLoader.getClass(), ADD_TRANSFORMER_METHOD_NAME, ClassFileTransformer.class); if (addTransformerMethod == null) { throw new IllegalStateException( "ClassLoader [" + classLoader.getClass().getName() + "] does NOT provide an " + "'addTransformer(ClassFileTransformer)' method."); } this.addTransformerMethod = addTransformerMethod; Method getThrowawayClassLoaderMethod = ClassUtils.getMethodIfAvailable( this.classLoader.getClass(), GET_THROWAWAY_CLASS_LOADER_METHOD_NAME); // getThrowawayClassLoader method is optional if (getThrowawayClassLoaderMethod == null) { if (logger.isDebugEnabled()) { logger.debug("The ClassLoader [" + classLoader.getClass().getName() + "] does NOT provide a " + "'getThrowawayClassLoader()' method; SimpleThrowawayClassLoader will be used instead."); } } this.getThrowawayClassLoaderMethod = getThrowawayClassLoaderMethod; }
this.addTransformerMethod = instrumentableLoaderClass.getMethod("addTransformer", ClassFileTransformer.class); Method copyMethod = ClassUtils.getMethodIfAvailable(instrumentableLoaderClass, "copyWithoutTransformers"); if (copyMethod == null) {
Method getProperties = ClassUtils.getMethodIfAvailable(sessionFactory.getClass(), "getProperties"); if (getProperties != null) { Map<?, ?> props = (Map<?, ?>) ReflectionUtils.invokeMethod(getProperties, sessionFactory);
final Method initMethod = (mbd.isNonPublicAccessAllowed() ? BeanUtils.findMethod(bean.getClass(), initMethodName) : ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName));
Method candidate = ClassUtils.getMethodIfAvailable( this.beanClass, "set" + StringUtils.capitalize(getName()), (Class<?>[]) null); if (candidate != null && candidate.getParameterCount() == 1) {
/** * Create a {@link MethodInvoker} using the provided method name to search. * * @param object to be invoked * @param methodName of the method to be invoked * @param paramsRequired boolean indicating whether the parameters are * required, if false, a no args version of the method will be searched for. * @param paramTypes - parameter types of the method to search for. * @return MethodInvoker if the method is found, null if it is not. */ public static MethodInvoker getMethodInvokerByName(Object object, String methodName, boolean paramsRequired, Class<?>... paramTypes) { Assert.notNull(object, "Object to invoke must not be null"); Method method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, paramTypes); if (method == null) { String errorMsg = "no method found with name [" + methodName + "] on class [" + object.getClass().getSimpleName() + "] compatible with the signature [" + getParamTypesString(paramTypes) + "]."; Assert.isTrue(!paramsRequired, errorMsg); // if no method was found for the given parameters, and the // parameters aren't required, then try with no params method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName); Assert.notNull(method, errorMsg); } return new SimpleMethodInvoker(object, method); }
/** * Create a {@link MethodInvoker} using the provided method name to search. * * @param object to be invoked * @param methodName of the method to be invoked * @param paramsRequired boolean indicating whether the parameters are required, if * false, a no args version of the method will be searched for. * @param paramTypes - parameter types of the method to search for. * @return MethodInvoker if the method is found, null if it is not. */ public static MethodInvoker getMethodInvokerByName(Object object, String methodName, boolean paramsRequired, Class<?>... paramTypes) { Assert.notNull(object, "Object to invoke must not be null"); Method method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, paramTypes); if (method == null) { String errorMsg = "no method found with name [" + methodName + "] on class [" + object.getClass().getSimpleName() + "] compatable with the signature [" + getParamTypesString(paramTypes) + "]."; Assert.isTrue(!paramsRequired, errorMsg); // if no method was found for the given parameters, and the // parameters aren't required, then try with no params method = ClassUtils.getMethodIfAvailable(object.getClass(), methodName, new Class[] {}); Assert.notNull(method, errorMsg); } return new SimpleMethodInvoker(object, method); }
@Test public void testGetMethodIfAvailable() { Method method = ClassUtils.getMethodIfAvailable(Collection.class, "size"); assertNotNull(method); assertEquals("size", method.getName()); method = ClassUtils.getMethodIfAvailable(Collection.class, "remove", Object.class); assertNotNull(method); assertEquals("remove", method.getName()); assertNull(ClassUtils.getMethodIfAvailable(Collection.class, "remove")); assertNull(ClassUtils.getMethodIfAvailable(Collection.class, "someOtherMethod")); }
Method getProperties = ClassUtils.getMethodIfAvailable(sessionFactory.getClass(), "getProperties"); if (getProperties != null) { Map<?, ?> props = (Map<?, ?>) ReflectionUtils.invokeMethod(getProperties, sessionFactory);
final Method initMethod = (mbd.isNonPublicAccessAllowed() ? BeanUtils.findMethod(bean.getClass(), initMethodName) : ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName));
Method candidate = ClassUtils.getMethodIfAvailable( this.beanClass, "set" + StringUtils.capitalize(getName()), (Class<?>[]) null); if (candidate != null && candidate.getParameterCount() == 1) {
@Test public void adapterHoldsCorrectData() throws Exception { TargetObject to = new TargetObject(); Method m = ClassUtils.getMethodIfAvailable(TargetObject.class, "countLength", new Class[] { String.class }); when(joinPoint.getTarget()).thenReturn(to); when(joinPoint.getArgs()).thenReturn(new Object[] { "Hi" }); MethodInvocationAdapter mia = new MethodInvocationAdapter(joinPoint); assertThat(mia.getArguments()[0]).isEqualTo("Hi"); assertThat(mia.getStaticPart()).isEqualTo(m); assertThat(mia.getMethod()).isEqualTo(m); assertThat(mia.getThis()).isSameAs(to); }