Project Report: fawkez

Packagesummary org.jcoderz.phoenix.report

org.jcoderz.phoenix.report.FindBugsReportReader

LineHitsNoteSource
1  /*
2   * $Id: FindBugsReportReader.java 1011 2008-06-16 17:57:36Z amandel $
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   */
33  package org.jcoderz.phoenix.report;
34  
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.IOException;
38  import java.util.ArrayList;
39  import java.util.HashMap;
40  import java.util.Iterator;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.logging.Logger;
44  
45  import javax.xml.bind.JAXBException;
46  
47  import org.jcoderz.phoenix.findbugs.jaxb.BugCollection;
48  import org.jcoderz.phoenix.findbugs.jaxb.BugInstanceType;
49  import org.jcoderz.phoenix.findbugs.jaxb.Class;
50  import org.jcoderz.phoenix.findbugs.jaxb.Field;
51  import org.jcoderz.phoenix.findbugs.jaxb.Int;
52  import org.jcoderz.phoenix.findbugs.jaxb.Method;
53  import org.jcoderz.phoenix.findbugs.jaxb.SourceLine;
54  import org.jcoderz.phoenix.findbugs.jaxb.SourceLineType;
55  import org.jcoderz.phoenix.report.jaxb.Item;
56  import org.jcoderz.phoenix.report.jaxb.ObjectFactory;
57  
58  /**
59   *
60   * @author Michael Griffel
61   */
62  public 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  
690    private static final String CLASSNAME = FindBugsReportReader.class.getName();
70  
710    private static final Logger logger = Logger.getLogger(CLASSNAME);
72  
73     private BugCollection mReportDocument;
74  
75  
76     FindBugsReportReader ()
77        throws JAXBException
78     {
790       super(FINDBUGS_JAXB_CONTEXT_PATH);
800    }
81  
82     /** {@inheritDoc} */
83     public void parse (File f)
84        throws JAXBException
85     {
86        try
87        {
880          mReportDocument = (BugCollection) getUnmarshaller().unmarshal(
89                 new JCoverageInputStream(new FileInputStream(f)));
90        }
910       catch (IOException e)
92        {
930(1)         throw new JAXBException("Cannot read JCoverage report", e);
940       }
950    }
96  
97     /** {@inheritDoc} */
98 (2)(3)(4)   public Map getItems ()
99        throws JAXBException
100     {
1010       final Map itemMap = new HashMap();
102  
1030       final List bugInstances = mReportDocument.getBugInstance();
1040       logger.fine("Found #" + bugInstances.size() + " FindBugs bug instances!");
105  
1060       final List sourceDirs
107           = mReportDocument.getProject().getSrcDir();
1080       logger.finer("Using source dir '" + sourceDirs + "'");
109  
1100       for (final Iterator iterator = bugInstances.iterator();
1110             iterator.hasNext();)
112        {
1130          final BugInstanceType bugInstance = (BugInstanceType) iterator.next();
114  
1150          final List list = bugInstance.getClassOrFieldOrMethod();
1160          final Item item = new ObjectFactory().createItem();
1170          final List objectMessageList = new ArrayList();
118  
1190          item.setMessage(bugInstance.getLongMessage());
1200          boolean topLevelSourceLineRead = false;
1210          for (final Iterator iter = list.iterator(); iter.hasNext();)
122           {
1230             final Object element = iter.next();
1240(5)            objectMessageList.add(toString(element));
1250(6)            if (element instanceof Class)
126              {
1270                if (item.isSetOrigin())
128                 {
1290                   continue;
130                 }
131  
1320                final Class c = (Class) element;
133  
1340                final String clazz = c.getClassname();
1350                logger.finer("Processing class '" + clazz + "'");
1360                final String javaFile = convertToRelativeJavaFile(clazz);
137  
1380                final ResourceInfo info = findResourceInfo(sourceDirs, javaFile);
139  
1400                if (info != null)
141                 {
1420                   List itemList = (List) itemMap.get(info);
1430                   if (itemList == null)
144                    {
1450                      itemList = new ArrayList();
1460(7)                     itemMap.put(info, itemList);
147                    }
1480                   item.setOrigin(Origin.FINDBUGS);
1490                   item.setSeverity(bugInstance.getPriority());
1500                   item.setFindingType(bugInstance.getType());
1510(8)                  itemList.add(item);
1520                   logger.finest("Adding findings for resource " + javaFile);
1530                }
154                 else
155                 {
1560                    logger.finer("Ignoring findings for resource " + javaFile);
157                 }
1580             }
1590             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.)
1680                if (topLevelSourceLineRead
169                     && item.isSetLine() && item.getLine() > 0)
170                 {
1710                    continue;
172                 }
1730                logger.finer("Adding source line information to item "
174                       + item.getFindingType());
1750                final SourceLine sourceLine = (SourceLine) element;
1760                if (sourceLine.isSetStart())
177                 {
1780                    item.setLine(sourceLine.getStart());
1790                    topLevelSourceLineRead = true;
1800                    if (sourceLine.isSetEnd())
181                     {
1820                        item.setEndLine(sourceLine.getEnd());
183                     }
184                 }
1850             }
1860             else if (element instanceof Method)
187              {
1880                if (item.isSetLine())
189                 {
1900                   continue;
191                 }
1920                if (((Method) element).isSetSourceLine())
193                 {
1940                   logger.finer("Adding source line information for method"
195                          + " to item " + item.getFindingType());
1960                   final SourceLineType sourceLine
197                          = ((Method) element).getSourceLine();
1980                   if (sourceLine.isSetStart())
199                    {
2000                       item.setLine(sourceLine.getStart());
2010(9)                      if (sourceLine.isSetEnd())
202                        {
2030                           item.setEndLine(sourceLine.getEnd());
204                        }
205                    }
206                 }
207              }
2080          }
2090       }
210  
2110       return itemMap;
212     }
213  
214     private ResourceInfo findResourceInfo (List sourceDirs, String javaFile)
215     {
2160       ResourceInfo info = null;
2170       for (final Iterator iterator = sourceDirs.iterator();
2180             iterator.hasNext();)
219        {
2200          final String srcDir = (String) iterator.next() + File.separator;
2210          final String key = normalizeFileName(srcDir + javaFile);
2220          logger.finest("Looking for file: " + key);
2230          info = ResourceInfo.lookup(key);
2240          if (info != null)
225           {
2260             break;
227           }
2280       }
2290       return info;
230     }
231  
232     private String toString (Object element)
233     {
234        final String ret;
2350       if (element instanceof Class)
236        {
2370          final Class c = (Class) element;
2380          ret = c.getClassname();
2390       }
2400       else if (element instanceof Method)
241        {
2420          final Method m = (Method) element;
2430          ret = m.getName() + m.getSignature();
2440       }
2450       else if (element instanceof Field)
246        {
2470          final Field f = (Field) element;
2480          ret = f.getName();
2490       }
2500       else if (element instanceof SourceLine)
251        {
2520          final SourceLine sl = (SourceLine) element;
2530          ret = sl.getStart() + "-" + sl.getEnd();
2540       }
2550       else if (element instanceof Int)
256        {
2570         final Int i = (Int) element;
2580         ret = String.valueOf(i.getValue());
2590       }
260        else
261        {
2620          ret = String.valueOf(element);
263        }
2640       return ret;
265     }
266  
267     private String convertToRelativeJavaFile (String clzznm)
268     {
2690       String clazzname = clzznm;
2700       if (clazzname.indexOf('$') != -1) // inner clazz
271        {
2720          clazzname = clazzname.substring(0, clazzname.indexOf('$'));
273        }
2740       return clazzname.replace('.', File.separatorChar) + ".java";
275     }
276  }

Findings in this File

i (1) 93 : 0 method org.jcoderz.phoenix.report.FindBugsReportReader.parse(File) throws exception with static message string
c (2) 98 : 4 Cyclomatic Complexity is 18 (max allowed is 12).
d (3) 98 : 4 Method length is 113 lines (max allowed is 100).
d (4) 98 : 15 getItems() in org.jcoderz.phoenix.report.FindBugsReportReader overrides getItems() in org.jcoderz.phoenix.report.AbstractReportReader; return type requires unchecked conversion found : java.util.Map required: java.util.Map<org.jcoderz.phoenix.report.ResourceInfo,java.util.List<org.jcoderz.phoenix.report.jaxb.Item>>
d (5) 124 : 34 [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
w (6) 125 : 0 Method org.jcoderz.phoenix.report.FindBugsReportReader.getItems() uses instanceof on multiple types to arbitrate logic
d (7) 146 : 33 [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Map
d (8) 151 : 31 [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
c (9) 201 : 23 Nested if-else depth is 3 (max allowed is 2).