Strip basename from reported secondary filename in StrictDuplicateCodeCheck.
Required moving getStrippedFileName() from Checker to Utils.
This commit is contained in:
parent
741454e7f1
commit
e708beb537
|
|
@ -38,6 +38,7 @@ import com.puppycrawl.tools.checkstyle.api.FilterSet;
|
|||
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
|
||||
import com.puppycrawl.tools.checkstyle.api.MessageDispatcher;
|
||||
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
|
||||
import com.puppycrawl.tools.checkstyle.api.Utils;
|
||||
|
||||
/**
|
||||
* This class provides the functionality to check a set of files.
|
||||
|
|
@ -170,6 +171,7 @@ public class Checker extends AutomaticBean
|
|||
context.add("classLoader", mLoader);
|
||||
context.add("moduleFactory", mModuleFactory);
|
||||
context.add("severity", mSeverityLevel.getName());
|
||||
context.add("basedir", mBasedir);
|
||||
mChildContext = context;
|
||||
}
|
||||
|
||||
|
|
@ -279,16 +281,7 @@ public class Checker extends AutomaticBean
|
|||
*/
|
||||
private String getStrippedFileName(final String aFileName)
|
||||
{
|
||||
final String stripped;
|
||||
if ((mBasedir == null) || !aFileName.startsWith(mBasedir)) {
|
||||
stripped = aFileName;
|
||||
}
|
||||
else {
|
||||
// making the assumption that there is text after basedir
|
||||
final int skipSep = mBasedir.endsWith(File.separator) ? 0 : 1;
|
||||
stripped = aFileName.substring(mBasedir.length() + skipSep);
|
||||
}
|
||||
return stripped;
|
||||
return Utils.getStrippedFileName(mBasedir, aFileName);
|
||||
}
|
||||
|
||||
/** @param aBasedir the base directory to strip off in filenames */
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ package com.puppycrawl.tools.checkstyle.api;
|
|||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.LineNumberReader;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
|
@ -209,4 +210,26 @@ public final class Utils
|
|||
final int i = aType.lastIndexOf(".");
|
||||
return (i == -1) ? aType : aType.substring(i + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a stripped down version of a filename.
|
||||
* @param aBasedir the prefix to strip off the original filename
|
||||
* @param aFileName the original filename
|
||||
* @return the filename where an initial prefix of basedir is stripped
|
||||
*/
|
||||
public static String getStrippedFileName(
|
||||
final String aBasedir, final String aFileName)
|
||||
{
|
||||
final String stripped;
|
||||
if ((aBasedir == null) || !aFileName.startsWith(aBasedir)) {
|
||||
stripped = aFileName;
|
||||
}
|
||||
else {
|
||||
// making the assumption that there is text after basedir
|
||||
final int skipSep = aBasedir.endsWith(File.separator) ? 0 : 1;
|
||||
stripped = aFileName.substring(aBasedir.length() + skipSep);
|
||||
}
|
||||
return stripped;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -157,6 +157,9 @@ public final class StrictDuplicateCodeCheck extends AbstractFileSetCheck
|
|||
/** number of lines that have to be idential for reporting duplicates */
|
||||
private int mMin = DEFAULT_MIN_DUPLICATE_LINES;
|
||||
|
||||
/** the basedir to strip off in filenames */
|
||||
private String mBasedir;
|
||||
|
||||
/** the checksums of all files that are currently checked */
|
||||
private long[][] mLineChecksums;
|
||||
|
||||
|
|
@ -197,6 +200,12 @@ public final class StrictDuplicateCodeCheck extends AbstractFileSetCheck
|
|||
mMin = aMin;
|
||||
}
|
||||
|
||||
/** @param aBasedir the base directory to strip off in filenames */
|
||||
public void setBasedir(String aBasedir)
|
||||
{
|
||||
mBasedir = aBasedir;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck#process
|
||||
*/
|
||||
|
|
@ -432,8 +441,10 @@ public final class StrictDuplicateCodeCheck extends AbstractFileSetCheck
|
|||
{
|
||||
final Integer dupLines = new Integer(aEquivalent);
|
||||
final Integer startLine = new Integer(aJLine + 1);
|
||||
final String fileName =
|
||||
Utils.getStrippedFileName(mBasedir, aJFile.getPath());
|
||||
log(aILine + 1, "duplicates.lines",
|
||||
new Object[]{dupLines, aJFile, startLine});
|
||||
new Object[]{dupLines, fileName, startLine});
|
||||
mDuplicates += 1;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue