From a00b76dd1b57d56fd06797b79b6b62bd1b5fcf9d Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 25 May 2012 15:51:44 +0200 Subject: [PATCH] Fix some quality flaws --- .../core/sensors/ReviewWorkflowDecorator.java | 5 +++-- .../org/sonar/batch/bootstrapper/Batch.java | 2 ++ .../core/persistence/dialect/DialectUtils.java | 8 ++++---- .../sonar/core/review/ReviewPredicates.java | 18 ++++++++++-------- .../duplications/DuplicationPredicates.java | 6 ++++-- .../java/org/sonar/graph/FeedbackEdge.java | 4 +++- .../resources/DefaultProjectFileSystem.java | 5 +++-- .../org/sonar/api/resources/ResourceTypes.java | 5 +++-- .../sonar/server/plugins/ClassLoaderUtils.java | 15 +++++++++++++-- .../sonar/squid/indexer/QueryByMeasure.java | 3 ++- 10 files changed, 47 insertions(+), 24 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecorator.java index 3e27ee2f61b..b5195812e8e 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/ReviewWorkflowDecorator.java @@ -38,6 +38,7 @@ import org.sonar.core.NotDryRun; import org.sonar.core.review.ReviewDao; import org.sonar.core.review.ReviewDto; +import javax.annotation.Nullable; import java.util.*; @NotDryRun @@ -164,8 +165,8 @@ public class ReviewWorkflowDecorator implements Decorator { } private static final class ViolationToPermanentIdFunction implements Function { - public Integer apply(Violation violation) { - return violation.getPermanentId(); + public Integer apply(@Nullable Violation violation) { + return (violation != null ? violation.getPermanentId() : null); } } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java index 9deee34e35e..254cd8c4f25 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrapper/Batch.java @@ -21,6 +21,7 @@ package org.sonar.batch.bootstrapper; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import org.slf4j.LoggerFactory; import org.sonar.api.batch.bootstrap.ProjectReactor; import org.sonar.batch.bootstrap.BootstrapModule; import org.sonar.batch.bootstrap.Module; @@ -77,6 +78,7 @@ public final class Batch { bootstrapModule.stop(); } catch (Exception e) { // never throw exceptions in a finally block + LoggerFactory.getLogger(Batch.class).error("Error while stopping batch", e); } } } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/DialectUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/DialectUtils.java index 71d07c0ac90..573b34d2c96 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/DialectUtils.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/DialectUtils.java @@ -24,6 +24,7 @@ import com.google.common.collect.Iterators; import org.apache.commons.lang.StringUtils; import org.sonar.api.utils.SonarException; +import javax.annotation.Nullable; import java.util.NoSuchElementException; public final class DialectUtils { @@ -42,12 +43,11 @@ public final class DialectUtils { } private static Dialect findByJdbcUrl(final String jdbcConnectionUrl) { - Dialect match = findDialect(new Predicate() { - public boolean apply(Dialect dialect) { - return dialect.matchesJdbcURL(StringUtils.trimToEmpty(jdbcConnectionUrl)); + return findDialect(new Predicate() { + public boolean apply(@Nullable Dialect dialect) { + return dialect != null && dialect.matchesJdbcURL(StringUtils.trimToEmpty(jdbcConnectionUrl)); } }); - return match; } private static Dialect findById(final String dialectId) { diff --git a/sonar-core/src/main/java/org/sonar/core/review/ReviewPredicates.java b/sonar-core/src/main/java/org/sonar/core/review/ReviewPredicates.java index 4529d0186f0..4fc6e3ff12b 100644 --- a/sonar-core/src/main/java/org/sonar/core/review/ReviewPredicates.java +++ b/sonar-core/src/main/java/org/sonar/core/review/ReviewPredicates.java @@ -22,6 +22,8 @@ package org.sonar.core.review; import com.google.common.base.Predicate; import org.apache.commons.lang.ArrayUtils; +import javax.annotation.Nullable; + /** * @since 2.14 */ @@ -53,8 +55,8 @@ public final class ReviewPredicates { this.statuses = statuses; } - public boolean apply(ReviewDto review) { - return ArrayUtils.contains(statuses, review.getStatus()); + public boolean apply(@Nullable ReviewDto review) { + return review!=null && ArrayUtils.contains(statuses, review.getStatus()); } } @@ -65,8 +67,8 @@ public final class ReviewPredicates { this.resolutions = resolutions; } - public boolean apply(ReviewDto review) { - return ArrayUtils.contains(resolutions, review.getResolution()); + public boolean apply(@Nullable ReviewDto review) { + return review!=null && ArrayUtils.contains(resolutions, review.getResolution()); } } @@ -76,8 +78,8 @@ public final class ReviewPredicates { private ManualViolationPredicate() { } - public boolean apply(ReviewDto review) { - return review.isManualViolation(); + public boolean apply(@Nullable ReviewDto review) { + return review!=null && review.isManualViolation(); } } @@ -87,8 +89,8 @@ public final class ReviewPredicates { private ManualSeverityPredicate() { } - public boolean apply(ReviewDto review) { - return review.isManualSeverity(); + public boolean apply(@Nullable ReviewDto review) { + return review!=null && review.isManualSeverity(); } } } diff --git a/sonar-duplications/src/main/java/org/sonar/duplications/DuplicationPredicates.java b/sonar-duplications/src/main/java/org/sonar/duplications/DuplicationPredicates.java index a6e05179fae..ce38086485a 100644 --- a/sonar-duplications/src/main/java/org/sonar/duplications/DuplicationPredicates.java +++ b/sonar-duplications/src/main/java/org/sonar/duplications/DuplicationPredicates.java @@ -23,6 +23,8 @@ import com.google.common.annotations.Beta; import com.google.common.base.Predicate; import org.sonar.duplications.index.CloneGroup; +import javax.annotation.Nullable; + @Beta public final class DuplicationPredicates { @@ -40,8 +42,8 @@ public final class DuplicationPredicates { this.min = min; } - public boolean apply(CloneGroup input) { - return input.getLengthInUnits() >= min; + public boolean apply(@Nullable CloneGroup input) { + return input != null && input.getLengthInUnits() >= min; } } diff --git a/sonar-graph/src/main/java/org/sonar/graph/FeedbackEdge.java b/sonar-graph/src/main/java/org/sonar/graph/FeedbackEdge.java index a8eccb9ad71..1d3a0f636ad 100644 --- a/sonar-graph/src/main/java/org/sonar/graph/FeedbackEdge.java +++ b/sonar-graph/src/main/java/org/sonar/graph/FeedbackEdge.java @@ -19,6 +19,8 @@ */ package org.sonar.graph; +import org.apache.commons.lang.math.NumberUtils; + public class FeedbackEdge implements Comparable { private Edge edge; @@ -53,7 +55,7 @@ public class FeedbackEdge implements Comparable { if (this.getRelativeWeight() < feedbackEdge.getRelativeWeight()) { return -1; } - if (this.getRelativeWeight() == feedbackEdge.getRelativeWeight()) { + if (NumberUtils.compare(this.getRelativeWeight(), feedbackEdge.getRelativeWeight())==0) { return this.getEdge().getFrom().toString().compareTo(feedbackEdge.getEdge().getFrom().toString()); } return 1; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java index 0c829ab7cdc..7afea1cad57 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java @@ -35,6 +35,7 @@ import org.sonar.api.utils.Logs; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.WildcardPattern; +import javax.annotation.Nullable; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; @@ -51,8 +52,8 @@ import java.util.*; public class DefaultProjectFileSystem implements ProjectFileSystem { protected static final Predicate DIRECTORY_EXISTS = new Predicate() { - public boolean apply(File input) { - return input.exists() && input.isDirectory(); + public boolean apply(@Nullable File input) { + return input != null && input.exists() && input.isDirectory(); } }; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java index 6fbcc46f299..306042d40d0 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java @@ -30,6 +30,7 @@ import com.google.common.collect.Maps; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; +import javax.annotation.Nullable; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -42,8 +43,8 @@ import java.util.Map; public final class ResourceTypes implements BatchComponent, ServerComponent { public static final Predicate AVAILABLE_FOR_FILTERS = new Predicate() { - public boolean apply(ResourceType input) { - return Boolean.TRUE.equals(input.getBooleanProperty("availableForFilters")); + public boolean apply(@Nullable ResourceType input) { + return input != null && Boolean.TRUE.equals(input.getBooleanProperty("availableForFilters")); } }; diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java b/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java index da957fc62a1..6bb90f623d4 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java +++ b/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java @@ -24,6 +24,7 @@ import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.CharEncoding; import org.apache.commons.lang.StringUtils; +import org.slf4j.LoggerFactory; import javax.annotation.Nullable; import java.io.File; @@ -92,6 +93,8 @@ public final class ClassLoaderUtils { * @return a list of relative paths, for example {"org/sonar/sqale", "org/sonar/sqale/foo", "org/sonar/sqale/foo/bar.txt}. Never null. */ public static Collection listResources(ClassLoader classLoader, String rootPath, Predicate predicate) { + String jarPath = null; + JarFile jar = null; try { Collection paths = Lists.newArrayList(); rootPath = StringUtils.removeStart(rootPath, "/"); @@ -109,8 +112,8 @@ public final class ClassLoaderUtils { if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) { rootDirectory = StringUtils.substringBeforeLast(rootPath, "/"); } - String jarPath = root.getPath().substring(5, root.getPath().indexOf("!")); //strip out only the JAR file - JarFile jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8)); + jarPath = root.getPath().substring(5, root.getPath().indexOf("!")); //strip out only the JAR file + jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8)); Enumeration entries = jar.entries(); while (entries.hasMoreElements()) { String name = entries.nextElement().getName(); @@ -122,6 +125,14 @@ public final class ClassLoaderUtils { return paths; } catch (Exception e) { throw Throwables.propagate(e); + } finally { + if (jar != null) { + try { + jar.close(); + } catch (Exception e) { + LoggerFactory.getLogger(ClassLoaderUtils.class).error("Fail to close JAR file: " + jarPath, e); + } + } } } diff --git a/sonar-squid/src/main/java/org/sonar/squid/indexer/QueryByMeasure.java b/sonar-squid/src/main/java/org/sonar/squid/indexer/QueryByMeasure.java index e4fd0f37347..0451d7526f9 100644 --- a/sonar-squid/src/main/java/org/sonar/squid/indexer/QueryByMeasure.java +++ b/sonar-squid/src/main/java/org/sonar/squid/indexer/QueryByMeasure.java @@ -20,6 +20,7 @@ package org.sonar.squid.indexer; +import org.apache.commons.lang.math.NumberUtils; import org.sonar.squid.api.Query; import org.sonar.squid.api.SourceCode; import org.sonar.squid.measures.Metric; @@ -52,7 +53,7 @@ public class QueryByMeasure implements Query { public boolean match(SourceCode unit) { switch (operator) { case EQUALS: - return unit.getDouble(metric) == value; + return NumberUtils.compare(unit.getDouble(metric), value)==0; case GREATER_THAN: return unit.getDouble(metric) > value; case GREATER_THAN_EQUALS: -- 2.39.5