View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.contract.model.util;
17  
18  import javax.xml.bind.annotation.XmlEnum;
19  
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import com.thoughtworks.qdox.model.Annotation;
24  import com.thoughtworks.qdox.model.JavaClass;
25  import com.thoughtworks.qdox.model.annotation.AnnotationValue;
26  
27  /**
28   * 
29   * Utility methods for extracting annotation details from a QDox JavaClass.
30   * 
31   * @author Kuali Student Team
32   */
33  public final class JavaClassAnnotationUtils {
34  
35  	private static final Logger log = LoggerFactory.getLogger(JavaClassAnnotationUtils.class);
36  	/**
37  	 * 
38  	 */
39  	private JavaClassAnnotationUtils() {
40  		// prevent subclassing and instances
41  	}
42  
43  	public static boolean doesAnnotationExist(String simpleAnnotationName, JavaClass javaClass) {
44  		
45  		Annotation[] as = javaClass.getAnnotations();
46  		
47  		for (Annotation annotation : as) {
48  			if (annotation.getType().getJavaClass().getName().equals(simpleAnnotationName))
49  				return true;
50  		}
51  		// no match
52  		return false;
53  		
54  	}
55  	/**
56  	 * Extract the value of the XmlEnum annotation from the class provided.
57  	 * 
58  	 * returns null if the annotation does not exist on the class.
59  	 * 
60  	 * returns String.class if no value is specified which is the default specified by the annotation.
61  	 * 
62  	 * @param javaClass
63  	 * @return the class value stored in the XmlEnum annotation on the class specified.
64  	 */
65  	public static Class<?>extractXmlEnumValue (JavaClass javaClass) {
66  		
67  		Annotation[] as = javaClass.getAnnotations();
68  		
69  		for (Annotation annotation : as) {
70  			if (annotation.getType().getJavaClass().getName().equals(XmlEnum.class.getSimpleName())) {
71  				
72  				AnnotationValue value = annotation.getProperty("value");
73  				
74  				if (value == null) {
75  					// this is what the XmlEnum annotation defaults to
76  					// ideally I would get this from the XmlEnum but I can't figure out how
77  					// so I will just hard code it.
78  					return String.class;
79  				}
80  				
81  					
82  				String clazz =  (String) value.getParameterValue();
83  				
84  					String className = clazz.replaceFirst("\\.class", "");
85  					
86  					try {
87  	                    return ClassLoader.getSystemClassLoader().loadClass(className);
88                      } catch (ClassNotFoundException e) {
89                      	log.error("No class found for name: " + className);
90                      	
91                      	return null;
92                      }
93  				
94  			}
95  		}
96  		
97  		// no match
98  		return null;
99  	}
100 }