Author: krosenvold Date: Thu Nov 19 06:50:28 2015 New Revision: 1715109 URL: http://svn.apache.org/viewvc?rev=1715109&view=rev Log: Fixed file handle leak
Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdViolationCheckMojo.java maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/stubs/DefaultConfigurationMavenProjectStub.java Modified: maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdViolationCheckMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdViolationCheckMojo.java?rev=1715109&r1=1715108&r2=1715109&view=diff ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdViolationCheckMojo.java (original) +++ maven/plugins/trunk/maven-pmd-plugin/src/main/java/org/apache/maven/plugin/pmd/PmdViolationCheckMojo.java Thu Nov 19 06:50:28 2015 @@ -19,19 +19,6 @@ package org.apache.maven.plugin.pmd; * under the License. */ -import java.io.File; -import java.io.FileInputStream; -import java.io.FileReader; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; -import java.util.Set; - import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.pmd.model.PmdErrorDetail; @@ -46,6 +33,19 @@ import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.Set; + /** * Fail the build if there were any PMD violations in the source code. * @@ -112,8 +112,11 @@ public class PmdViolationCheckMojo for ( final Entry<Object, Object> propEntry : props.entrySet() ) { final Set<String> excludedRuleSet = new HashSet<String>(); - final String className = propEntry.getKey().toString(); - final String[] excludedRules = propEntry.getValue().toString().split( "," ); + final String className = propEntry.getKey() + .toString(); + final String[] excludedRules = propEntry.getValue() + .toString() + .split( "," ); for ( final String excludedRule : excludedRules ) { excludedRuleSet.add( excludedRule.trim() ); @@ -141,7 +144,8 @@ public class PmdViolationCheckMojo { final String fileName = errorDetail.getFileName(); final int javaIdx = fileName.indexOf( "\\java\\" ); - return fileName.substring( javaIdx >= 0 ? javaIdx + 6 : 0, fileName.length() - 5 ).replace( '\\', '.' ); + return fileName.substring( javaIdx >= 0 ? javaIdx + 6 : 0, fileName.length() - 5 ) + .replace( '\\', '.' ); } } @@ -152,7 +156,9 @@ public class PmdViolationCheckMojo { StringBuilder buff = new StringBuilder( 100 ); - buff.append( "PMD " ).append( severity ).append( ": " ); + buff.append( "PMD " ) + .append( severity ) + .append( ": " ); if ( item.getViolationClass() != null ) { if ( item.getViolationPackage() != null ) @@ -168,32 +174,45 @@ public class PmdViolationCheckMojo } buff.append( ":" ); buff.append( item.getBeginline() ); - buff.append( " Rule:" ).append( item.getRule() ); - buff.append( " Priority:" ).append( item.getPriority() ); - buff.append( " " ).append( item.getText() ).append( "." ); + buff.append( " Rule:" ) + .append( item.getRule() ); + buff.append( " Priority:" ) + .append( item.getPriority() ); + buff.append( " " ) + .append( item.getText() ) + .append( "." ); - this.getLog().info( buff.toString() ); + this.getLog() + .info( buff.toString() ); } @Override protected List<Violation> getErrorDetails( File pmdFile ) throws XmlPullParserException, IOException { - PmdXpp3Reader reader = new PmdXpp3Reader(); - PmdErrorDetail details = reader.read( new FileReader( pmdFile ), false ); - - List<Violation> violations = new ArrayList<Violation>(); - for ( PmdFile file : details.getFiles() ) + final FileReader reader1 = new FileReader( pmdFile ); + try { - String fullPath = file.getName(); + PmdXpp3Reader reader = new PmdXpp3Reader(); + PmdErrorDetail details = reader.read( reader1, false ); - for ( Violation violation : file.getViolations() ) + List<Violation> violations = new ArrayList<Violation>(); + for ( PmdFile file : details.getFiles() ) { - violation.setFileName( getFilename( fullPath, violation.getViolationPackage() ) ); - violations.add( violation ); + String fullPath = file.getName(); + + for ( Violation violation : file.getViolations() ) + { + violation.setFileName( getFilename( fullPath, violation.getViolationPackage() ) ); + violations.add( violation ); + } } + return violations; + } + finally + { + reader1.close(); } - return violations; } @Override @@ -214,7 +233,8 @@ public class PmdViolationCheckMojo while ( StringUtils.isNotEmpty( pkg ) ) { - index = fullpath.substring( 0, index ).lastIndexOf( File.separatorChar ); + index = fullpath.substring( 0, index ) + .lastIndexOf( File.separatorChar ); int dot = pkg.indexOf( '.' ); Modified: maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/stubs/DefaultConfigurationMavenProjectStub.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/stubs/DefaultConfigurationMavenProjectStub.java?rev=1715109&r1=1715108&r2=1715109&view=diff ============================================================================== --- maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/stubs/DefaultConfigurationMavenProjectStub.java (original) +++ maven/plugins/trunk/maven-pmd-plugin/src/test/java/org/apache/maven/plugin/pmd/stubs/DefaultConfigurationMavenProjectStub.java Thu Nov 19 06:50:28 2015 @@ -31,6 +31,7 @@ import org.apache.maven.model.ReportPlug import org.apache.maven.model.Scm; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.codehaus.plexus.util.IOUtil; /** * @author <a href="mailto:och...@apache.org">Maria Odea Ching</a> @@ -48,17 +49,21 @@ public class DefaultConfigurationMavenPr MavenXpp3Reader pomReader = new MavenXpp3Reader(); Model model = null; + FileReader reader = null; try { - model = - pomReader.read( new FileReader( new File( getBasedir() - + "/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" ) ) ); + reader = new FileReader( new File( getBasedir() + "/src/test/resources/unit/default-configuration/default-configuration-plugin-config.xml" ) ); + model = pomReader.read( reader ); setModel( model ); } catch ( Exception e ) { } + finally + { + IOUtil.close( reader ); + } setGroupId( model.getGroupId() ); setArtifactId( model.getArtifactId() ); @@ -72,12 +77,14 @@ public class DefaultConfigurationMavenPr setScm( scm ); Build build = new Build(); - build.setFinalName( model.getBuild().getFinalName() ); + build.setFinalName( model.getBuild() + .getFinalName() ); build.setDirectory( getBasedir() + "/target/test/unit/default-configuration/target" ); build.setSourceDirectory( getBasedir() + "/src/test/resources/unit/default-configuration" ); setBuild( build ); - setReportPlugins( model.getReporting().getPlugins() ); + setReportPlugins( model.getReporting() + .getPlugins() ); String basedir = getBasedir().getAbsolutePath(); List<String> compileSourceRoots = new ArrayList<String>();