View Javadoc

1   /*
2    * Copyright 2007-2011 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.exceptions.report.sort;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Comparator;
22  import java.util.List;
23  
24  import com.sun.javadoc.ClassDoc;
25  import com.sun.javadoc.Doc;
26  import com.sun.javadoc.FieldDoc;
27  
28  import de.smartics.analysis.javadoc.runtime.RuntimeUtils;
29  import de.smartics.exceptions.core.Code;
30  import de.smartics.exceptions.report.message.PlaceHolderHandler;
31  import de.smartics.exceptions.report.message.PlaceHolderInfo;
32  import de.smartics.report.conf.ProjectConfiguration;
33  import de.smartics.report.util.JavadocUtils;
34  
35  /**
36   * Utilities handling Javadocs representing codes.
37   *
38   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
39   * @version $Revision:591 $
40   */
41  public final class CodeUtils
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    // --- members --------------------------------------------------------------
48  
49    // ****************************** Initializer *******************************
50  
51    // ****************************** Constructors ******************************
52  
53    /**
54     * Utility class pattern.
55     */
56    private CodeUtils()
57    {
58    }
59  
60    // ****************************** Inner Classes *****************************
61  
62    /**
63     * Bundle of information about a code. This information includes
64     * <ul>
65     * <li>Javadoc-Information</li>
66     * <li>An instance of the code</li>
67     * <li>Annotated information</li>
68     * </ul>
69     */
70    public static final class CodeContainer
71    {
72      /**
73       * The instance of the code.
74       */
75      private final Code code;
76  
77      /**
78       * The Javadoc information collected at class level.
79       */
80      private final ClassDoc classDoc;
81  
82      /**
83       * The Javadoc information collected at field level.
84       */
85      private final FieldDoc fieldDoc;
86  
87      /**
88       * The descriptions of the place holders.
89       */
90      private final PlaceHolderInfo placeHolderInfo;
91  
92      /**
93       * Default constructor.
94       *
95       * @param code the instance of the code.
96       * @param classDoc the Javadoc information collected at class level.
97       * @param fieldDoc the Javadoc information collected at field level.
98       * @param placeHolderInfo the descriptions of the place holders.
99       */
100     public CodeContainer(final Code code, final ClassDoc classDoc,
101         final FieldDoc fieldDoc, final PlaceHolderInfo placeHolderInfo)
102     {
103       this.code = code;
104       this.classDoc = classDoc;
105       this.fieldDoc = fieldDoc;
106       this.placeHolderInfo = placeHolderInfo;
107     }
108 
109     /**
110      * Returns the instance of the code.
111      *
112      * @return the instance of the code.
113      */
114     public Code getCode()
115     {
116       return code;
117     }
118 
119     /**
120      * Returns the Javadoc information collected at class level.
121      *
122      * @return the Javadoc information collected at class level.
123      */
124     public ClassDoc getClassDoc()
125     {
126       return classDoc;
127     }
128 
129     /**
130      * Returns the Javadoc information collected at field level.
131      *
132      * @return the Javadoc information collected at field level.
133      */
134     public FieldDoc getFieldDoc()
135     {
136       return fieldDoc;
137     }
138 
139     /**
140      * Returns the descriptions of the place holders.
141      *
142      * @return the descriptions of the place holders.
143      */
144     public PlaceHolderInfo getPlaceHolderInfo()
145     {
146       return placeHolderInfo;
147     }
148   }
149 
150   // ********************************* Methods ********************************
151 
152   // --- init -----------------------------------------------------------------
153 
154   // --- get&set --------------------------------------------------------------
155 
156   public static List<CodeContainer> getCodeList(
157       final ProjectConfiguration<?> config, final Collection<Doc> javadocs)
158   {
159     return getCodeList(config, javadocs, new CodeComparator());
160   }
161 
162   public static List<CodeContainer> getCodeList(
163       final ProjectConfiguration<?> config, final Collection<Doc> javadocs,
164       final Comparator<CodeContainer> comparator)
165   {
166     final ClassLoader classLoader = config.getProjectClassLoader();
167     final PlaceHolderHandler placeHolderHandler =
168         new PlaceHolderHandler(config);
169 
170     final List<CodeContainer> codeList =
171         new ArrayList<CodeContainer>(javadocs.size());
172     for (Doc doc : javadocs)
173     {
174       if (doc.isClass())
175       {
176         final ClassDoc classDoc = (ClassDoc) doc;
177         for (FieldDoc fieldDoc : JavadocUtils.getClassElements(classDoc))
178         {
179           final Code code = loadCode(classLoader, fieldDoc);
180           if (code != null)
181           {
182             final PlaceHolderInfo placeHolderInfo =
183                 placeHolderHandler.readPlaceholderDesc(classLoader, fieldDoc);
184             codeList.add(new CodeContainer(code, classDoc, fieldDoc,
185                 placeHolderInfo));
186           }
187         }
188       }
189     }
190 
191     Collections.sort(codeList, comparator);
192     return codeList;
193   }
194 
195   private static Code loadCode(final ClassLoader classLoader,
196       final FieldDoc fieldDoc)
197   {
198     try
199     {
200       final Code code = (Code) RuntimeUtils.loadInstance(classLoader, fieldDoc);
201       return code;
202     }
203     catch (final Exception e)
204     {
205       // TODO LOG!!!
206     }
207     return null;
208   }
209 
210   // --- business -------------------------------------------------------------
211 
212   // --- object basics --------------------------------------------------------
213 
214 }