diff options
26 files changed, 125 insertions, 261 deletions
diff --git a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubRestClient.java b/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubRestClient.java index 9d67ce4f705..580fc46565f 100644 --- a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubRestClient.java +++ b/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubRestClient.java @@ -83,9 +83,7 @@ public class GitHubRestClient { Response response = scribe.execute(request); int code = response.getCode(); switch (code) { - case HttpURLConnection.HTTP_MOVED_TEMP: - case HttpURLConnection.HTTP_NOT_FOUND: - case HttpURLConnection.HTTP_NO_CONTENT: + case HttpURLConnection.HTTP_MOVED_TEMP, HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_NO_CONTENT: LOGGER.trace("Orgs response received : {}", code); return code == HttpURLConnection.HTTP_NO_CONTENT; default: diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/MeasureToMeasureDto.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/MeasureToMeasureDto.java index 8779bd95cb2..b417944d440 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/MeasureToMeasureDto.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/MeasureToMeasureDto.java @@ -67,11 +67,7 @@ public class MeasureToMeasureDto { private static String data(Measure in) { switch (in.getValueType()) { - case NO_VALUE: - case BOOLEAN: - case INT: - case LONG: - case DOUBLE: + case NO_VALUE, BOOLEAN, INT, LONG, DOUBLE: return in.getData(); case STRING: return in.getStringValue(); @@ -88,20 +84,12 @@ public class MeasureToMeasureDto { */ @CheckForNull private static Double valueAsDouble(Measure measure) { - switch (measure.getValueType()) { - case BOOLEAN: - return measure.getBooleanValue() ? 1.0D : 0.0D; - case INT: - return (double) measure.getIntValue(); - case LONG: - return (double) measure.getLongValue(); - case DOUBLE: - return measure.getDoubleValue(); - case NO_VALUE: - case STRING: - case LEVEL: - default: - return null; - } + return switch (measure.getValueType()) { + case BOOLEAN -> measure.getBooleanValue() ? 1.0D : 0.0D; + case INT -> (double) measure.getIntValue(); + case LONG -> (double) measure.getLongValue(); + case DOUBLE -> measure.getDoubleValue(); + default -> null; + }; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/BlobColumnDef.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/BlobColumnDef.java index f26640bb767..a47e58666a4 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/BlobColumnDef.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/BlobColumnDef.java @@ -37,17 +37,12 @@ public class BlobColumnDef extends AbstractColumnDef { @Override public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case MsSql.ID: - return "VARBINARY(MAX)"; - case Oracle.ID: - case H2.ID: - return "BLOB"; - case PostgreSql.ID: - return "BYTEA"; - default: - throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); - } + return switch (dialect.getId()) { + case MsSql.ID -> "VARBINARY(MAX)"; + case Oracle.ID, H2.ID -> "BLOB"; + case PostgreSql.ID -> "BYTEA"; + default -> throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); + }; } public static Builder newBlobColumnDefBuilder() { diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/BooleanColumnDef.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/BooleanColumnDef.java index cc0d302b4a5..057ef1a1df6 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/BooleanColumnDef.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/BooleanColumnDef.java @@ -50,8 +50,7 @@ public class BooleanColumnDef extends AbstractColumnDef { @Override public String generateSqlType(Dialect dialect) { switch (dialect.getId()) { - case PostgreSql.ID: - case H2.ID: + case PostgreSql.ID, H2.ID: return "BOOLEAN"; case Oracle.ID: return "NUMBER(1)"; diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/ClobColumnDef.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/ClobColumnDef.java index 52edf32821b..d5015e1e477 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/ClobColumnDef.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/ClobColumnDef.java @@ -47,8 +47,7 @@ public class ClobColumnDef extends AbstractColumnDef { switch (dialect.getId()) { case MsSql.ID: return "NVARCHAR (MAX)"; - case Oracle.ID: - case H2.ID: + case Oracle.ID, H2.ID: return "CLOB"; case PostgreSql.ID: return "TEXT"; diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/DecimalColumnDef.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/DecimalColumnDef.java index f2ebd095a08..2dd8e7002fe 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/DecimalColumnDef.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/DecimalColumnDef.java @@ -58,17 +58,12 @@ public class DecimalColumnDef extends AbstractColumnDef { @Override public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case PostgreSql.ID: - case Oracle.ID: - return String.format("NUMERIC (%s,%s)", precision, scale); - case MsSql.ID: - return String.format("DECIMAL (%s,%s)", precision, scale); - case H2.ID: - return "DOUBLE"; - default: - throw new UnsupportedOperationException(String.format("Unknown dialect '%s'", dialect.getId())); - } + return switch (dialect.getId()) { + case PostgreSql.ID, Oracle.ID -> String.format("NUMERIC (%s,%s)", precision, scale); + case MsSql.ID -> String.format("DECIMAL (%s,%s)", precision, scale); + case H2.ID -> "DOUBLE"; + default -> throw new UnsupportedOperationException(String.format("Unknown dialect '%s'", dialect.getId())); + }; } public static class Builder { diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/IntegerColumnDef.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/IntegerColumnDef.java index 128c2ce9800..3242e155334 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/IntegerColumnDef.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/IntegerColumnDef.java @@ -43,17 +43,12 @@ public class IntegerColumnDef extends AbstractColumnDef { @Override public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case PostgreSql.ID: - case H2.ID: - return "INTEGER"; - case MsSql.ID: - return "INT"; - case Oracle.ID: - return "NUMBER(38,0)"; - default: - throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); - } + return switch (dialect.getId()) { + case PostgreSql.ID, H2.ID -> "INTEGER"; + case MsSql.ID -> "INT"; + case Oracle.ID -> "NUMBER(38,0)"; + default -> throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); + }; } public static class Builder { diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/TimestampColumnDef.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/TimestampColumnDef.java index 4074b2e5f99..18372dc634a 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/TimestampColumnDef.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/TimestampColumnDef.java @@ -48,17 +48,12 @@ public class TimestampColumnDef extends AbstractColumnDef { @Override public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case MsSql.ID: - return "DATETIME"; - case Oracle.ID: - return "TIMESTAMP (6)"; - case H2.ID: - case PostgreSql.ID: - return "TIMESTAMP"; - default: - throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); - } + return switch (dialect.getId()) { + case MsSql.ID -> "DATETIME"; + case Oracle.ID -> "TIMESTAMP (6)"; + case H2.ID, PostgreSql.ID -> "TIMESTAMP"; + default -> throw new IllegalArgumentException("Unsupported dialect id " + dialect.getId()); + }; } public static class Builder { diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/TinyIntColumnDef.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/TinyIntColumnDef.java index 622e8c6a4d0..419645d3da6 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/TinyIntColumnDef.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/TinyIntColumnDef.java @@ -45,17 +45,12 @@ public class TinyIntColumnDef extends AbstractColumnDef { @Override public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case PostgreSql.ID: - return "SMALLINT"; - case Oracle.ID: - return "NUMBER(3)"; - case MsSql.ID: - case H2.ID: - return "TINYINT"; - default: - throw new UnsupportedOperationException(String.format("Unknown dialect '%s'", dialect.getId())); - } + return switch (dialect.getId()) { + case PostgreSql.ID -> "SMALLINT"; + case Oracle.ID -> "NUMBER(3)"; + case MsSql.ID, H2.ID -> "TINYINT"; + default -> throw new UnsupportedOperationException(String.format("Unknown dialect '%s'", dialect.getId())); + }; } public static class Builder { diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropConstraintBuilder.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropConstraintBuilder.java index 52d8ce20898..7acf545e77f 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropConstraintBuilder.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropConstraintBuilder.java @@ -65,14 +65,9 @@ public class DropConstraintBuilder { } private String createSqlStatement() { - switch (dialect.getId()) { - case MsSql.ID: - case Oracle.ID: - case PostgreSql.ID: - case H2.ID: - return "ALTER TABLE " + tableName + " DROP CONSTRAINT " + constraintName; - default: - throw new IllegalStateException("Unsupported dialect for drop of constraint: " + dialect); - } + return switch (dialect.getId()) { + case MsSql.ID, Oracle.ID, PostgreSql.ID, H2.ID -> "ALTER TABLE " + tableName + " DROP CONSTRAINT " + constraintName; + default -> throw new IllegalStateException("Unsupported dialect for drop of constraint: " + dialect); + }; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropTableBuilder.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropTableBuilder.java index bce1e12699b..6ac5948e373 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropTableBuilder.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropTableBuilder.java @@ -42,18 +42,14 @@ public class DropTableBuilder { } public List<String> build() { - switch (dialect.getId()) { - case Oracle.ID: - return forOracle(tableName); - case H2.ID: - case PostgreSql.ID: - return singletonList("drop table if exists " + tableName); - case MsSql.ID: + return switch (dialect.getId()) { + case Oracle.ID -> forOracle(tableName); + case H2.ID, PostgreSql.ID -> singletonList("drop table if exists " + tableName); + case MsSql.ID -> // "if exists" is supported only since MSSQL 2016. - return singletonList("drop table " + tableName); - default: - throw new IllegalStateException("Unsupported DB: " + dialect.getId()); - } + singletonList("drop table " + tableName); + default -> throw new IllegalStateException("Unsupported DB: " + dialect.getId()); + }; } private static List<String> forOracle(String tableName) { diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/RenameTableBuilder.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/RenameTableBuilder.java index 415f209e004..6cf70f766b4 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/RenameTableBuilder.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/RenameTableBuilder.java @@ -73,8 +73,7 @@ public class RenameTableBuilder { private List<String> createSqlStatement() { switch (dialect.getId()) { - case H2.ID: - case PostgreSql.ID: + case H2.ID, PostgreSql.ID: return singletonList("ALTER TABLE " + name + " RENAME TO " + newName); case MsSql.ID: return singletonList("EXEC sp_rename '" + name + "', '" + newName + "'"); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DropIndexBuilder.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DropIndexBuilder.java index 1757c272e3c..d1a013cf428 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DropIndexBuilder.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DropIndexBuilder.java @@ -61,16 +61,11 @@ class DropIndexBuilder { } private String createSqlStatement() { - switch (dialect.getId()) { - case MsSql.ID: - return "DROP INDEX " + indexName + " ON " + tableName; - case Oracle.ID: - return "DROP INDEX " + indexName; - case H2.ID: - case PostgreSql.ID: - return "DROP INDEX IF EXISTS " + indexName; - default: - throw new IllegalStateException("Unsupported dialect for drop of index: " + dialect); - } + return switch (dialect.getId()) { + case MsSql.ID -> "DROP INDEX " + indexName + " ON " + tableName; + case Oracle.ID -> "DROP INDEX " + indexName; + case H2.ID, PostgreSql.ID -> "DROP INDEX IF EXISTS " + indexName; + default -> throw new IllegalStateException("Unsupported dialect for drop of index: " + dialect); + }; } } diff --git a/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java b/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java index 9b1a45643cf..ca6fe276c4b 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java +++ b/server/sonar-main/src/main/java/org/sonar/application/SchedulerImpl.java @@ -114,17 +114,12 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr } private static ManagedProcessHandler.Timeout stopTimeoutFor(ProcessId processId, AppSettings settings) { - switch (processId) { - case ELASTICSEARCH: - return HARD_STOP_TIMEOUT; - case WEB_SERVER: - return newTimeout(getStopTimeoutMs(settings, WEB_GRACEFUL_STOP_TIMEOUT), TimeUnit.MILLISECONDS); - case COMPUTE_ENGINE: - return newTimeout(getStopTimeoutMs(settings, CE_GRACEFUL_STOP_TIMEOUT), TimeUnit.MILLISECONDS); - case APP: - default: - throw new IllegalArgumentException("Unsupported processId " + processId); - } + return switch (processId) { + case ELASTICSEARCH -> HARD_STOP_TIMEOUT; + case WEB_SERVER -> newTimeout(getStopTimeoutMs(settings, WEB_GRACEFUL_STOP_TIMEOUT), TimeUnit.MILLISECONDS); + case COMPUTE_ENGINE -> newTimeout(getStopTimeoutMs(settings, CE_GRACEFUL_STOP_TIMEOUT), TimeUnit.MILLISECONDS); + default -> throw new IllegalArgumentException("Unsupported processId " + processId); + }; } private static long getStopTimeoutMs(AppSettings settings, ProcessProperties.Property property) { @@ -385,8 +380,7 @@ public class SchedulerImpl implements Scheduler, ManagedProcessEventListener, Pr restartAsync(); } break; - case HARD_STOPPING: - case STOPPING: + case HARD_STOPPING, STOPPING: if (lastProcessStopped) { finalizeStop(); } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java index a08b332e000..202f45328a5 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java @@ -90,20 +90,14 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe @Override public Collection<EsQueueDto> prepareForRecovery(DbSession dbSession, Collection<String> projectUuids, Cause cause) { switch (cause) { - case MEASURE_CHANGE: - case PROJECT_TAGS_UPDATE: - case PERMISSION_CHANGE: + case MEASURE_CHANGE, PROJECT_TAGS_UPDATE, PERMISSION_CHANGE: // measures, tags and permissions are not part of type components/component return emptyList(); - - case PROJECT_CREATION: - case PROJECT_DELETION: - case PROJECT_KEY_UPDATE: + case PROJECT_CREATION, PROJECT_DELETION, PROJECT_KEY_UPDATE: List<EsQueueDto> items = projectUuids.stream() .map(branchUuid -> EsQueueDto.create(TYPE_COMPONENT.format(), branchUuid, null, branchUuid)) .collect(MoreCollectors.toArrayList(projectUuids.size())); return dbClient.esQueueDao().insert(dbSession, items); - default: // defensive case throw new IllegalStateException("Unsupported cause: " + cause); diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexer.java b/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexer.java index 34701bef9ab..975f73ccc01 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexer.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexer.java @@ -123,13 +123,9 @@ public class IssueIndexer implements ProjectIndexer, NeedAuthorizationIndexer { @Override public Collection<EsQueueDto> prepareForRecovery(DbSession dbSession, Collection<String> projectUuids, ProjectIndexer.Cause cause) { switch (cause) { - case PROJECT_CREATION: - // nothing to do, issues do not exist at project creation - case MEASURE_CHANGE: - case PROJECT_KEY_UPDATE: - case PROJECT_TAGS_UPDATE: - case PERMISSION_CHANGE: - // nothing to do. Measures, permissions, project key and tags are not used in type issues/issue + case PROJECT_CREATION, MEASURE_CHANGE, PROJECT_KEY_UPDATE, PROJECT_TAGS_UPDATE, PERMISSION_CHANGE: + // Nothing to do, issues do not exist at project creation + // Measures, permissions, project key and tags are not used in type issues/issue return emptyList(); case PROJECT_DELETION: diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexer.java b/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexer.java index 3819871ae55..86dbfc1eec6 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexer.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexer.java @@ -93,13 +93,9 @@ public class ProjectMeasuresIndexer implements ProjectIndexer, NeedAuthorization case PERMISSION_CHANGE: // nothing to do, permissions are not used in type projectmeasures/projectmeasure return Collections.emptyList(); - case MEASURE_CHANGE: - case PROJECT_KEY_UPDATE: - // project must be re-indexed because key is used in this index - case PROJECT_CREATION: - // provisioned projects are supported by WS api/components/search_projects - case PROJECT_TAGS_UPDATE: - case PROJECT_DELETION: + case MEASURE_CHANGE, PROJECT_KEY_UPDATE, PROJECT_CREATION, PROJECT_TAGS_UPDATE, PROJECT_DELETION: + // when MEASURE_CHANGE or PROJECT_KEY_UPDATE project must be re-indexed because key is used in this index + // when PROJECT_CREATION provisioned projects are supported by WS api/components/search_projects List<EsQueueDto> items = projectUuids.stream() .map(projectUuid -> EsQueueDto.create(TYPE_PROJECT_MEASURES.format(), projectUuid, null, projectUuid)) .collect(MoreCollectors.toArrayList(projectUuids.size())); diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/ConditionEvaluator.java b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/ConditionEvaluator.java index 7ca1c0768a7..6c2c7b7a62d 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/ConditionEvaluator.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/qualitygate/ConditionEvaluator.java @@ -128,19 +128,12 @@ class ConditionEvaluator { } private static Comparable getNumericValue(ValueType type, double value) { - switch (type) { - case INT: - case RATING: - return (int) value; - case FLOAT: - case PERCENT: - return value; - case MILLISEC: - case WORK_DUR: - return (long) value; - default: - throw new IllegalArgumentException("Condition on numeric value is not allowed for type " + type); - } + return switch (type) { + case INT, RATING -> (int) value; + case FLOAT, PERCENT -> value; + case MILLISEC, WORK_DUR -> (long) value; + default -> throw new IllegalArgumentException("Condition on numeric value is not allowed for type " + type); + }; } private static int parseInteger(String value) { diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/webhook/WebhookCallerImpl.java b/server/sonar-server-common/src/main/java/org/sonar/server/webhook/WebhookCallerImpl.java index 65cba7e23bb..97bcefd7bbd 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/webhook/WebhookCallerImpl.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/webhook/WebhookCallerImpl.java @@ -101,21 +101,17 @@ public class WebhookCallerImpl implements WebhookCaller { private Response execute(Request request) throws IOException { Response response = okHttpClient.newCall(request).execute(); - switch (response.code()) { - case HTTP_MOVED_PERM: - case HTTP_MOVED_TEMP: - case HTTP_TEMP_REDIRECT: - case HTTP_PERM_REDIRECT: + return switch (response.code()) { + case HTTP_MOVED_PERM, HTTP_MOVED_TEMP, HTTP_TEMP_REDIRECT, HTTP_PERM_REDIRECT -> // OkHttpClient does not follow the redirect with the same HTTP method. A POST is // redirected to a GET. Because of that the redirect must be manually // implemented. // See: // https://github.com/square/okhttp/blob/07309c1c7d9e296014268ebd155ebf7ef8679f6c/okhttp/src/main/java/okhttp3/internal/http/RetryAndFollowUpInterceptor.java#L316 // https://github.com/square/okhttp/issues/936#issuecomment-266430151 - return followPostRedirect(response); - default: - return response; - } + followPostRedirect(response); + default -> response; + }; } /** diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQueryFactory.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQueryFactory.java index 6650136199c..fa41d7b28f1 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQueryFactory.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQueryFactory.java @@ -321,8 +321,7 @@ public class IssueQueryFactory { setBranch(builder, components.get(0), request.getBranch(), request.getPullRequest(), dbSession); String qualifier = qualifiers.iterator().next(); switch (qualifier) { - case Qualifiers.VIEW: - case Qualifiers.SUBVIEW: + case Qualifiers.VIEW, Qualifiers.SUBVIEW: addViewsOrSubViews(builder, components); break; case Qualifiers.APP: @@ -338,8 +337,7 @@ public class IssueQueryFactory { case Qualifiers.DIRECTORY: addDirectories(builder, components); break; - case Qualifiers.FILE: - case Qualifiers.UNIT_TEST_FILE: + case Qualifiers.FILE, Qualifiers.UNIT_TEST_FILE: builder.componentUuids(components.stream().map(ComponentDto::uuid).collect(toList())); break; default: diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java index 7fc65f902f0..12b092a6a00 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java @@ -40,10 +40,10 @@ import org.sonar.server.es.IndexType; import org.sonar.server.es.IndexingResult; import org.sonar.server.es.OneToOneResilientIndexingListener; import org.sonar.server.es.ProjectIndexer; +import org.springframework.beans.factory.annotation.Autowired; import static java.util.Collections.emptyList; import static org.sonar.core.util.stream.MoreCollectors.toArrayList; -import org.springframework.beans.factory.annotation.Autowired; /** * Populates the types "authorization" of each index requiring project @@ -106,22 +106,15 @@ public class PermissionIndexer implements ProjectIndexer { @Override public Collection<EsQueueDto> prepareForRecovery(DbSession dbSession, Collection<String> projectUuids, ProjectIndexer.Cause cause) { - switch (cause) { - case MEASURE_CHANGE: - case PROJECT_KEY_UPDATE: - case PROJECT_TAGS_UPDATE: + return switch (cause) { + case MEASURE_CHANGE, PROJECT_KEY_UPDATE, PROJECT_TAGS_UPDATE -> // nothing to change. Measures, project key and tags are not part of this index - return emptyList(); - - case PROJECT_CREATION: - case PROJECT_DELETION: - case PERMISSION_CHANGE: - return insertIntoEsQueue(dbSession, projectUuids); - - default: + emptyList(); + case PROJECT_CREATION, PROJECT_DELETION, PERMISSION_CHANGE -> insertIntoEsQueue(dbSession, projectUuids); + default -> // defensive case throw new IllegalStateException("Unsupported cause: " + cause); - } + }; } private Collection<EsQueueDto> insertIntoEsQueue(DbSession dbSession, Collection<String> projectUuids) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java index 86407ec6e62..56ecd44fdb4 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/SearchProjectsAction.java @@ -291,14 +291,10 @@ public class SearchProjectsAction implements ComponentsWsAction { return Sets.newHashSet(Qualifiers.PROJECT); } - switch (edition.get()) { - case ENTERPRISE: - case DATACENTER: - case DEVELOPER: - return Sets.newHashSet(Qualifiers.PROJECT, Qualifiers.APP); - default: - return Sets.newHashSet(Qualifiers.PROJECT); - } + return switch (edition.get()) { + case ENTERPRISE, DATACENTER, DEVELOPER -> Sets.newHashSet(Qualifiers.PROJECT, Qualifiers.APP); + default -> Sets.newHashSet(Qualifiers.PROJECT); + }; } private static boolean hasFavoriteFilter(List<Criterion> criteria) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/MeasureValueFormatter.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/MeasureValueFormatter.java index 0540cc7fa11..b6049734459 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/MeasureValueFormatter.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/MeasureValueFormatter.java @@ -50,50 +50,26 @@ public class MeasureValueFormatter { @CheckForNull static String formatMeasureValue(double doubleValue, @Nullable String stringValue, MetricDto metric) { Metric.ValueType metricType = Metric.ValueType.valueOf(metric.getValueType()); - switch (metricType) { - case BOOL: - return formatBoolean(doubleValue); - case INT: - return formatInteger(doubleValue); - case MILLISEC: - case WORK_DUR: - return formatLong(doubleValue); - case FLOAT: - case PERCENT: - case RATING: - return String.valueOf(doubleValue); - case LEVEL: - case STRING: - case DATA: - case DISTRIB: - return stringValue; - default: - throw new IllegalArgumentException("Unsupported metric type: " + metricType.name()); - } + return switch (metricType) { + case BOOL -> formatBoolean(doubleValue); + case INT -> formatInteger(doubleValue); + case MILLISEC, WORK_DUR -> formatLong(doubleValue); + case FLOAT, PERCENT, RATING -> String.valueOf(doubleValue); + case LEVEL, STRING, DATA, DISTRIB -> stringValue; + default -> throw new IllegalArgumentException("Unsupported metric type: " + metricType.name()); + }; } static String formatNumericalValue(double value, MetricDto metric) { Metric.ValueType metricType = Metric.ValueType.valueOf(metric.getValueType()); - switch (metricType) { - case BOOL: - return formatBoolean(value); - case INT: - return formatInteger(value); - case MILLISEC: - case WORK_DUR: - return formatLong(value); - case FLOAT: - case PERCENT: - case RATING: - return String.valueOf(value); - case LEVEL: - case STRING: - case DATA: - case DISTRIB: - default: - throw new IllegalArgumentException(String.format("Unsupported metric type '%s' for numerical value", metricType.name())); - } + return switch (metricType) { + case BOOL -> formatBoolean(value); + case INT -> formatInteger(value); + case MILLISEC, WORK_DUR -> formatLong(value); + case FLOAT, PERCENT, RATING -> String.valueOf(value); + default -> throw new IllegalArgumentException(String.format("Unsupported metric type '%s' for numerical value", metricType.name())); + }; } private static String formatBoolean(double value) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/CurrentAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/CurrentAction.java index 708afcc5e29..17dfe192cb9 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/CurrentAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/CurrentAction.java @@ -221,13 +221,10 @@ public class CurrentAction implements UsersWsAction { if (!edition.isPresent()) { return false; } - switch (edition.get()) { - case ENTERPRISE: - case DATACENTER: - return true; - default: - return false; - } + return switch (edition.get()) { + case ENTERPRISE, DATACENTER -> true; + default -> false; + }; } private void cleanUserHomepageInDb(DbSession dbSession, UserDto user) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SetHomepageAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SetHomepageAction.java index 8c87eae9cb2..3c9f7d18168 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SetHomepageAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/user/ws/SetHomepageAction.java @@ -121,13 +121,10 @@ public class SetHomepageAction implements UsersWsAction { } else { return projectDto.getUuid(); } - case PORTFOLIO: - case APPLICATION: + case PORTFOLIO, APPLICATION: checkArgument(isNotBlank(componentParameter), PARAMETER_REQUIRED, type.name(), PARAM_COMPONENT); return componentFinder.getByKey(dbSession, componentParameter).uuid(); - case PORTFOLIOS: - case PROJECTS: - case ISSUES: + case PORTFOLIOS, PROJECTS, ISSUES: return null; default: throw new IllegalArgumentException(format("Unknown type '%s'", type.name())); diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/app/EmbeddedTomcat.java b/server/sonar-webserver/src/main/java/org/sonar/server/app/EmbeddedTomcat.java index 458b8312e5a..91596edbe7c 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/app/EmbeddedTomcat.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/app/EmbeddedTomcat.java @@ -82,19 +82,13 @@ class EmbeddedTomcat { if (webappContext == null) { return Status.DOWN; } - switch (webappContext.getState()) { - case NEW: - case INITIALIZING: - case INITIALIZED: - case STARTING_PREP: - case STARTING: - return Status.DOWN; - case STARTED: - return Status.UP; - default: + return switch (webappContext.getState()) { + case NEW, INITIALIZING, INITIALIZED, STARTING_PREP, STARTING -> Status.DOWN; + case STARTED -> Status.UP; + default -> // problem, stopped or failed - return Status.FAILED; - } + Status.FAILED; + }; } public enum Status { |