1 package org.apache.commons.digester3.annotations.utils; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import static org.apache.commons.beanutils.MethodUtils.invokeExactMethod; 23 24 import java.lang.annotation.Annotation; 25 26 /** 27 * Simple utility class to introspect annotations. 28 * 29 * @since 2.1 30 */ 31 public class AnnotationUtils 32 { 33 34 /** 35 * The {@code value} string constant. 36 */ 37 private static final String VALUE = "value"; 38 39 /** 40 * The {@code pattern} string constant. 41 */ 42 private static final String PATTERN = "pattern"; 43 44 /** 45 * The {@code namespaceURI} string constant. 46 */ 47 private static final String NAMESPACE_URI = "namespaceURI"; 48 49 /** 50 * The {@code namespaceURI} string constant. 51 */ 52 private static final String FIRE_ON_BEGIN = "fireOnBegin"; 53 54 /** 55 * This class can't be instantiated. 56 */ 57 private AnnotationUtils() 58 { 59 // this class can' be instantiated 60 } 61 62 /** 63 * Extract the {@code value()} from annotation. 64 * 65 * @param annotation the annotation has to be introspected. 66 * @return the annotation {@code value()}. 67 */ 68 public static Object getAnnotationValue( Annotation annotation ) 69 { 70 return invokeAnnotationMethod( annotation, VALUE ); 71 } 72 73 /** 74 * Extract the {@code pattern()} from annotation. 75 * 76 * @param annotation the annotation has to be introspected. 77 * @return the annotation {@code pattern()}. 78 */ 79 public static String getAnnotationPattern( Annotation annotation ) 80 { 81 Object ret = invokeAnnotationMethod( annotation, PATTERN ); 82 if ( ret != null ) 83 { 84 return (String) ret; 85 } 86 return null; 87 } 88 89 /** 90 * Extract the {@code namespaceURI()} from annotation. 91 * 92 * @param annotation The annotation has to be introspected 93 * @return The annotation {@code namespaceURI()} 94 */ 95 public static String getAnnotationNamespaceURI( Annotation annotation ) 96 { 97 Object ret = invokeAnnotationMethod( annotation, NAMESPACE_URI ); 98 if ( ret != null ) 99 { 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 }