Skip to content

Commit

Permalink
BE: Clean some Sonar issues (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
sixdouglas authored Feb 10, 2025
1 parent cb52c06 commit 844fbc9
Show file tree
Hide file tree
Showing 34 changed files with 172 additions and 184 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine} --illegal-access=permit</argLine>
<argLine>@{argLine}</argLine>
</configuration>
</plugin>
<plugin>
Expand Down
5 changes: 4 additions & 1 deletion api/src/main/java/io/kafbat/ui/exception/CelException.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.kafbat.ui.exception;

import lombok.Getter;

@Getter
public class CelException extends CustomBaseException {
private String celOriginalExpression;
private final String celOriginalExpression;

public CelException(String celOriginalExpression, String errorMessage) {
super("CEL error. Original expression: %s. Error message: %s".formatted(celOriginalExpression, errorMessage));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ private Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
Throwable throwable = getError(request);

// validation and params binding errors
if (throwable instanceof WebExchangeBindException) {
return render((WebExchangeBindException) throwable, request);
if (throwable instanceof WebExchangeBindException webExchangeBindException) {
return render(webExchangeBindException, request);
}

// requests mapping & access errors
if (throwable instanceof ResponseStatusException) {
return render((ResponseStatusException) throwable, request);
if (throwable instanceof ResponseStatusException responseStatusException) {
return render(responseStatusException, request);
}

// custom exceptions
if (throwable instanceof CustomBaseException) {
return render((CustomBaseException) throwable, request);
if (throwable instanceof CustomBaseException customBaseException) {
return render(customBaseException, request);
}

return renderDefault(throwable, request);
Expand Down Expand Up @@ -151,9 +151,7 @@ private String requestId(ServerRequest request) {
}

private Consumer<HttpHeaders> headers(ServerRequest request) {
return (HttpHeaders headers) -> {
CorsGlobalConfiguration.fillCorsHeader(headers, request.exchange().getRequest());
};
return (HttpHeaders headers) -> CorsGlobalConfiguration.fillCorsHeader(headers, request.exchange().getRequest());
}

private BigDecimal currentTimestamp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public static Provider fromString(String name) {
}

public static class Name {
public static String GOOGLE = "google";
public static String GITHUB = "github";
public static String COGNITO = "cognito";
public static final String GOOGLE = "google";
public static final String GITHUB = "github";
public static final String COGNITO = "cognito";

public static String OAUTH = "oauth";
public static final String OAUTH = "oauth";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Serde.Serializer serializer(String topic, Serde.Target type) {
return inputString -> {
inputString = inputString.trim();
// it is actually a hack to provide ability to sent empty array as a key/value
if (inputString.length() == 0) {
if (inputString.isEmpty()) {
return new byte[] {};
}
return decoder.decode(inputString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Serializer serializer(String topic, Target type) {
return input -> {
input = input.trim();
// it is a hack to provide ability to sent empty array as a key/value
if (input.length() == 0) {
if (input.isEmpty()) {
return new byte[] {};
}
return HexFormat.of().parseHex(prepareInputForParse(input));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,9 @@ private ProtoFile loadKnownProtoFile(String path, Descriptors.FileDescriptor fil
String protoFileString = null;
// know type file contains either message or enum
if (!fileDescriptor.getMessageTypes().isEmpty()) {
protoFileString = new ProtobufSchema(fileDescriptor.getMessageTypes().get(0)).canonicalString();
protoFileString = new ProtobufSchema(fileDescriptor.getMessageTypes().getFirst()).canonicalString();
} else if (!fileDescriptor.getEnumTypes().isEmpty()) {
protoFileString = new ProtobufSchema(fileDescriptor.getEnumTypes().get(0)).canonicalString();
protoFileString = new ProtobufSchema(fileDescriptor.getEnumTypes().getFirst()).canonicalString();
} else {
throw new IllegalStateException();
}
Expand Down
14 changes: 6 additions & 8 deletions api/src/main/java/io/kafbat/ui/service/audit/AuditRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,12 @@ static OperationResult successful() {
}

static OperationResult error(Throwable th) {
OperationError err = OperationError.UNRECOGNIZED_ERROR;
if (th instanceof AccessDeniedException) {
err = OperationError.ACCESS_DENIED;
} else if (th instanceof ValidationException) {
err = OperationError.VALIDATION_ERROR;
} else if (th instanceof CustomBaseException) {
err = OperationError.EXECUTION_ERROR;
}
OperationError err = switch (th) {
case AccessDeniedException ignored -> OperationError.ACCESS_DENIED;
case ValidationException ignored -> OperationError.VALIDATION_ERROR;
case CustomBaseException ignored -> OperationError.EXECUTION_ERROR;
case null, default -> OperationError.UNRECOGNIZED_ERROR;
};
return new OperationResult(false, err);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.apache.commons.collections.CollectionUtils;
Expand All @@ -22,7 +22,7 @@ static ConnectorInfo extract(String className,
ConnectorTypeDTO type,
Map<String, Object> config,
List<String> topicsFromApi, // can be empty for old Connect API versions
Function<String, String> topicOddrnBuilder) {
UnaryOperator<String> topicOddrnBuilder) {
return switch (className) {
case "org.apache.kafka.connect.file.FileStreamSinkConnector",
"org.apache.kafka.connect.file.FileStreamSourceConnector",
Expand All @@ -43,7 +43,7 @@ static ConnectorInfo extract(String className,
private static ConnectorInfo extractFileIoConnector(ConnectorTypeDTO type,
List<String> topics,
Map<String, Object> config,
Function<String, String> topicOddrnBuilder) {
UnaryOperator<String> topicOddrnBuilder) {
return new ConnectorInfo(
extractInputs(type, topics, config, topicOddrnBuilder),
extractOutputs(type, topics, config, topicOddrnBuilder)
Expand All @@ -53,7 +53,7 @@ private static ConnectorInfo extractFileIoConnector(ConnectorTypeDTO type,
private static ConnectorInfo extractJdbcSink(ConnectorTypeDTO type,
List<String> topics,
Map<String, Object> config,
Function<String, String> topicOddrnBuilder) {
UnaryOperator<String> topicOddrnBuilder) {
String tableNameFormat = (String) config.getOrDefault("table.name.format", "${topic}");
List<String> targetTables = extractTopicNamesBestEffort(topics, config)
.map(topic -> tableNameFormat.replace("${kafka}", topic))
Expand Down Expand Up @@ -106,7 +106,7 @@ private static ConnectorInfo extractDebeziumMysql(Map<String, Object> config) {
private static ConnectorInfo extractS3Sink(ConnectorTypeDTO type,
List<String> topics,
Map<String, Object> config,
Function<String, String> topicOrrdnBuilder) {
UnaryOperator<String> topicOrrdnBuilder) {
String bucketName = (String) config.get("s3.bucket.name");
String topicsDir = (String) config.getOrDefault("topics.dir", "topics");
String directoryDelim = (String) config.getOrDefault("directory.delim", "/");
Expand All @@ -122,7 +122,7 @@ private static ConnectorInfo extractS3Sink(ConnectorTypeDTO type,
private static List<String> extractInputs(ConnectorTypeDTO type,
List<String> topicsFromApi,
Map<String, Object> config,
Function<String, String> topicOrrdnBuilder) {
UnaryOperator<String> topicOrrdnBuilder) {
return type == ConnectorTypeDTO.SINK
? extractTopicsOddrns(config, topicsFromApi, topicOrrdnBuilder)
: List.of();
Expand All @@ -131,7 +131,7 @@ private static List<String> extractInputs(ConnectorTypeDTO type,
private static List<String> extractOutputs(ConnectorTypeDTO type,
List<String> topicsFromApi,
Map<String, Object> config,
Function<String, String> topicOrrdnBuilder) {
UnaryOperator<String> topicOrrdnBuilder) {
return type == ConnectorTypeDTO.SOURCE
? extractTopicsOddrns(config, topicsFromApi, topicOrrdnBuilder)
: List.of();
Expand All @@ -158,7 +158,7 @@ private static Stream<String> extractTopicNamesBestEffort(

private static List<String> extractTopicsOddrns(Map<String, Object> config,
List<String> topicsFromApi,
Function<String, String> topicOrrdnBuilder) {
UnaryOperator<String> topicOrrdnBuilder) {
return extractTopicNamesBestEffort(topicsFromApi, config)
.map(topicOrrdnBuilder)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,25 +286,17 @@ private static DataSetField createDataSetField(Schema schema,
}

private static DataSetFieldType.TypeEnum mapType(Schema type) {
if (type instanceof NumberSchema) {
return DataSetFieldType.TypeEnum.NUMBER;
}
if (type instanceof StringSchema) {
return DataSetFieldType.TypeEnum.STRING;
}
if (type instanceof BooleanSchema || type instanceof TrueSchema || type instanceof FalseSchema) {
return DataSetFieldType.TypeEnum.BOOLEAN;
}
if (type instanceof ObjectSchema) {
return DataSetFieldType.TypeEnum.STRUCT;
}
if (type instanceof ReferenceSchema s) {
return mapType(s.getReferredSchema());
}
if (type instanceof CombinedSchema) {
return DataSetFieldType.TypeEnum.UNION;
}
return DataSetFieldType.TypeEnum.UNKNOWN;
return switch (type) {
case NumberSchema ignored -> DataSetFieldType.TypeEnum.NUMBER;
case StringSchema ignored -> DataSetFieldType.TypeEnum.STRING;
case BooleanSchema ignored -> DataSetFieldType.TypeEnum.BOOLEAN;
case TrueSchema ignored -> DataSetFieldType.TypeEnum.BOOLEAN;
case FalseSchema ignored -> DataSetFieldType.TypeEnum.BOOLEAN;
case ObjectSchema ignored -> DataSetFieldType.TypeEnum.STRUCT;
case ReferenceSchema referenceSchema -> mapType(referenceSchema.getReferredSchema());
case CombinedSchema ignored -> DataSetFieldType.TypeEnum.UNION;
default -> DataSetFieldType.TypeEnum.UNKNOWN;
};
}

}
11 changes: 4 additions & 7 deletions api/src/main/java/io/kafbat/ui/service/ksql/KsqlGrammar.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,10 @@ public static CaseInsensitiveStream from(CharStream stream) {
@Override
public int LA(final int i) {
final int result = stream.LA(i);
switch (result) {
case 0:
case IntStream.EOF:
return result;
default:
return Character.toUpperCase(result);
}
return switch (result) {
case 0, IntStream.EOF -> result;
default -> Character.toUpperCase(result);
};
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ private static Optional<BigDecimal> convertNumericValue(Object value) {
return Optional.empty();
}
try {
if (value instanceof Long) {
return Optional.of(new BigDecimal((Long) value));
} else if (value instanceof Integer) {
return Optional.of(new BigDecimal((Integer) value));
if (value instanceof Long longValue) {
return Optional.of(new BigDecimal(longValue));
} else if (value instanceof Integer integerValue) {
return Optional.of(new BigDecimal(integerValue));
}
return Optional.of(new BigDecimal(value.toString()));
} catch (NumberFormatException nfe) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,11 @@ private JsonType convertType(Schema schema) {
case INT, LONG -> new SimpleJsonType(JsonType.Type.INTEGER);
case MAP, RECORD -> new SimpleJsonType(JsonType.Type.OBJECT);
case ENUM -> new EnumJsonType(schema.getEnumSymbols());
case BYTES, STRING -> new SimpleJsonType(JsonType.Type.STRING);
case NULL -> new SimpleJsonType(JsonType.Type.NULL);
case ARRAY -> new SimpleJsonType(JsonType.Type.ARRAY);
case FIXED, FLOAT, DOUBLE -> new SimpleJsonType(JsonType.Type.NUMBER);
case BOOLEAN -> new SimpleJsonType(JsonType.Type.BOOLEAN);
default -> new SimpleJsonType(JsonType.Type.STRING);
default -> new SimpleJsonType(JsonType.Type.STRING); // BYTES, STRING and the remaining possibilities
};
}
}
Loading

0 comments on commit 844fbc9

Please sign in to comment.