Project Report: fawkez

Packagesummary org.jcoderz.phoenix.report

org.jcoderz.phoenix.report.CpdReportReader

LineHitsNoteSource
1  /*
2   * $Id: CpdReportReader.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.FileNotFoundException;
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.cpd.jaxb.Duplication;
48  import org.jcoderz.phoenix.cpd.jaxb.PmdCpd;
49  import org.jcoderz.phoenix.report.jaxb.Item;
50  import org.jcoderz.phoenix.report.jaxb.ObjectFactory;
51  
52  /**
53   * CPD Report Reader.
54   *
55   * @author Michael Griffel
56   */
57  public final class CpdReportReader
58        extends AbstractReportReader
59  {
60     /** JAXB context path. */
61     public static final String CPD_JAXB_CONTEXT_PATH
62        = "org.jcoderz.phoenix.cpd.jaxb";
63  
640    private static final String CLASSNAME = CpdReportReader.class.getName();
650    private static final Logger logger = Logger.getLogger(CLASSNAME);
66  
67     private PmdCpd mReportDocument;
68  
69 (1)   public CpdReportReader ()
70           throws JAXBException
71     {
720       super(CPD_JAXB_CONTEXT_PATH);
730    }
74  
75     /** {@inheritDoc} */
76     public void parse (File f) throws JAXBException, FileNotFoundException
77     {
780(2)      mReportDocument = (PmdCpd) getUnmarshaller().unmarshal(
79              new FileInputStream(f));
800    }
81  
82     /** {@inheritDoc} */
83 (3)   protected Map getItems () throws JAXBException
84     {
850       final Map result = new HashMap();
86  
870       for (final Iterator iterator
88                = mReportDocument.getDuplication().iterator();
890            iterator.hasNext();)
90        {
910          final Duplication duplication = (Duplication) iterator.next();
920          final List filez = duplication.getFile();
930          for (int i = 0; i < filez.size(); i++)
94           {
950             final org.jcoderz.phoenix.cpd.jaxb.File file
96                 = (org.jcoderz.phoenix.cpd.jaxb.File) filez.get(i);
97  
980             final String key = normalizeFileName(file.getPath());
990             final ResourceInfo info = ResourceInfo.lookup(key);
1000             if (info != null)
101              {
1020                final Item item = new ObjectFactory().createItem();
1030                item.setMessage(constructMessage(i, filez, duplication));
1040                item.setOrigin(Origin.CPD);
1050                item.setSeverity(Severity.CPD);
1060                item.setFindingType(CpdFindingType.NAME);
1070                item.setLine(file.getLine());
1080                item.setEndLine(file.getLine() + duplication.getLines());
109  
1100                if (result.get(info) == null)
111                 {
1120                   final List list = new ArrayList();
1130(4)                  list.add(item);
1140(5)                  result.put(info, list);
1150                }
116                 else
117                 {
1180                   final List list = (List) result.get(info);
1190(6)                  list.add(item);
120                 }
1210             }
122              else
123              {
1240                logger.finer("Ignoring findings for resource " + key);
125              }
126           }
1270       }
1280       return result;
129     }
130  
131     /**
132      * @param file
133    * @param filez
134    * @return
135      */
136     private String constructMessage (int currentIndex, List filez,
137           Duplication duplication)
138     {
1390       final StringBuilder sb = new StringBuilder();
1400       sb.append("Copied and pasted code. ");
1410       sb.append(duplication.getTokens());
1420       sb.append(" equal tokens (");
1430       sb.append(duplication.getLines());
1440       sb.append(" lines) found in ");
1450       sb.append(filez.size()).append(" locations. See also: ");
146  
1470       for (int i = 0; i < filez.size(); i++)
148        {
1490          if (i == currentIndex)
150           {
1510             continue; // skip current finding
152           }
153  
1540          final org.jcoderz.phoenix.cpd.jaxb.File file
155                 = (org.jcoderz.phoenix.cpd.jaxb.File) filez.get(i);
156  
1570          final ResourceInfo info = ResourceInfo.lookup(file.getPath());
158           final String resourceName;
1590          if (info != null)
160           {
1610             resourceName = info.getPackage() + "." + info.getClassname();
162           }
163           else
164           {
1650             resourceName = file.getPath();
166           }
1670(7)         sb.append(resourceName + ":" + file.getLine() + " ");
168        }
1690       return sb.toString();
170     }
171  
172  }

Findings in this File

c (1) 69 : 4 Missing a Javadoc comment.
w (2) 78 : 0 Method org.jcoderz.phoenix.report.CpdReportReader.parse(File) may fail to clean up stream or resource of type java.io.InputStream
d (3) 83 : 18 getItems() in org.jcoderz.phoenix.report.CpdReportReader 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 (4) 113 : 27 [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
d (5) 114 : 29 [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Map
d (6) 119 : 27 [unchecked] unchecked call to add(E) as a member of the raw type java.util.List
w (7) 167 : 0 method org.jcoderz.phoenix.report.CpdReportReader.constructMessage(int, List, Duplication) passes simple concatenating string in StringBuffer or StringBuilder append