/** * {@inheritDoc} */ @SuppressWarnings("unchecked") public T classify(C classifiable) { if (classifier != null) { return classifier.classify(classifiable); } return (T) invoker.invokeMethod(classifiable); }
/** * Search for the * {@link org.springframework.classify.annotation.Classifier * Classifier} annotation on a method in the supplied delegate and use that * to create a {@link Classifier} from the parameter type to the return * type. If the annotation is not found a unique non-void method with a * single parameter will be used, if it exists. The signature of the method * cannot be checked here, so might be a runtime exception when the method * is invoked if the signature doesn't match the classifier types. * * @param delegate an object with an annotated method */ public final void setDelegate(Object delegate) { classifier = null; invoker = MethodInvokerUtils.getMethodInvokerByAnnotation( org.springframework.classify.annotation.Classifier.class, delegate); if (invoker == null) { invoker = MethodInvokerUtils.<C, T> getMethodInvokerForSingleArgument(delegate); } Assert.state(invoker != null, "No single argument public method with or without " + "@Classifier was found in delegate of type " + delegate.getClass()); }
/** * Create a {@link MethodInvoker} using the provided interface, and method name from * that interface. * * @param cls the interface to search for the method named * @param methodName of the method to be invoked * @param object to be invoked * @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 getMethodInvokerForInterface(Class<?> cls, String methodName, Object object, Class<?>... paramTypes) { if (cls.isAssignableFrom(object.getClass())) { return MethodInvokerUtils.getMethodInvokerByName(object, methodName, true, paramTypes); } else { return null; } }
/** * 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); }
@Override public Object invokeMethod(Object... args) { Assert.state(this.parameterTypes.length == args.length, "Wrong number of arguments, expected no more than: [" + this.parameterTypes.length + "]"); try { // Extract the target from an Advised as late as possible // in case it contains a lazy initialization Object target = extractTarget(this.object, this.method); return method.invoke(target, args); } catch (Exception e) { throw new IllegalArgumentException("Unable to invoke method: [" + this.method + "] on object: [" + this.object + "] with arguments: [" + Arrays.toString(args) + "]", e); } }
return new SimpleMethodInvoker(target, method);
/** * Find a <em>single</em> Method on the Class of the given candidate object * that contains the annotation type for which this resolver is searching. * * @param candidate the instance whose Class will be checked for the * annotation * * @return a single matching Method instance or <code>null</code> if the * candidate's Class contains no Methods with the specified annotation * * @throws IllegalArgumentException if more than one Method has the * specified annotation */ public Method findMethod(Object candidate) { Assert.notNull(candidate, "candidate object must not be null"); Class<?> targetClass = AopUtils.getTargetClass(candidate); if (targetClass == null) { targetClass = candidate.getClass(); } return this.findMethod(targetClass); }
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType); if (annotation != null) { Class<?>[] paramTypes = method.getParameterTypes(); if (paramTypes.length > 0) { String errorMsg = "The method [" + method.getName() + "] on target class [" + targetClass.getSimpleName() + "] is incompatable with the signature [" + getParamTypesString(expectedParamTypes) + "] expected for the annotation [" + annotationType.getSimpleName() + "]."; Assert.isTrue( paramTypes.length == expectedParamTypes.length, errorMsg); for (int i = 0; i < paramTypes.length; i++) { Assert.isTrue(expectedParamTypes[i] .isAssignableFrom(paramTypes[i]), errorMsg); } } } } });
final Class<? extends Annotation> annotationType, final Object target, final Class<?>... expectedParamTypes) { MethodInvoker mi = MethodInvokerUtils.getMethodInvokerByAnnotation(annotationType, target); final Class<?> targetClass = (target instanceof Advised)
/** * Search for the * {@link org.springframework.classify.annotation.Classifier * Classifier} annotation on a method in the supplied delegate and use that * to create a {@link Classifier} from the parameter type to the return * type. If the annotation is not found a unique non-void method with a * single parameter will be used, if it exists. The signature of the method * cannot be checked here, so might be a runtime exception when the method * is invoked if the signature doesn't match the classifier types. * * @param delegate an object with an annotated method */ public final void setDelegate(Object delegate) { classifier = null; invoker = MethodInvokerUtils.getMethodInvokerByAnnotation( org.springframework.classify.annotation.Classifier.class, delegate); if (invoker == null) { invoker = MethodInvokerUtils.<C, T> getMethodInvokerForSingleArgument(delegate); } Assert.state(invoker != null, "No single argument public method with or without " + "@Classifier was found in delegate of type " + delegate.getClass()); }
/** * 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); }
private Object extractTarget(Object target, Method method) { if (this.target == null) { if (target instanceof Advised) { Object source; try { source = ((Advised) target).getTargetSource().getTarget(); } catch (Exception e) { throw new IllegalStateException("Could not extract target from proxy", e); } if (source instanceof Advised) { source = extractTarget(source, method); } if (method.getDeclaringClass().isAssignableFrom(source.getClass())) { target = source; } } this.target = target; } return this.target; }
return new SimpleMethodInvoker(target, annotatedMethod.get());
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") public T classify(C classifiable) { if (classifier != null) { return classifier.classify(classifiable); } return (T) invoker.invokeMethod(classifiable); }
/** * Create a {@link MethodInvoker} using the provided interface, and method name from * that interface. * * @param cls the interface to search for the method named * @param methodName of the method to be invoked * @param object to be invoked * @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 getMethodInvokerForInterface(Class<?> cls, String methodName, Object object, Class<?>... paramTypes) { if (cls.isAssignableFrom(object.getClass())) { return MethodInvokerUtils.getMethodInvokerByName(object, methodName, true, paramTypes); } else { return null; } }
/** * Find a <em>single</em> Method on the Class of the given candidate object * that contains the annotation type for which this resolver is searching. * * @param candidate the instance whose Class will be checked for the * annotation * * @return a single matching Method instance or <code>null</code> if the * candidate's Class contains no Methods with the specified annotation * * @throws IllegalArgumentException if more than one Method has the * specified annotation */ public Method findMethod(Object candidate) { Assert.notNull(candidate, "candidate object must not be null"); Class<?> targetClass = AopUtils.getTargetClass(candidate); if (targetClass == null) { targetClass = candidate.getClass(); } return this.findMethod(targetClass); }
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType); if (annotation != null) { Class<?>[] paramTypes = method.getParameterTypes(); if (paramTypes.length > 0) { String errorMsg = "The method [" + method.getName() + "] on target class [" + targetClass.getSimpleName() + "] is incompatable with the signature [" + getParamTypesString(expectedParamTypes) + "] expected for the annotation [" + annotationType.getSimpleName() + "]."; Assert.isTrue( paramTypes.length == expectedParamTypes.length, errorMsg); for (int i = 0; i < paramTypes.length; i++) { Assert.isTrue(expectedParamTypes[i] .isAssignableFrom(paramTypes[i]), errorMsg); } } } } });
@Override public Object invokeMethod(Object... args) { Assert.state(this.parameterTypes.length == args.length, "Wrong number of arguments, expected no more than: [" + this.parameterTypes.length + "]"); try { // Extract the target from an Advised as late as possible // in case it contains a lazy initialization Object target = extractTarget(this.object, this.method); return method.invoke(target, args); } catch (Exception e) { throw new IllegalArgumentException("Unable to invoke method: [" + this.method + "] on object: [" + this.object + "] with arguments: [" + Arrays.toString(args) + "]", e); } }
return new SimpleMethodInvoker(target, method);
private Object extractTarget(Object target, Method method) { if (this.target == null) { if (target instanceof Advised) { Object source; try { source = ((Advised) target).getTargetSource().getTarget(); } catch (Exception e) { throw new IllegalStateException("Could not extract target from proxy", e); } if (source instanceof Advised) { source = extractTarget(source, method); } if (method.getDeclaringClass().isAssignableFrom(source.getClass())) { target = source; } } this.target = target; } return this.target; }