From 24afbf6059147ac8feaa64087419dd4a48d302e0 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 18 Nov 2008 17:10:35 +0000 Subject: [PATCH] Added HandlerMethodUtils. --- .../handler/HandlerMethodUtils.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 org.springframework.integration/src/main/java/org/springframework/integration/handler/HandlerMethodUtils.java diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/handler/HandlerMethodUtils.java b/org.springframework.integration/src/main/java/org/springframework/integration/handler/HandlerMethodUtils.java new file mode 100644 index 0000000000..876ca9c524 --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/handler/HandlerMethodUtils.java @@ -0,0 +1,80 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.handler; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +import org.springframework.integration.annotation.Header; +import org.springframework.integration.annotation.Headers; + +/** + * Utility methods for common behavior related to Message-handling methods. + * + * @author Mark Fisher + */ +public abstract class HandlerMethodUtils { + + /** + * Verifies that the provided Method is capable of handling Messages. + * It must accept at least one parameter, and if it expects more than one + * parameter, at most one of them may expect the payload object. Others + * must be annotated for accepting Message header values. + */ + public static boolean isValidHandlerMethod(Method method) { + if (method.getDeclaringClass().equals(Object.class)) { + return false; + } + if (!Modifier.isPublic(method.getModifiers())) { + return false; + } + Class[] parameterTypes = method.getParameterTypes(); + if (parameterTypes.length == 0) { + return false; + } + if (parameterTypes.length > 1) { + // at most one parameter can be lacking @Header or @Headers + boolean foundPayloadParam = false; + Annotation[][] allParamAnnotations = method.getParameterAnnotations(); + for (Annotation[] paramAnnotations : allParamAnnotations) { + if (!containsHeaderAnnotation(paramAnnotations)) { + if (foundPayloadParam) { + return false; + } + foundPayloadParam = true; + } + } + } + return true; + } + + /** + * Checks the array of Annotations for a method parameter to see if either + * the @Header or @Headers annotation is present. + */ + public static boolean containsHeaderAnnotation(Annotation[] parameterAnnotations) { + for (Annotation annotation : parameterAnnotations) { + if (annotation.annotationType().equals(Header.class) + || annotation.annotationType().equals(Headers.class)) { + return true; + } + } + return false; + } + +}