Skip to content

Commit 55185a3

Browse files
committed
[#12] deal with IOException
- style updates - remove pom/java comment (issue exists)
1 parent dac102d commit 55185a3

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

src/main/java/org/codehaus/mojo/taglist/FileAnalyser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public FileAnalyser( TagListReport report, List<TagClass> tagClasses )
129129
multipleLineCommentsOn = report.isMultipleLineComments();
130130
emptyCommentsOn = report.isEmptyComments();
131131
log = report.getLog();
132-
sourceDirs = report.constructSourceDirs();
132+
sourceDirs = report.getSourceDirs();
133133
encoding = report.getInputEncoding();
134134
locale = report.getLocale();
135135
noCommentString = report.getBundle().getString( "report.taglist.nocomment" );

src/main/java/org/codehaus/mojo/taglist/TagListReport.java

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.Locale;
3131
import java.util.ResourceBundle;
32+
import java.util.concurrent.atomic.AtomicReference;
3233

3334
import org.apache.maven.doxia.siterenderer.Renderer;
3435
import org.apache.maven.model.ReportPlugin;
@@ -38,6 +39,7 @@
3839
import org.apache.maven.project.MavenProject;
3940
import org.apache.maven.reporting.AbstractMavenReport;
4041
import org.apache.maven.reporting.MavenReportException;
42+
import org.apache.maven.shared.utils.io.FileUtils;
4143
import org.codehaus.mojo.taglist.beans.FileReport;
4244
import org.codehaus.mojo.taglist.beans.TagReport;
4345
import org.codehaus.mojo.taglist.output.TagListXMLComment;
@@ -49,7 +51,6 @@
4951
import org.codehaus.mojo.taglist.tags.InvalidTagException;
5052
import org.codehaus.mojo.taglist.tags.TagClass;
5153
import org.codehaus.mojo.taglist.tags.TagFactory;
52-
import org.codehaus.plexus.util.FileUtils;
5354
import org.codehaus.plexus.util.IOUtil;
5455
import org.codehaus.plexus.util.PathTool;
5556
import org.codehaus.plexus.util.StringUtils;
@@ -192,6 +193,8 @@ public class TagListReport
192193

193194
private String[] tags;
194195

196+
private AtomicReference<List> sourceDirs = new AtomicReference<>();
197+
195198
/**
196199
* {@inheritDoc}
197200
*
@@ -424,7 +427,7 @@ private String getRelativePath( File location )
424427
*/
425428
public boolean canGenerateReport()
426429
{
427-
boolean canGenerate = !constructSourceDirs().isEmpty();
430+
boolean canGenerate = !getSourceDirs().isEmpty();
428431
if ( aggregate && !getProject().isExecutionRoot() )
429432
{
430433
canGenerate = false;
@@ -434,11 +437,11 @@ public boolean canGenerateReport()
434437

435438
/**
436439
* Removes empty dirs from the list.
437-
*
440+
*
438441
* @param sourceDirectories the original list of directories.
439442
* @return a new list containing only non empty dirs.
440443
*/
441-
private List pruneSourceDirs( List sourceDirectories )
444+
private List pruneSourceDirs( List sourceDirectories ) throws IOException
442445
{
443446
List pruned = new ArrayList( sourceDirectories.size() );
444447
for ( Iterator i = sourceDirectories.iterator(); i.hasNext(); )
@@ -454,45 +457,46 @@ private List pruneSourceDirs( List sourceDirectories )
454457

455458
/**
456459
* Checks whether the given directory contains source files.
457-
*
460+
*
458461
* @param dir the source directory.
459-
* @return true if the folder or one of its subfolders contains at least 1 source file that matches includes/excludes.
462+
* @return true if the folder or one of its subfolders contains at least 1 source file that matches
463+
* includes/excludes.
460464
*/
461-
private boolean hasSources( File dir )
465+
private boolean hasSources( File dir ) throws IOException
462466
{
463-
boolean found = false;
464467
if ( dir.exists() && dir.isDirectory() )
465468
{
466-
try {
467-
if ( ! FileUtils.getFiles( dir, getIncludesCommaSeparated(), getExcludesCommaSeparated() ).isEmpty() ) {
468-
found = true;
469-
}
470-
} catch (IOException e) {
471-
// should never get here
472-
getLog().error("Error whilst trying to scan the directory " + dir.getAbsolutePath(), e);
469+
if ( !FileUtils.getFiles( dir, getIncludesCommaSeparated(), getExcludesCommaSeparated() ).isEmpty() )
470+
{
471+
return true;
473472
}
473+
474474
File[] files = dir.listFiles();
475-
if ( files != null ) {
476-
for ( int i = 0; i < files.length && !found; i++ ) {
475+
if ( files != null )
476+
{
477+
for ( int i = 0; i < files.length; i++ )
478+
{
477479
File currentFile = files[i];
478-
if ( currentFile.isDirectory() ) {
480+
if ( currentFile.isDirectory() )
481+
{
479482
boolean hasSources = hasSources( currentFile );
480-
if ( hasSources ) {
481-
found = true;
483+
if ( hasSources )
484+
{
485+
return true;
482486
}
483487
}
484488
}
485489
}
486490
}
487-
return found;
491+
return false;
488492
}
489493

490494
/**
491495
* Construct the list of source directories to analyze.
492-
*
496+
*
493497
* @return the list of dirs.
494498
*/
495-
public List constructSourceDirs()
499+
private List constructSourceDirs()
496500
{
497501
List dirs = new ArrayList( getProject().getCompileSourceRoots() );
498502
if ( !skipTestSources )
@@ -506,7 +510,6 @@ public List constructSourceDirs()
506510
{
507511
MavenProject reactorProject = (MavenProject) i.next();
508512

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

521-
dirs = pruneSourceDirs( dirs );
524+
/*
525+
* This try-catch is needed due to a missing declared exception in the
526+
* 'canGenerateReport()' method. For this reason, neither the 'canGenerateReport()'
527+
* nor the 'constructSourceDirs()' can throw exceptions.
528+
* The exception itself is caused by a declaration from the FileUtils, but never used
529+
* there. The FileUtils.getFiles() should be replaced by an NIO filter at some point.
530+
*/
531+
try
532+
{
533+
dirs = pruneSourceDirs( dirs );
534+
}
535+
catch ( IOException javaIoIOException )
536+
{
537+
getLog().warn( "Unable to prune source dirs.", javaIoIOException );
538+
}
539+
522540
return dirs;
523541
}
524542

543+
protected List getSourceDirs()
544+
{
545+
if ( sourceDirs.get() == null )
546+
{
547+
sourceDirs.compareAndSet( null, constructSourceDirs() );
548+
}
549+
550+
return sourceDirs.get();
551+
}
552+
525553
/**
526554
* Get the files to include, as a comma separated list of patterns.
527555
*/
528556
String getIncludesCommaSeparated()
529557
{
530-
if ( includes != null ) {
531-
return String.join(",", includes);
532-
} else {
558+
if ( includes != null )
559+
{
560+
return String.join( ",", includes );
561+
}
562+
else
563+
{
533564
return "";
534565
}
535566
}

0 commit comments

Comments
 (0)