root/trunk/src/java/org/jcoderz/phoenix/report/FindBugsReportReader.java

Revision 1011, 9.3 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.phoenix.report;
34
35import java.io.File;
36import java.io.FileInputStream;
37import java.io.IOException;
38import java.util.ArrayList;
39import java.util.HashMap;
40import java.util.Iterator;
41import java.util.List;
42import java.util.Map;
43import java.util.logging.Logger;
44
45import javax.xml.bind.JAXBException;
46
47import org.jcoderz.phoenix.findbugs.jaxb.BugCollection;
48import org.jcoderz.phoenix.findbugs.jaxb.BugInstanceType;
49import org.jcoderz.phoenix.findbugs.jaxb.Class;
50import org.jcoderz.phoenix.findbugs.jaxb.Field;
51import org.jcoderz.phoenix.findbugs.jaxb.Int;
52import org.jcoderz.phoenix.findbugs.jaxb.Method;
53import org.jcoderz.phoenix.findbugs.jaxb.SourceLine;
54import org.jcoderz.phoenix.findbugs.jaxb.SourceLineType;
55import org.jcoderz.phoenix.report.jaxb.Item;
56import org.jcoderz.phoenix.report.jaxb.ObjectFactory;
57
58/**
59 *
60 * @author Michael Griffel
61 */
62public final class FindBugsReportReader
63   extends AbstractReportReader
64{
65   /** JAXB context path. */
66   public static final String FINDBUGS_JAXB_CONTEXT_PATH
67      = "org.jcoderz.phoenix.findbugs.jaxb";
68
69   private static final String CLASSNAME = FindBugsReportReader.class.getName();
70
71   private static final Logger logger = Logger.getLogger(CLASSNAME);
72
73   private BugCollection mReportDocument;
74
75
76   FindBugsReportReader ()
77      throws JAXBException
78   {
79      super(FINDBUGS_JAXB_CONTEXT_PATH);
80   }
81
82   /** {@inheritDoc} */
83   public void parse (File f)
84      throws JAXBException
85   {
86      try
87      {
88         mReportDocument = (BugCollection) getUnmarshaller().unmarshal(
89               new JCoverageInputStream(new FileInputStream(f)));
90      }
91      catch (IOException e)
92      {
93         throw new JAXBException("Cannot read JCoverage report", e);
94      }
95   }
96
97   /** {@inheritDoc} */
98   public Map getItems ()
99      throws JAXBException
100   {
101      final Map itemMap = new HashMap();
102
103      final List bugInstances = mReportDocument.getBugInstance();
104      logger.fine("Found #" + bugInstances.size() + " FindBugs bug instances!");
105
106      final List sourceDirs
107         = mReportDocument.getProject().getSrcDir();
108      logger.finer("Using source dir '" + sourceDirs + "'");
109
110      for (final Iterator iterator = bugInstances.iterator();
111            iterator.hasNext();)
112      {
113         final BugInstanceType bugInstance = (BugInstanceType) iterator.next();
114
115         final List list = bugInstance.getClassOrFieldOrMethod();
116         final Item item = new ObjectFactory().createItem();
117         final List objectMessageList = new ArrayList();
118
119         item.setMessage(bugInstance.getLongMessage());
120         boolean topLevelSourceLineRead = false;
121         for (final Iterator iter = list.iterator(); iter.hasNext();)
122         {
123            final Object element = iter.next();
124            objectMessageList.add(toString(element));
125            if (element instanceof Class)
126            {
127               if (item.isSetOrigin())
128               {
129                  continue;
130               }
131
132               final Class c = (Class) element;
133
134               final String clazz = c.getClassname();
135               logger.finer("Processing class '" + clazz + "'");
136               final String javaFile = convertToRelativeJavaFile(clazz);
137
138               final ResourceInfo info = findResourceInfo(sourceDirs, javaFile);
139
140               if (info != null)
141               {
142                  List itemList = (List) itemMap.get(info);
143                  if (itemList == null)
144                  {
145                     itemList = new ArrayList();
146                     itemMap.put(info, itemList);
147                  }
148                  item.setOrigin(Origin.FINDBUGS);
149                  item.setSeverity(bugInstance.getPriority());
150                  item.setFindingType(bugInstance.getType());
151                  itemList.add(item);
152                  logger.finest("Adding findings for resource " + javaFile);
153               }
154               else
155               {
156                   logger.finer("Ignoring findings for resource " + javaFile);
157               }
158            }
159            else if (element instanceof SourceLine)
160            {
161               // Can be more specific info so allow override
162               // if given data is not concrete.
163               // There are finders like IL_INFINITE_LOOP which
164               // report additional SourceLine items that point to
165               // informative other lines rather than the buginstance.
166               // Til we know how to get the correct line we should leave it
167               // like that. (see also http://tinyurl.com/ycol9h ff.)
168               if (topLevelSourceLineRead
169                   && item.isSetLine() && item.getLine() > 0)
170               {
171                   continue;
172               }
173               logger.finer("Adding source line information to item "
174                     + item.getFindingType());
175               final SourceLine sourceLine = (SourceLine) element;
176               if (sourceLine.isSetStart())
177               {
178                   item.setLine(sourceLine.getStart());
179                   topLevelSourceLineRead = true;
180                   if (sourceLine.isSetEnd())
181                   {
182                       item.setEndLine(sourceLine.getEnd());
183                   }
184               }
185            }
186            else if (element instanceof Method)
187            {
188               if (item.isSetLine())
189               {
190                  continue;
191               }
192               if (((Method) element).isSetSourceLine())
193               {
194                  logger.finer("Adding source line information for method"
195                        + " to item " + item.getFindingType());
196                  final SourceLineType sourceLine
197                        = ((Method) element).getSourceLine();
198                  if (sourceLine.isSetStart())
199                  {
200                      item.setLine(sourceLine.getStart());
201                      if (sourceLine.isSetEnd())
202                      {
203                          item.setEndLine(sourceLine.getEnd());
204                      }
205                  }
206               }
207            }
208         }
209      }
210
211      return itemMap;
212   }
213
214   private ResourceInfo findResourceInfo (List sourceDirs, String javaFile)
215   {
216      ResourceInfo info = null;
217      for (final Iterator iterator = sourceDirs.iterator();
218            iterator.hasNext();)
219      {
220         final String srcDir = (String) iterator.next() + File.separator;
221         final String key = normalizeFileName(srcDir + javaFile);
222         logger.finest("Looking for file: " + key);
223         info = ResourceInfo.lookup(key);
224         if (info != null)
225         {
226            break;
227         }
228      }
229      return info;
230   }
231
232   private String toString (Object element)
233   {
234      final String ret;
235      if (element instanceof Class)
236      {
237         final Class c = (Class) element;
238         ret = c.getClassname();
239      }
240      else if (element instanceof Method)
241      {
242         final Method m = (Method) element;
243         ret = m.getName() + m.getSignature();
244      }
245      else if (element instanceof Field)
246      {
247         final Field f = (Field) element;
248         ret = f.getName();
249      }
250      else if (element instanceof SourceLine)
251      {
252         final SourceLine sl = (SourceLine) element;
253         ret = sl.getStart() + "-" + sl.getEnd();
254      }
255      else if (element instanceof Int)
256      {
257        final Int i = (Int) element;
258        ret = String.valueOf(i.getValue());
259      }
260      else
261      {
262         ret = String.valueOf(element);
263      }
264      return ret;
265   }
266
267   private String convertToRelativeJavaFile (String clzznm)
268   {
269      String clazzname = clzznm;
270      if (clazzname.indexOf('$') != -1) // inner clazz
271      {
272         clazzname = clazzname.substring(0, clazzname.indexOf('$'));
273      }
274      return clazzname.replace('.', File.separatorChar) + ".java";
275   }
276}
Note: See TracBrowser for help on using the browser.