001package org.apache.commons.digester3.annotations.utils;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.apache.commons.beanutils.MethodUtils.invokeExactMethod;
023
024import java.lang.annotation.Annotation;
025
026/**
027 * Simple utility class to introspect annotations.
028 * 
029 * @since 2.1
030 */
031public class AnnotationUtils
032{
033
034    /**
035     * The {@code value} string constant.
036     */
037    private static final String VALUE = "value";
038
039    /**
040     * The {@code pattern} string constant.
041     */
042    private static final String PATTERN = "pattern";
043
044    /**
045     * The {@code namespaceURI} string constant.
046     */
047    private static final String NAMESPACE_URI = "namespaceURI";
048
049    /**
050     * The {@code namespaceURI} string constant.
051     */
052    private static final String FIRE_ON_BEGIN = "fireOnBegin";
053
054    /**
055     * This class can't be instantiated.
056     */
057    private AnnotationUtils()
058    {
059        // this class can' be instantiated
060    }
061
062    /**
063     * Extract the {@code value()} from annotation.
064     * 
065     * @param annotation the annotation has to be introspected.
066     * @return the annotation {@code value()}.
067     */
068    public static Object getAnnotationValue( Annotation annotation )
069    {
070        return invokeAnnotationMethod( annotation, VALUE );
071    }
072
073    /**
074     * Extract the {@code pattern()} from annotation.
075     * 
076     * @param annotation the annotation has to be introspected.
077     * @return the annotation {@code pattern()}.
078     */
079    public static String getAnnotationPattern( Annotation annotation )
080    {
081        Object ret = invokeAnnotationMethod( annotation, PATTERN );
082        if ( ret != null )
083        {
084            return (String) ret;
085        }
086        return null;
087    }
088
089    /**
090     * Extract the {@code namespaceURI()} from annotation.
091     *
092     * @param annotation The annotation has to be introspected
093     * @return The annotation {@code namespaceURI()}
094     */
095    public static String getAnnotationNamespaceURI( Annotation annotation )
096    {
097        Object ret = invokeAnnotationMethod( annotation, NAMESPACE_URI );
098        if ( ret != null )
099        {
100            return (String) ret;
101        }
102        return null;
103    }
104
105    /**
106     * Extract the {@code fireOnBegin()} from annotation.
107     *
108     * @param annotation The annotation has to be introspected
109     * @return The annotation {@code fireOnBegin()}
110     */
111    public static boolean getFireOnBegin( Annotation annotation )
112    {
113        Object ret = invokeAnnotationMethod( annotation, FIRE_ON_BEGIN );
114        if ( ret != null )
115        {
116            return (Boolean) ret;
117        }
118        return false;
119    }
120
121    /**
122     * Extract the Annotations array {@code value()} from annotation if present, nul otherwise.
123     * 
124     * @param annotation the annotation has to be introspected.
125     * @return the annotation {@code value()} as Annotations array.
126     */
127    public static Annotation[] getAnnotationsArrayValue( Annotation annotation )
128    {
129        Object value = getAnnotationValue( annotation );
130        if ( value != null && value.getClass().isArray()
131            && Annotation.class.isAssignableFrom( value.getClass().getComponentType() ) )
132        {
133            return (Annotation[]) value;
134        }
135        return null;
136    }
137
138    /**
139     * Invokes an annotation method.
140     * 
141     * @param annotationn the annotation has to be introspected.
142     * @param method the method name to execute.
143     * @return the annotation method value, null if any error occurs.
144     */
145    private static Object invokeAnnotationMethod( Annotation annotation, String method )
146    {
147        try
148        {
149            return invokeExactMethod( annotation, method, null );
150        }
151        catch ( Throwable t )
152        {
153            return null;
154        }
155    }
156
157}