View Javadoc

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