001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.student.contract.model.util;
017
018 import javax.xml.bind.annotation.XmlEnum;
019
020 import org.slf4j.Logger;
021 import org.slf4j.LoggerFactory;
022
023 import com.thoughtworks.qdox.model.Annotation;
024 import com.thoughtworks.qdox.model.JavaClass;
025 import com.thoughtworks.qdox.model.annotation.AnnotationValue;
026
027 /**
028 *
029 * Utility methods for extracting annotation details from a QDox JavaClass.
030 *
031 * @author Kuali Student Team
032 */
033 public final class JavaClassAnnotationUtils {
034
035 private static final Logger log = LoggerFactory.getLogger(JavaClassAnnotationUtils.class);
036 /**
037 *
038 */
039 private JavaClassAnnotationUtils() {
040 // prevent subclassing and instances
041 }
042
043 public static boolean doesAnnotationExist(String simpleAnnotationName, JavaClass javaClass) {
044
045 Annotation[] as = javaClass.getAnnotations();
046
047 for (Annotation annotation : as) {
048 if (annotation.getType().getJavaClass().getName().equals(simpleAnnotationName))
049 return true;
050 }
051 // no match
052 return false;
053
054 }
055 /**
056 * Extract the value of the XmlEnum annotation from the class provided.
057 *
058 * returns null if the annotation does not exist on the class.
059 *
060 * returns String.class if no value is specified which is the default specified by the annotation.
061 *
062 * @param javaClass
063 * @return the class value stored in the XmlEnum annotation on the class specified.
064 */
065 public static Class<?>extractXmlEnumValue (JavaClass javaClass) {
066
067 Annotation[] as = javaClass.getAnnotations();
068
069 for (Annotation annotation : as) {
070 if (annotation.getType().getJavaClass().getName().equals(XmlEnum.class.getSimpleName())) {
071
072 AnnotationValue value = annotation.getProperty("value");
073
074 if (value == null) {
075 // this is what the XmlEnum annotation defaults to
076 // ideally I would get this from the XmlEnum but I can't figure out how
077 // so I will just hard code it.
078 return String.class;
079 }
080
081
082 String clazz = (String) value.getParameterValue();
083
084 String className = clazz.replaceFirst("\\.class", "");
085
086 try {
087 return ClassLoader.getSystemClassLoader().loadClass(className);
088 } catch (ClassNotFoundException e) {
089 log.error("No class found for name: " + className);
090
091 return null;
092 }
093
094 }
095 }
096
097 // no match
098 return null;
099 }
100 }