commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r897402 - in /commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils: ./ AnnotationUtils.java
Date Sat, 09 Jan 2010 09:59:59 GMT
Author: simonetripodi
Date: Sat Jan  9 09:59:58 2010
New Revision: 897402

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

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

Added: commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/AnnotationUtils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/AnnotationUtils.java?rev=897402&view=auto
==============================================================================
--- commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/AnnotationUtils.java
(added)
+++ commons/sandbox/at-digester/trunk/src/java/org/apache/commons/digester/annotations/utils/AnnotationUtils.java
Sat Jan  9 09:59:58 2010
@@ -0,0 +1,104 @@
+/*
+ * 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.utils;
+
+import java.lang.annotation.Annotation;
+
+import org.apache.commons.beanutils.MethodUtils;
+
+/**
+ * Simple utility class to introspect annotations.
+ *
+ * @author Simone Tripodi (simonetripodi)
+ * @version $Id$
+ */
+public class AnnotationUtils {
+
+    /**
+     * The {@code value} string constant.
+     */
+    private static final String VALUE = "value";
+
+    /**
+     * The {@code pattern} string constant.
+     */
+    private static final String PATTERN = "pattern";
+
+    /**
+     * This class can't be instantiated.
+     */
+    private AnnotationUtils() {
+        // this class can' be instantiated
+    }
+
+    /**
+     * Extract the {@code value()} from annotation.
+     *
+     * @param annotation the annotation has to be introspected.
+     * @return the annotation {@code value()}.
+     */
+    public static Object getAnnotationValue(Annotation annotation) {
+        return invokeAnnotationMethod(annotation, VALUE);
+    }
+
+    /**
+     * Extract the {@code pattern()} from annotation.
+     *
+     * @param annotation the annotation has to be introspected.
+     * @return the annotation {@code pattern()}.
+     */
+    public static String getAnnotationPattern(Annotation annotation) {
+        Object ret = invokeAnnotationMethod(annotation, PATTERN);
+        if (ret != null) {
+            return (String) ret;
+        }
+        return null;
+    }
+
+    /**
+     * Extract the Annotations array {@code value()} from annotation if present,
+     * nul otherwise.
+     *
+     * @param annotation the annotation has to be introspected.
+     * @return the annotation {@code value()} as Annotations array.
+     */
+    public static Annotation[] getAnnotationsArrayValue(Annotation annotation) {
+        Object value = getAnnotationValue(annotation);
+        if (value != null
+                && value.getClass().isArray()
+                && Annotation.class.isAssignableFrom(value.getClass().getComponentType()))
{
+            return (Annotation[]) value;
+        }
+        return null;
+    }
+
+    /**
+     * Invokes an annotation method.
+     *
+     * @param annotationn the annotation has to be introspected.
+     * @param method the method name to execute.
+     * @return the annotation method value, null if any error occurs.
+     */
+    private static Object invokeAnnotationMethod(Annotation annotation, String method) {
+        try {
+            return MethodUtils.invokeExactMethod(annotation, method, null);
+        } catch (Throwable t) {
+            return null;
+        }
+    }
+
+}

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

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

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



Mime
View raw message