KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > vladium > emma > report > AbstractReportGenerator


1 /* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2  *
3  * This program and the accompanying materials are made available under
4  * the terms of the Common Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/cpl-v10.html
6  *
7  * $Id: AbstractReportGenerator.java,v 1.1.1.1.2.4 2005/04/24 23:51:37 vlad_r Exp $
8  */

9 package com.vladium.emma.report;
10
11 import java.util.Iterator JavaDoc;
12 import java.util.Set JavaDoc;
13 import java.util.TreeSet JavaDoc;
14
15 import com.vladium.logging.Logger;
16 import com.vladium.util.Descriptors;
17 import com.vladium.util.IProperties;
18 import com.vladium.util.IntIntMap;
19 import com.vladium.util.IntVector;
20 import com.vladium.util.ObjectIntMap;
21 import com.vladium.emma.EMMARuntimeException;
22 import com.vladium.emma.data.ClassDescriptor;
23 import com.vladium.emma.data.ICoverageData;
24 import com.vladium.emma.data.IMetaData;
25
26 // ----------------------------------------------------------------------------
27
/**
28  * @author Vlad Roubtsov, (C) 2003
29  */

30 public
31 abstract class AbstractReportGenerator extends AbstractItemVisitor
32                                        implements IReportGenerator
33 {
34     // public: ................................................................
35

36     
37     public static IReportGenerator create (final String JavaDoc type)
38     {
39         if ((type == null) || (type.length () == 0))
40             throw new IllegalArgumentException JavaDoc ("null/empty input: type");
41         
42         // TODO: proper pluggability pattern here
43

44         if ("html".equals (type))
45             return new com.vladium.emma.report.html.ReportGenerator ();
46         else if ("txt".equals (type))
47             return new com.vladium.emma.report.txt.ReportGenerator ();
48         else if ("xml".equals (type))
49             return new com.vladium.emma.report.xml.ReportGenerator ();
50         else // TODO: error code
51
throw new EMMARuntimeException ("no report generator class found for type [" + type + "]");
52     }
53     
54     
55     public void initialize (final IMetaData mdata, final ICoverageData cdata,
56                             final SourcePathCache cache, final IProperties properties)
57         throws EMMARuntimeException
58     {
59         m_log = Logger.getLogger ();
60         m_verbose = m_log.atVERBOSE ();
61         
62         m_settings = ReportProperties.parseProperties (properties, getType ());
63         
64         m_cache = cache;
65                 
66         m_hasSrcFileInfo = mdata.hasSrcFileData ();
67         m_hasLineNumberInfo = mdata.hasLineNumberData ();
68         
69         boolean debugInfoWarning = false;
70         boolean bailOut = false;
71         
72         // src view is not possible if 'm_hasSrcFileInfo' is false:
73
if (! mdata.hasSrcFileData () && (m_settings.getViewType () == IReportDataView.HIER_SRC_VIEW))
74         {
75             debugInfoWarning = true;
76             
77             m_log.warning ("not all instrumented classes were compiled with source file");
78             m_log.warning ("debug data: no sources will be embedded in the report.");
79             
80             m_settings.setViewType (IReportDataView.HIER_CLS_VIEW);
81         }
82         
83         // line coverage column must be removed if 'm_hasLineNumberInfo' is false:
84
if (! m_hasLineNumberInfo)
85         {
86             final int [] userColumnIDs = m_settings.getColumnOrder ();
87             final IntVector columnIDs = new IntVector ();
88             
89             boolean removed = false;
90             for (int c = 0; c < userColumnIDs.length; ++ c)
91             {
92                 if (userColumnIDs [c] == IItemAttribute.ATTRIBUTE_LINE_COVERAGE_ID)
93                     removed = true;
94                 else
95                     columnIDs.add (userColumnIDs [c]);
96             }
97             
98             // at this point it is possible that there are no columns left: bail out
99
if (removed)
100             {
101                 debugInfoWarning = true;
102                 
103                 if (columnIDs.size () == 0)
104                 {
105                     m_log.warning ("line coverage requested in a report of type [" + getType () + "] but");
106                     m_log.warning ("not all instrumented classes were compiled with line number");
107                     m_log.warning ("debug data: since this was the only requested column, no report will be generated.");
108
109                     bailOut = true;
110                 }
111                 else
112                 {
113                     m_log.warning ("line coverage requested in a report of type [" + getType () + "] but");
114                     m_log.warning ("not all instrumented classes were compiled with line number");
115                     m_log.warning ("debug data: this column will be removed from the report.");
116                     
117                     m_settings.setColumnOrder (columnIDs.values ());
118                     
119                     final int [] userSort = m_settings.getSortOrder ();
120                     final IntVector sort = new IntVector ();
121                     
122                     for (int c = 0; c < userSort.length; c += 2)
123                     {
124                         if (Math.abs (userSort [c]) != IItemAttribute.ATTRIBUTE_LINE_COVERAGE_ID)
125                         {
126                             sort.add (userSort [c]);
127                             sort.add (userSort [c + 1]);
128                         }
129                     }
130                     
131                     m_settings.setSortOrder (sort.values ());
132                 }
133             }
134         }
135         // note: no need to adjust m_metrics due to possible column removal above
136

137         // SF FR 971176: provide user with sample classes that caused the above warnings
138
if (debugInfoWarning && m_log.atINFO ())
139         {
140             final Set JavaDoc /* String */ sampleClassNames = new TreeSet JavaDoc ();
141             final ObjectIntMap /* packageVMName:String -> count:int */ countMap = new ObjectIntMap ();
142             final int [] _count = new int [1];
143             
144             for (Iterator JavaDoc /* ClassDescriptor */ descriptors = mdata.iterator (); descriptors.hasNext (); )
145             {
146                 final ClassDescriptor cls = (ClassDescriptor) descriptors.next ();
147                 
148                 // SF BUG 979717: this check was incorrectly absent in the initial FR impl:
149
if (! cls.hasCompleteLineNumberInfo () || ! cls.hasSrcFileInfo ())
150                 {
151                     final String JavaDoc packageVMName = cls.getPackageVMName ();
152                     final int count = countMap.get (packageVMName, _count)
153                         ? _count [0]
154                         : 0;
155                     
156                     if (count < MAX_DEBUG_INFO_WARNING_COUNT)
157                     {
158                         sampleClassNames.add (Descriptors.vmNameToJavaName (cls.getClassVMName ()));
159                         countMap.put (packageVMName, count + 1);
160                     }
161                 }
162             }
163             
164             m_log.info ("showing up to " + MAX_DEBUG_INFO_WARNING_COUNT + " classes without full debug info per package:");
165             for (Iterator JavaDoc /* String */ names = sampleClassNames.iterator (); names.hasNext (); )
166             {
167                 m_log.info (" " + names.next ());
168             }
169         }
170         
171         if (bailOut)
172         {
173             // TODO: error code
174
throw new EMMARuntimeException ("BAILED OUT");
175         }
176         
177         final IItemMetadata [] allTypes = IItemMetadata.Factory.getAllTypes ();
178         m_typeSortComparators = new ItemComparator [allTypes.length];
179         
180         for (int t = 0; t < allTypes.length; ++ t)
181         {
182             final IntVector orderedAttrIDsWithDir = new IntVector ();
183             final long typeAttrIDSet = allTypes [t].getAttributeIDs ();
184             
185             for (int s = 0; s < m_settings.getSortOrder ().length; s += 2)
186             {
187                 final int attrID = m_settings.getSortOrder () [s];
188                 
189                 if ((typeAttrIDSet & (1 << attrID)) != 0)
190                 {
191                     orderedAttrIDsWithDir.add (attrID);
192                     
193                     final int dir = m_settings.getSortOrder () [s + 1];
194                     orderedAttrIDsWithDir.add (dir);
195                 }
196             }
197             
198             m_typeSortComparators [t] = ItemComparator.Factory.create (orderedAttrIDsWithDir.values (), m_settings.getUnitsType ());
199         }
200         
201         m_metrics = new int [allTypes.length];
202         final IntIntMap metrics = m_settings.getMetrics ();
203         for (int t = 0; t < m_metrics.length; ++ t)
204         {
205             m_metrics [t] = -1;
206             metrics.get (t, m_metrics, t);
207         }
208         
209         final IReportDataModel model = IReportDataModel.Factory.create (mdata, cdata);
210         m_view = model.getView (m_settings.getViewType ());
211         
212         m_srcView = (m_settings.getViewType () == IReportDataView.HIER_SRC_VIEW);
213     }
214     
215     public void cleanup ()
216     {
217         reset ();
218     }
219     
220     // protected: .............................................................
221

222     
223     protected void reset ()
224     {
225         m_settings = null;
226         m_cache = null;
227         m_view = null;
228         m_srcView = false;
229         
230         //m_typeSortIDs = null;
231
m_typeSortComparators = null;
232         m_metrics = null;
233         
234         m_log = null;
235     }
236     
237
238     protected ReportProperties.ParsedProperties m_settings;
239     protected SourcePathCache m_cache;
240     protected IReportDataView m_view;
241     protected boolean m_srcView;
242     
243     protected boolean m_hasSrcFileInfo, m_hasLineNumberInfo;
244     protected ItemComparator [] m_typeSortComparators; // m_typeSortComparators [t] is a comparator representing the sort order for item type t
245
protected int [] m_metrics; // -1 means no pass/fail check for this attribute
246

247     protected Logger m_log; // every report generator is used on a single thread but the logger needs to be run()-scoped
248
protected boolean m_verbose;
249     
250     // package: ...............................................................
251

252     // private: ...............................................................
253

254     
255     private static final int MAX_DEBUG_INFO_WARNING_COUNT = 3; // per package
256

257 } // end of class
258
// ----------------------------------------------------------------------------
Popular Tags