Skip to content

Commit

Permalink
LPS-58330 Make sure that checking for unused variables or imports in …
Browse files Browse the repository at this point in the history
…jsp files doesn't break stuff when using ant format-source-local-changes
  • Loading branch information
hhuijser authored and brianchandotcom committed Sep 2, 2015
1 parent 1efdb7f commit fd9235a
Showing 1 changed file with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.IOException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -86,6 +87,41 @@ protected void addImportCounts(String content) {
}
}

protected List<String> addIncludedAndReferencedFileNames(
List<String> fileNames, Set<String> checkedFileNames) {

Set<String> includedAndReferencedFileNames = new HashSet<>();

for (String fileName : fileNames) {
if (!checkedFileNames.add(fileName)) {
continue;
}

fileName = StringUtil.replace(
fileName, StringPool.BACK_SLASH, StringPool.SLASH);

includedAndReferencedFileNames.addAll(
getJSPIncludeFileNames(fileName, fileNames));
includedAndReferencedFileNames.addAll(
getJSPReferenceFileNames(fileName, fileNames));
}

if (includedAndReferencedFileNames.isEmpty()) {
return fileNames;
}

for (String fileName : includedAndReferencedFileNames) {
fileName = StringUtil.replace(
fileName, StringPool.SLASH, StringPool.BACK_SLASH);

if (!fileNames.contains(fileName)) {
fileNames.add(fileName);
}
}

return addIncludedAndReferencedFileNames(fileNames, checkedFileNames);
}

protected void addJSPUnusedImports(
String fileName, List<String> importLines,
List<String> unneededImports) {
Expand Down Expand Up @@ -376,11 +412,26 @@ protected List<String> doGetFileNames() throws Exception {

List<String> fileNames = getFileNames(excludes, getIncludes());

if (fileNames.isEmpty()) {
return fileNames;
}

List<String> allFileNames = null;

if (sourceFormatterArgs.isFormatLocalChanges()) {
allFileNames = getFileNames(
sourceFormatterArgs.getBaseDirName(), null, excludes,
getIncludes());
}
else {
allFileNames = fileNames;
}

try {
Pattern pattern = Pattern.compile(
"\\s*@\\s*include\\s*file=['\"](.*)['\"]");

for (String fileName : fileNames) {
for (String fileName : allFileNames) {
File file = new File(fileName);

fileName = StringUtil.replace(
Expand Down Expand Up @@ -422,7 +473,12 @@ protected List<String> doGetFileNames() throws Exception {
ReflectionUtil.throwException(e);
}

return fileNames;
if (!sourceFormatterArgs.isFormatLocalChanges()) {
return fileNames;
}

return addIncludedAndReferencedFileNames(
fileNames, new HashSet<String>());
}

protected String fixRedirectBackURL(String content) {
Expand Down Expand Up @@ -960,7 +1016,7 @@ protected List<String> getJSPDuplicateImports(
}

protected Set<String> getJSPIncludeFileNames(
String fileName, Set<String> fileNames) {
String fileName, Collection<String> fileNames) {

Set<String> includeFileNames = new HashSet<>();

Expand Down Expand Up @@ -1022,7 +1078,7 @@ protected Set<String> getJSPIncludeFileNames(
}

protected Set<String> getJSPReferenceFileNames(
String fileName, Set<String> fileNames) {
String fileName, Collection<String> fileNames) {

Set<String> referenceFileNames = new HashSet<>();

Expand Down

0 comments on commit fd9235a

Please sign in to comment.