You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2010/01/13 22:51:05 UTC

svn commit: r898948 - /commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java

Author: simonetripodi
Date: Wed Jan 13 21:51:05 2010
New Revision: 898948

URL: http://svn.apache.org/viewvc?rev=898948&view=rev
Log:
first checkin of MethodHandler class

Added:
    commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java   (with props)

Added: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java
URL: http://svn.apache.org/viewvc/commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java?rev=898948&view=auto
==============================================================================
--- commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java (added)
+++ commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java Wed Jan 13 21:51:05 2010
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.commons.digester.annotations.handlers;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+import org.apache.commons.digester.annotations.CreationRule;
+import org.apache.commons.digester.annotations.DigesterLoader;
+import org.apache.commons.digester.annotations.DigesterLoaderHandler;
+import org.apache.commons.digester.annotations.DigesterRule;
+import org.apache.commons.digester.annotations.DigesterRuleList;
+import org.apache.commons.digester.annotations.FromAnnotationsRuleSet;
+import org.apache.commons.digester.annotations.utils.AnnotationUtils;
+
+/**
+ * Handler that takes care to create the
+ * {@link org.apache.commons.digester.annotations.providers.SetNextRuleProviderRuleProvider}.
+ *
+ * @version $Id$
+ * @since 2.1
+ */
+public final class MethodHandler implements DigesterLoaderHandler<Annotation, Method> {
+
+    /**
+     * The default args size the method has to have in order to be analyzed.
+     */
+    private static final int SUPPORTED_ARGS = 1;
+
+    /**
+     * 
+     */
+    private DefaultLoaderHandler defaultLoaderHandler = new DefaultLoaderHandler();
+
+    /**
+     * {@inheritDoc}
+     */
+    public void handle(Annotation annotation, Method element, FromAnnotationsRuleSet ruleSet) {
+        if (SUPPORTED_ARGS != element.getParameterTypes().length) {
+            DigesterRule rule = annotation.annotationType().getAnnotation(DigesterRule.class);
+
+            throw new IllegalArgumentException("Methods annotated with digester annotation rule @"
+                    + rule.reflectsRule().getName()
+                    + " must have just one argument");
+        }
+
+        Object explicitTypesObject = AnnotationUtils.getAnnotationValue(annotation);
+        if (explicitTypesObject == null
+                || !explicitTypesObject.getClass().isArray()
+                || Class.class != explicitTypesObject.getClass().getComponentType()) {
+            throw new IllegalArgumentException("Impossible to apply this handler, @"
+                    + annotation.getClass().getName()
+                    + ".value() has to be of type 'Class<?>[]'");
+        }
+
+        Class<?>[] explicitTypes = (Class<?>[]) explicitTypesObject;
+        Class<?> paramType = element.getParameterTypes()[0];
+
+        if (explicitTypes.length > 0) {
+            for (Class<?> explicitType : explicitTypes) {
+                if (!paramType.isAssignableFrom(explicitType)) {
+                    throw new IllegalArgumentException("Impossible to handle annotation "
+                            + annotation
+                            + " on method "
+                            + element.toGenericString()
+                            + ", "
+                            + explicitType.getName()
+                            + " has to be a "
+                            + paramType.getName());
+                }
+
+                this.doHandle(element, explicitType, ruleSet);
+            }
+        } else {
+            this.doHandle(element, paramType, ruleSet);
+        }
+    }
+
+    /**
+     * 
+     *
+     * @param method
+     * @param type
+     * @param ruleSet
+     */
+    private void doHandle(Method method, Class<?> type, FromAnnotationsRuleSet ruleSet) {
+        if (type.isInterface()
+                && Modifier.isAbstract(type.getModifiers())) {
+            throw new IllegalArgumentException("");
+        }
+
+        for (Annotation annotation : type.getAnnotations()) {
+            this.doHandle(annotation, method, type, ruleSet);
+        }
+    }
+
+    /**
+     * 
+     *
+     * @param annotation
+     * @param method
+     * @param type
+     * @param ruleSet
+     */
+    private void doHandle(Annotation annotation, Method method, Class<?> type, FromAnnotationsRuleSet ruleSet) {
+        if (annotation.annotationType().isAnnotationPresent(DigesterRule.class)
+                && annotation.annotationType().isAnnotationPresent(CreationRule.class)) {
+            DigesterLoader.addRules(type, ruleSet);
+            this.defaultLoaderHandler.handle(annotation, method, ruleSet);
+        } else if (annotation.annotationType().isAnnotationPresent(DigesterRuleList.class)) {
+            // check if it is one of the *.List annotation
+            Annotation[] annotations = AnnotationUtils.getAnnotationsArrayValue(annotation);
+            if (annotations != null) {
+                // if it is an annotations array, process them
+                for (Annotation ptr : annotations) {
+                    this.doHandle(ptr, method, type, ruleSet);
+                }
+            }
+        }
+    }
+
+}

Propchange: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/handlers/MethodHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain