001    /* $Id: AnnotationUtils.java 992060 2010-09-02 19:09:47Z simonetripodi $
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.commons.digester.annotations.utils;
019    
020    import java.lang.annotation.Annotation;
021    
022    import org.apache.commons.beanutils.MethodUtils;
023    
024    /**
025     * Simple utility class to introspect annotations.
026     *
027     * @since 2.1
028     */
029    public class AnnotationUtils {
030    
031        /**
032         * The {@code value} string constant.
033         */
034        private static final String VALUE = "value";
035    
036        /**
037         * The {@code pattern} string constant.
038         */
039        private static final String PATTERN = "pattern";
040    
041        /**
042         * This class can't be instantiated.
043         */
044        private AnnotationUtils() {
045            // this class can' be instantiated
046        }
047    
048        /**
049         * Extract the {@code value()} from annotation.
050         *
051         * @param annotation the annotation has to be introspected.
052         * @return the annotation {@code value()}.
053         */
054        public static Object getAnnotationValue(Annotation annotation) {
055            return invokeAnnotationMethod(annotation, VALUE);
056        }
057    
058        /**
059         * Extract the {@code pattern()} from annotation.
060         *
061         * @param annotation the annotation has to be introspected.
062         * @return the annotation {@code pattern()}.
063         */
064        public static String getAnnotationPattern(Annotation annotation) {
065            Object ret = invokeAnnotationMethod(annotation, PATTERN);
066            if (ret != null) {
067                return (String) ret;
068            }
069            return null;
070        }
071    
072        /**
073         * Extract the Annotations array {@code value()} from annotation if present,
074         * nul otherwise.
075         *
076         * @param annotation the annotation has to be introspected.
077         * @return the annotation {@code value()} as Annotations array.
078         */
079        public static Annotation[] getAnnotationsArrayValue(Annotation annotation) {
080            Object value = getAnnotationValue(annotation);
081            if (value != null
082                    && value.getClass().isArray()
083                    && Annotation.class.isAssignableFrom(value.getClass().getComponentType())) {
084                return (Annotation[]) value;
085            }
086            return null;
087        }
088    
089        /**
090         * Invokes an annotation method.
091         *
092         * @param annotationn the annotation has to be introspected.
093         * @param method the method name to execute.
094         * @return the annotation method value, null if any error occurs.
095         */
096        private static Object invokeAnnotationMethod(Annotation annotation, String method) {
097            try {
098                return MethodUtils.invokeExactMethod(annotation, method, null);
099            } catch (Throwable t) {
100                return null;
101            }
102        }
103    
104    }