From 424cffa7da8a34ab30cf4426cb3ee897ba01c743 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 16 Aug 2013 11:54:32 +0200 Subject: [PATCH] Fix quality flaws --- .../core/issue/IssueTrackingDecorator.java | 2 +- .../core/timemachine/TendencyAnalyser.java | 8 ++++---- .../core/timemachine/VariationDecorator.java | 8 ++++---- .../org/sonar/batch/bootstrap/ExtensionUtils.java | 2 +- .../org/sonar/batch/bootstrap/ServerClient.java | 2 +- .../org/sonar/batch/components/PastSnapshot.java | 6 +++--- .../components/PastSnapshotFinderByDate.java | 2 +- .../batch/index/DefaultResourcePersister.java | 2 +- .../org/sonar/batch/issue/DefaultIssuable.java | 2 +- .../java/org/sonar/batch/issue/IssueFilters.java | 2 +- .../batch/scan/DefaultProjectBootstrapper.java | 15 +++++++-------- .../sonar/batch/scan/DeprecatedJsonReport.java | 2 +- .../batch/scan/filesystem/ExclusionFilters.java | 8 ++++---- .../java/org/sonar/core/i18n/RuleI18nManager.java | 2 +- .../java/org/sonar/core/issue/IssueUpdater.java | 2 +- .../sonar/core/measure/MeasureFilterExecutor.java | 4 ++-- .../org/sonar/core/measure/MeasureFilterSql.java | 2 +- .../sonar/core/plugins/PluginClassloaders.java | 6 +++--- .../sonar/api/utils/command/CommandException.java | 2 +- 19 files changed, 39 insertions(+), 40 deletions(-) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java index 9685cd217f8..80501ae5c0b 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingDecorator.java @@ -186,7 +186,7 @@ public class IssueTrackingDecorator implements Decorator { Rule rule = ruleFinder.findByKey(issue.ruleKey()); if (manualIssue) { // Manual rules are not declared in Quality profiles, so no need to check ActiveRule - boolean isRemovedRule = (rule == null || Rule.STATUS_REMOVED.equals(rule.getStatus())); + boolean isRemovedRule = rule == null || Rule.STATUS_REMOVED.equals(rule.getStatus()); issue.setEndOfLife(forceEndOfLife || isRemovedRule); issue.setOnDisabledRule(isRemovedRule); } else { diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TendencyAnalyser.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TendencyAnalyser.java index 666eaefb3bb..6923ec0fa94 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TendencyAnalyser.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TendencyAnalyser.java @@ -68,9 +68,9 @@ public class TendencyAnalyser { if (nullValuesYList || nbrPoints == 1) { return null; } - double n0 = (((nbrPoints) * (sumXY)) - ((sumX) * (sumY))); - double d = (((nbrPoints) * (sumXPower2)) - ((sumX) * (sumX))); - double n1 = (((sumY) * (sumXPower2)) - ((sumX) * (sumXY))); + double n0 = ((nbrPoints * sumXY) - (sumX * sumY)); + double d = ((nbrPoints * sumXPower2) - (sumX * sumX)); + double n1 = ((sumY * sumXPower2) - (sumX * sumXY)); SlopeData result = new SlopeData(); @@ -94,7 +94,7 @@ public class TendencyAnalyser { if (sumXPower2 == 0 || sumYPower2 == 0) { result.setCorrelationRate(0.0); } else { - result.setCorrelationRate((sumXY) / (Math.sqrt(sumXPower2 * sumYPower2))); + result.setCorrelationRate(sumXY / Math.sqrt(sumXPower2 * sumYPower2)); } return result; diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java index 3dc3631e6b9..c081abb44e8 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java @@ -93,10 +93,10 @@ public class VariationDecorator implements Decorator { // for each measure, search equivalent past measure for (Measure measure : context.getMeasures(MeasuresFilters.all())) { // compare with past measure - Integer metricId = (measure.getMetric().getId() != null ? measure.getMetric().getId() : metricFinder.findByKey(measure.getMetric().getKey()).getId()); - Integer characteristicId = (measure.getCharacteristic() != null ? measure.getCharacteristic().getId() : null); + Integer metricId = measure.getMetric().getId() != null ? measure.getMetric().getId() : metricFinder.findByKey(measure.getMetric().getKey()).getId(); + Integer characteristicId = measure.getCharacteristic() != null ? measure.getCharacteristic().getId() : null; Integer personId = measure.getPersonId(); - Integer ruleId = (measure instanceof RuleMeasure ? ((RuleMeasure) measure).getRule().getId() : null); + Integer ruleId = measure instanceof RuleMeasure ? ((RuleMeasure) measure).getRule().getId() : null; Object[] pastMeasure = pastMeasuresByKey.get(new MeasureKey(metricId, characteristicId, personId, ruleId)); if (updateVariation(measure, pastMeasure, index)) { @@ -107,7 +107,7 @@ public class VariationDecorator implements Decorator { boolean updateVariation(Measure measure, Object[] pastMeasure, int index) { if (pastMeasure != null && PastMeasuresLoader.hasValue(pastMeasure) && measure.getValue() != null) { - double variation = (measure.getValue().doubleValue() - PastMeasuresLoader.getValue(pastMeasure)); + double variation = measure.getValue() - PastMeasuresLoader.getValue(pastMeasure); measure.setVariation(index, variation); return true; } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java index ce585828cc2..1111e7fbfd2 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ExtensionUtils.java @@ -68,7 +68,7 @@ public class ExtensionUtils { } public static boolean isType(Object extension, Class extensionClass) { - Class clazz = (extension instanceof Class ? (Class) extension : extension.getClass()); + Class clazz = extension instanceof Class ? (Class) extension : extension.getClass(); return extensionClass.isAssignableFrom(clazz); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java index a046369d920..fe75a71053d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java @@ -87,7 +87,7 @@ public class ServerClient implements BatchComponent { try { return IOUtils.toString(inputSupplier.getInput(), "UTF-8"); } catch (HttpDownloader.HttpException e) { - throw (wrapHttpException ? handleHttpException(e) : e); + throw wrapHttpException ? handleHttpException(e) : e; } catch (IOException e) { throw new SonarException(String.format("Unable to request: %s", pathStartingWithSlash), e); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java index e252433c3ff..24c0ae618c8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java @@ -73,7 +73,7 @@ public class PastSnapshot { } public Date getDate() { - return (projectSnapshot != null ? projectSnapshot.getCreatedAt() : null); + return projectSnapshot != null ? projectSnapshot.getCreatedAt() : null; } public String getMode() { @@ -90,11 +90,11 @@ public class PastSnapshot { } Integer getProjectSnapshotId() { - return (projectSnapshot != null ? projectSnapshot.getId() : null); + return projectSnapshot != null ? projectSnapshot.getId() : null; } public String getQualifier() { - return (projectSnapshot != null ? projectSnapshot.getQualifier() : null); + return projectSnapshot != null ? projectSnapshot.getQualifier() : null; } public Date getTargetDate() { diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java index 5dc9b0d0eb4..3767e174b39 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java @@ -58,6 +58,6 @@ public class PastSnapshotFinderByDate implements BatchExtension { .setMaxResults(1) .getResultList(); - return (snapshots.isEmpty() ? null : snapshots.get(0)); + return snapshots.isEmpty() ? null : snapshots.get(0); } } \ No newline at end of file diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java index 0d754d215d6..15b0933f77c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java @@ -194,7 +194,7 @@ public final class DefaultResourcePersister implements ResourcePersister { if (snapshots.isEmpty()) { snapshots = session.getResults(Snapshot.class, "resourceId", resourceId, "version", version, "scope", Scopes.PROJECT, "qualifier", Qualifiers.LIBRARY); } - return (snapshots.isEmpty() ? null : snapshots.get(0)); + return snapshots.isEmpty() ? null : snapshots.get(0); } /** diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java index 5f5ead3d3f1..0be18b5dff9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/DefaultIssuable.java @@ -50,7 +50,7 @@ public class DefaultIssuable implements Issuable { @Override public boolean addIssue(Issue issue) { - return scanIssues.initAndAddIssue(((DefaultIssue) issue)); + return scanIssues.initAndAddIssue((DefaultIssue) issue); } @SuppressWarnings("unchecked") diff --git a/sonar-batch/src/main/java/org/sonar/batch/issue/IssueFilters.java b/sonar-batch/src/main/java/org/sonar/batch/issue/IssueFilters.java index d33d1fcce07..190b4ea1916 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/issue/IssueFilters.java +++ b/sonar-batch/src/main/java/org/sonar/batch/issue/IssueFilters.java @@ -50,7 +50,7 @@ public class IssueFilters implements BatchExtension { } } if (!deprecatedFilters.isEmpty()) { - Violation v = (violation != null ? violation : deprecatedViolations.toViolation(issue)); + Violation v = violation != null ? violation : deprecatedViolations.toViolation(issue); return !deprecatedFilters.isIgnored(v); } return true; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/DefaultProjectBootstrapper.java b/sonar-batch/src/main/java/org/sonar/batch/scan/DefaultProjectBootstrapper.java index e8f1a6b5fc4..b141ff79ffe 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/DefaultProjectBootstrapper.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/DefaultProjectBootstrapper.java @@ -20,6 +20,7 @@ package org.sonar.batch.scan; import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import org.apache.commons.io.IOUtils; import org.apache.commons.io.filefilter.AndFileFilter; @@ -74,14 +75,12 @@ class DefaultProjectBootstrapper implements ProjectBootstrapper { private static final String PROPERTY_OLD_TESTS = "tests"; private static final String PROPERTY_OLD_BINARIES = "binaries"; private static final String PROPERTY_OLD_LIBRARIES = "libraries"; - private static final Map DEPRECATED_PROPS_TO_NEW_PROPS = new HashMap() { - { - put(PROPERTY_OLD_SOURCES, PROPERTY_SOURCES); - put(PROPERTY_OLD_TESTS, PROPERTY_TESTS); - put(PROPERTY_OLD_BINARIES, PROPERTY_BINARIES); - put(PROPERTY_OLD_LIBRARIES, PROPERTY_LIBRARIES); - } - }; + private static final Map DEPRECATED_PROPS_TO_NEW_PROPS = ImmutableMap.of( + PROPERTY_OLD_SOURCES, PROPERTY_SOURCES, + PROPERTY_OLD_TESTS, PROPERTY_TESTS, + PROPERTY_OLD_BINARIES, PROPERTY_BINARIES, + PROPERTY_OLD_LIBRARIES, PROPERTY_LIBRARIES + ); /** * @since 1.4 diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/DeprecatedJsonReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/DeprecatedJsonReport.java index 4f20681d120..10a61278be7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/DeprecatedJsonReport.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/DeprecatedJsonReport.java @@ -119,7 +119,7 @@ public class DeprecatedJsonReport implements BatchComponent { .name("rule_repository").value(issue.ruleKey().repository()) .name("rule_name").value(ruleName(issue.ruleKey())) .name("switched_off").value(Issue.RESOLUTION_FALSE_POSITIVE.equals(issue.resolution())) - .name("is_new").value((issue.isNew())) + .name("is_new").value(issue.isNew()) .name("created_at").value(DateUtils.formatDateTime(issue.creationDate())) .endObject(); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java index 1028417cd2d..e8e3eab9943 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilters.java @@ -57,7 +57,7 @@ public class ExclusionFilters implements FileSystemFilter, ResourceFilter, Batch } public boolean accept(File file, Context context) { - PathPattern[] inclusionPatterns = (context.type() == FileType.TEST ? testInclusions() : sourceInclusions()); + PathPattern[] inclusionPatterns = context.type() == FileType.TEST ? testInclusions() : sourceInclusions(); if (inclusionPatterns.length > 0) { boolean matchInclusion = false; for (PathPattern pattern : inclusionPatterns) { @@ -67,7 +67,7 @@ public class ExclusionFilters implements FileSystemFilter, ResourceFilter, Batch return false; } } - PathPattern[] exclusionPatterns = (context.type() == FileType.TEST ? testExclusions() : sourceExclusions()); + PathPattern[] exclusionPatterns = context.type() == FileType.TEST ? testExclusions() : sourceExclusions(); for (PathPattern pattern : exclusionPatterns) { if (pattern.match(context)) { return false; @@ -78,11 +78,11 @@ public class ExclusionFilters implements FileSystemFilter, ResourceFilter, Batch public boolean isIgnored(Resource resource) { if (ResourceUtils.isFile(resource)) { - PathPattern[] inclusionPatterns = (ResourceUtils.isUnitTestClass(resource) ? testInclusions() : sourceInclusions()); + PathPattern[] inclusionPatterns = ResourceUtils.isUnitTestClass(resource) ? testInclusions() : sourceInclusions(); if (isIgnoredByInclusions(resource, inclusionPatterns)) { return true; } - PathPattern[] exclusionPatterns = (ResourceUtils.isUnitTestClass(resource) ? testExclusions() : sourceExclusions()); + PathPattern[] exclusionPatterns = ResourceUtils.isUnitTestClass(resource) ? testExclusions() : sourceExclusions(); return isIgnoredByExclusions(resource, exclusionPatterns); } return false; diff --git a/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java b/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java index 3316edc27c4..b256c223fb7 100644 --- a/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java +++ b/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java @@ -67,7 +67,7 @@ public class RuleI18nManager implements RuleI18n, ServerExtension, BatchExtensio public String getDescription(String repositoryKey, String ruleKey, Locale locale) { String relatedProperty = new StringBuilder().append(RULE_PREFIX).append(repositoryKey).append(".").append(ruleKey).append(NAME_SUFFIX).toString(); - Locale localeWithoutCountry = (locale.getCountry() == null ? locale : new Locale(locale.getLanguage())); + Locale localeWithoutCountry = locale.getCountry() == null ? locale : new Locale(locale.getLanguage()); String ruleDescriptionFilePath = "rules/" + repositoryKey + "/" + ruleKey + ".html"; String description = i18nManager.messageFromFile(localeWithoutCountry, ruleDescriptionFilePath, relatedProperty, true); if (description == null) { diff --git a/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java b/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java index 1262c9edf0e..c5330050a9b 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java @@ -171,7 +171,7 @@ public class IssueUpdater implements BatchComponent, ServerComponent { } public void setCloseDate(DefaultIssue issue, @Nullable Date d, IssueChangeContext context) { - Date dateWithoutMilliseconds = (d == null ? null : DateUtils.truncate(d, Calendar.SECOND)); + Date dateWithoutMilliseconds = d == null ? null : DateUtils.truncate(d, Calendar.SECOND); if (!Objects.equal(dateWithoutMilliseconds, issue.closeDate())) { issue.setCloseDate(d); issue.setUpdateDate(context.date()); diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java index 81168944cf2..a80c621fcd5 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterExecutor.java @@ -82,8 +82,8 @@ public class MeasureFilterExecutor implements ServerComponent { } static boolean isValid(MeasureFilter filter, MeasureFilterContext context) { - boolean valid = (Strings.isNullOrEmpty(filter.getBaseResourceKey()) || context.getBaseSnapshot()!=null); - valid &= (filter.getBaseResourceId()==null || context.getBaseSnapshot()!=null); + boolean valid = Strings.isNullOrEmpty(filter.getBaseResourceKey()) || context.getBaseSnapshot()!=null; + valid &= filter.getBaseResourceId()==null || context.getBaseSnapshot()!=null; valid &= !(filter.isOnBaseResourceChildren() && context.getBaseSnapshot() == null); valid &= !(filter.isOnFavourites() && context.getUserId() == null); valid &= validateMeasureConditions(filter); diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java index 90862fc1826..ea072f115ae 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java @@ -153,7 +153,7 @@ class MeasureFilterSql { if (filter.isOnBaseResourceChildren()) { sb.append(" AND s.parent_snapshot_id=").append(baseSnapshot.getId()); } else { - Long rootSnapshotId = (baseSnapshot.getRootId() != null ? baseSnapshot.getRootId() : baseSnapshot.getId()); + Long rootSnapshotId = baseSnapshot.getRootId() != null ? baseSnapshot.getRootId() : baseSnapshot.getId(); sb.append(" AND s.root_snapshot_id=").append(rootSnapshotId); sb.append(" AND s.path LIKE '").append(StringUtils.defaultString(baseSnapshot.getPath())).append(baseSnapshot.getId()).append(".%'"); } diff --git a/sonar-core/src/main/java/org/sonar/core/plugins/PluginClassloaders.java b/sonar-core/src/main/java/org/sonar/core/plugins/PluginClassloaders.java index 8999a8f6f05..5beddfdd531 100644 --- a/sonar-core/src/main/java/org/sonar/core/plugins/PluginClassloaders.java +++ b/sonar-core/src/main/java/org/sonar/core/plugins/PluginClassloaders.java @@ -133,7 +133,7 @@ public class PluginClassloaders { } catch (UnsupportedClassVersionError e) { throw new SonarException("The plugin " + plugin.getKey() + " is not supported with Java " + SystemUtils.JAVA_VERSION_TRIMMED, e); - } catch (Throwable e) { + } catch (Exception e) { throw new SonarException("Fail to build the classloader of " + plugin.getKey(), e); } } @@ -158,7 +158,7 @@ public class PluginClassloaders { } catch (UnsupportedClassVersionError e) { throw new SonarException("The plugin " + plugin.getKey() + " is not supported with Java " + SystemUtils.JAVA_VERSION_TRIMMED, e); - } catch (Throwable e) { + } catch (Exception e) { throw new SonarException("Fail to extend the plugin " + plugin.getBasePlugin() + " for " + plugin.getKey(), e); } } @@ -225,7 +225,7 @@ public class PluginClassloaders { } catch (UnsupportedClassVersionError e) { throw new SonarException("The plugin " + plugin.getKey() + " is not supported with Java " + SystemUtils.JAVA_VERSION_TRIMMED, e); - } catch (Throwable e) { + } catch (Exception e) { throw new SonarException("Fail to load plugin " + plugin.getKey(), e); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java index d7b4f322133..dd9a09fab64 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandException.java @@ -23,7 +23,7 @@ import javax.annotation.Nullable; public final class CommandException extends RuntimeException { - private transient Command command = null; + private final Command command; public CommandException(Command command, String message, @Nullable Throwable throwable) { super(message + " [command: " + command + "]", throwable); -- 2.39.5