1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 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 de.smartics.properties.utils; 17 18 import java.lang.reflect.Field; 19 import java.lang.reflect.Modifier; 20 21 import com.thoughtworks.qdox.model.JavaClass; 22 import com.thoughtworks.qdox.model.JavaField; 23 24 /** 25 * Utilities to instantiate classes and access their runtime information. 26 */ 27 public final class RuntimeUtils 28 { 29 // ********************************* Fields ********************************* 30 31 // --- constants ------------------------------------------------------------ 32 33 // --- members -------------------------------------------------------------- 34 35 /** 36 * The class loader to use to instantiate the referenced class. 37 */ 38 private final ClassLoader classLoader; 39 40 // ****************************** Initializer ******************************* 41 42 // ****************************** Constructors ****************************** 43 44 /** 45 * Default constructor. 46 * 47 * @param classLoader the class loader to use to instantiate the referenced 48 * class. 49 */ 50 public RuntimeUtils(final ClassLoader classLoader) 51 { 52 this.classLoader = classLoader; 53 } 54 55 // ****************************** Inner Classes ***************************** 56 57 // ********************************* Methods ******************************** 58 59 // --- init ----------------------------------------------------------------- 60 61 // --- get&set -------------------------------------------------------------- 62 63 // --- business ------------------------------------------------------------- 64 65 /** 66 * Loads the instance referenced by the given {@code fieldDoc}. 67 * 68 * @param fieldDoc the property of a class to be instantiated. This may be an 69 * enumeration element or a constant in a class. 70 * @return the instance if {@code fieldDoc} references an enumeration element 71 * or a constant, <code>null</code> otherwise. 72 * @throws ClassNotFoundException if the class of the field or any depending 73 * classes cannot be loaded. 74 * @throws NullPointerException if {@code fieldDoc} is <code>null</code>. 75 */ 76 public Object loadInstance(final JavaField fieldDoc) 77 throws ClassNotFoundException, NullPointerException 78 { 79 final JavaClass classDoc = fieldDoc.getDeclaringClass(); 80 final String className = classDoc.getFullyQualifiedName(); 81 final Class<?> clazz = Class.forName(className, true, classLoader); 82 final String fieldName = fieldDoc.getName(); 83 if (classDoc.isEnum()) 84 { 85 return loadEnumInstance(clazz, fieldName); 86 } 87 else 88 { 89 return loadStaticFieldInstance(clazz, fieldName); 90 } 91 } 92 93 /** 94 * Loads the class referenced by the given {@code classDoc}. 95 * 96 * @param classDoc the class documentation to load the class. 97 * @return the class instance. 98 * @throws NullPointerException if {@code classDoc} is <code>null</code>. 99 * @throws ClassNotFoundException if the class of the field or any depending 100 * classes cannot be loaded. 101 */ 102 public Class<?> loadClass(final JavaClass classDoc) 103 throws ClassNotFoundException, NullPointerException 104 { 105 final String className = classDoc.getFullyQualifiedName(); 106 final Class<?> clazz = Class.forName(className, true, classLoader); 107 return clazz; 108 } 109 110 /** 111 * Loads the enumeration element instance with the given identifier from the 112 * given class. 113 * 114 * @param clazz the enumeration class that contains the requested enumeration 115 * element. 116 * @param identifier the identifier of the enumeration element to return. 117 * @return the enumeration element instance or <code>null</code> if the class 118 * is not an enumeration or the enumeration does not contain an 119 * element with a name matching the given identifier. 120 * @throws NullPointerException if <code>clazz</code> or 121 * <code>identifier</code> is <code>null</code>. 122 */ 123 public static Enum<?> loadEnumInstance(final Class<?> clazz, 124 final String identifier) throws NullPointerException 125 { 126 final Enum<?>[] elements = (Enum[]) clazz.getEnumConstants(); 127 if (elements != null) 128 { 129 for (Enum<?> element : elements) 130 { 131 final String elementName = ((Enum<?>) element).name(); 132 if (identifier.equals(elementName)) 133 { 134 return element; 135 } 136 } 137 } 138 139 return null; 140 } 141 142 /** 143 * Loads the static property instance with the given identifier from the given 144 * class. 145 * 146 * @param clazz the class that contains the requested static property. 147 * @param identifier the identifier of the static property to return. 148 * @return the static property instance or <code>null</code> if the class does 149 * not contain a static property with a name matching the given 150 * identifier. 151 * @throws NullPointerException if <code>clazz</code> or 152 * <code>identifier</code> is <code>null</code>. 153 */ 154 public static Object loadStaticFieldInstance(final Class<?> clazz, 155 final String identifier) throws NullPointerException 156 { 157 try 158 { 159 final Field field = clazz.getDeclaredField(identifier); 160 field.setAccessible(true); 161 final int modifiers = field.getModifiers(); 162 if (Modifier.isStatic(modifiers)) 163 { 164 return field.get(null); 165 } 166 } 167 catch (final Exception e) 168 { 169 // return null at the end. 170 } 171 return null; 172 } 173 174 // --- object basics -------------------------------------------------------- 175 176 }