Skip to content

Commit

Permalink
[#12] deal with IOException
Browse files Browse the repository at this point in the history
 - style updates
 - remove pom/java comment (issue exists)
  • Loading branch information
bmarwell committed Jan 21, 2022
1 parent dac102d commit 55185a3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/codehaus/mojo/taglist/FileAnalyser.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public FileAnalyser( TagListReport report, List<TagClass> tagClasses )
multipleLineCommentsOn = report.isMultipleLineComments();
emptyCommentsOn = report.isEmptyComments();
log = report.getLog();
sourceDirs = report.constructSourceDirs();
sourceDirs = report.getSourceDirs();
encoding = report.getInputEncoding();
locale = report.getLocale();
noCommentString = report.getBundle().getString( "report.taglist.nocomment" );
Expand Down
87 changes: 59 additions & 28 deletions src/main/java/org/codehaus/mojo/taglist/TagListReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.maven.doxia.siterenderer.Renderer;
import org.apache.maven.model.ReportPlugin;
Expand All @@ -38,6 +39,7 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;
import org.apache.maven.shared.utils.io.FileUtils;
import org.codehaus.mojo.taglist.beans.FileReport;
import org.codehaus.mojo.taglist.beans.TagReport;
import org.codehaus.mojo.taglist.output.TagListXMLComment;
Expand All @@ -49,7 +51,6 @@
import org.codehaus.mojo.taglist.tags.InvalidTagException;
import org.codehaus.mojo.taglist.tags.TagClass;
import org.codehaus.mojo.taglist.tags.TagFactory;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.PathTool;
import org.codehaus.plexus.util.StringUtils;
Expand Down Expand Up @@ -192,6 +193,8 @@ public class TagListReport

private String[] tags;

private AtomicReference<List> sourceDirs = new AtomicReference<>();

/**
* {@inheritDoc}
*
Expand Down Expand Up @@ -424,7 +427,7 @@ private String getRelativePath( File location )
*/
public boolean canGenerateReport()
{
boolean canGenerate = !constructSourceDirs().isEmpty();
boolean canGenerate = !getSourceDirs().isEmpty();
if ( aggregate && !getProject().isExecutionRoot() )
{
canGenerate = false;
Expand All @@ -434,11 +437,11 @@ public boolean canGenerateReport()

/**
* Removes empty dirs from the list.
*
*
* @param sourceDirectories the original list of directories.
* @return a new list containing only non empty dirs.
*/
private List pruneSourceDirs( List sourceDirectories )
private List pruneSourceDirs( List sourceDirectories ) throws IOException
{
List pruned = new ArrayList( sourceDirectories.size() );
for ( Iterator i = sourceDirectories.iterator(); i.hasNext(); )
Expand All @@ -454,45 +457,46 @@ private List pruneSourceDirs( List sourceDirectories )

/**
* Checks whether the given directory contains source files.
*
*
* @param dir the source directory.
* @return true if the folder or one of its subfolders contains at least 1 source file that matches includes/excludes.
* @return true if the folder or one of its subfolders contains at least 1 source file that matches
* includes/excludes.
*/
private boolean hasSources( File dir )
private boolean hasSources( File dir ) throws IOException
{
boolean found = false;
if ( dir.exists() && dir.isDirectory() )
{
try {
if ( ! FileUtils.getFiles( dir, getIncludesCommaSeparated(), getExcludesCommaSeparated() ).isEmpty() ) {
found = true;
}
} catch (IOException e) {
// should never get here
getLog().error("Error whilst trying to scan the directory " + dir.getAbsolutePath(), e);
if ( !FileUtils.getFiles( dir, getIncludesCommaSeparated(), getExcludesCommaSeparated() ).isEmpty() )
{
return true;
}

File[] files = dir.listFiles();
if ( files != null ) {
for ( int i = 0; i < files.length && !found; i++ ) {
if ( files != null )
{
for ( int i = 0; i < files.length; i++ )
{
File currentFile = files[i];
if ( currentFile.isDirectory() ) {
if ( currentFile.isDirectory() )
{
boolean hasSources = hasSources( currentFile );
if ( hasSources ) {
found = true;
if ( hasSources )
{
return true;
}
}
}
}
}
return found;
return false;
}

/**
* Construct the list of source directories to analyze.
*
*
* @return the list of dirs.
*/
public List constructSourceDirs()
private List constructSourceDirs()
{
List dirs = new ArrayList( getProject().getCompileSourceRoots() );
if ( !skipTestSources )
Expand All @@ -506,7 +510,6 @@ public List constructSourceDirs()
{
MavenProject reactorProject = (MavenProject) i.next();

// TODO should this be more like ! "pom".equals(...)
if ( "java".equals( reactorProject.getArtifact().getArtifactHandler().getLanguage() ) )
{
dirs.addAll( reactorProject.getCompileSourceRoots() );
Expand All @@ -518,18 +521,46 @@ public List constructSourceDirs()
}
}

dirs = pruneSourceDirs( dirs );
/*
* This try-catch is needed due to a missing declared exception in the
* 'canGenerateReport()' method. For this reason, neither the 'canGenerateReport()'
* nor the 'constructSourceDirs()' can throw exceptions.
* The exception itself is caused by a declaration from the FileUtils, but never used
* there. The FileUtils.getFiles() should be replaced by an NIO filter at some point.
*/
try
{
dirs = pruneSourceDirs( dirs );
}
catch ( IOException javaIoIOException )
{
getLog().warn( "Unable to prune source dirs.", javaIoIOException );
}

return dirs;
}

protected List getSourceDirs()
{
if ( sourceDirs.get() == null )
{
sourceDirs.compareAndSet( null, constructSourceDirs() );
}

return sourceDirs.get();
}

/**
* Get the files to include, as a comma separated list of patterns.
*/
String getIncludesCommaSeparated()
{
if ( includes != null ) {
return String.join(",", includes);
} else {
if ( includes != null )
{
return String.join( ",", includes );
}
else
{
return "";
}
}
Expand Down

0 comments on commit 55185a3

Please sign in to comment.