Skip to content

Commit

Permalink
Address sonar cloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Jun 17, 2024
1 parent cd3d602 commit 8edb5e8
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;

/**
Expand Down Expand Up @@ -71,9 +72,7 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {
cursorPosition.get())) {
return Optional.empty();
}
CodeActionArgument arg = CodeActionArgument.from(NODE_LOCATION_KEY, node.lineRange());
CodeActionInfo info = CodeActionInfo.from(String.format("Add %s parameter", paramKind()), List.of(arg));
return Optional.of(info);
return getCodeActionInfoWithLocation(node, String.format("Add %s parameter", paramKind()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;

/**
Expand All @@ -58,8 +59,7 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {
return Optional.empty();
}

CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY, node.location().lineRange());
return Optional.of(CodeActionInfo.from("Add response cache configuration", List.of(locationArg)));
return getCodeActionInfoWithLocation(node, "Add response cache configuration");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;

/**
Expand All @@ -58,8 +59,7 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {
return Optional.empty();
}

CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY, node.location().lineRange());
return Optional.of(CodeActionInfo.from("Add response content-type", List.of(locationArg)));
return getCodeActionInfoWithLocation(node, "Add response content-type");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.ballerina.compiler.syntax.tree.RestParameterNode;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.projects.plugins.codeaction.CodeAction;
import io.ballerina.projects.plugins.codeaction.CodeActionArgument;
import io.ballerina.projects.plugins.codeaction.CodeActionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionInfo;
Expand All @@ -44,7 +43,8 @@
import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getLineRangeFromLocationKey;

/**
* Abstract implementation of code action to change a resource header param's type.
Expand All @@ -66,38 +66,27 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {

DiagnosticProperty<?> diagnosticProperty = properties.get(0);
if (!(diagnosticProperty instanceof BSymbolicProperty) ||
!(diagnosticProperty.value() instanceof ParameterSymbol)) {
!(diagnosticProperty.value() instanceof ParameterSymbol parameterSymbol)) {
return Optional.empty();
}

ParameterSymbol parameterSymbol = (ParameterSymbol) diagnosticProperty.value();
Optional<NonTerminalNode> nonTerminalNode = parameterSymbol.getLocation()
.flatMap(location -> Optional.ofNullable(CodeActionUtil.findNode(syntaxTree, parameterSymbol)));
if (nonTerminalNode.isEmpty()) {
return Optional.empty();
}
return nonTerminalNode.flatMap(terminalNode -> getCodeActionInfoWithLocation(terminalNode,
String.format("Change header param to '%s'", headerParamType())));

CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY,
nonTerminalNode.get().location().lineRange());
return Optional.of(CodeActionInfo.from(String.format("Change header param to '%s'", headerParamType()),
List.of(locationArg)));
}

@Override
public List<DocumentEdit> execute(CodeActionExecutionContext context) {
LineRange lineRange = null;
for (CodeActionArgument argument : context.arguments()) {
if (NODE_LOCATION_KEY.equals(argument.key())) {
lineRange = argument.valueAs(LineRange.class);
}
}
Optional<LineRange> lineRange = getLineRangeFromLocationKey(context);

if (lineRange == null) {
if (lineRange.isEmpty()) {
return Collections.emptyList();
}

SyntaxTree syntaxTree = context.currentDocument().syntaxTree();
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange);
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange.get());

TextRange typeNodeTextRange;
switch (node.kind()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.projects.plugins.codeaction.CodeAction;
import io.ballerina.projects.plugins.codeaction.CodeActionArgument;
import io.ballerina.projects.plugins.codeaction.CodeActionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionInfo;
Expand All @@ -37,7 +36,8 @@
import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getLineRangeFromLocationKey;

/**
* Codeaction to change the return type to <pre>error?</pre> if the resource function has <pre>http:Caller</pre> as a
Expand All @@ -58,28 +58,22 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {
return Optional.empty();
}

CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY, node.location().lineRange());
return Optional.of(CodeActionInfo.from("Change return type to 'error?'", List.of(locationArg)));
return getCodeActionInfoWithLocation(node, "Change return type to 'error?'");
}

@Override
public List<DocumentEdit> execute(CodeActionExecutionContext context) {
LineRange lineRange = null;
for (CodeActionArgument argument : context.arguments()) {
if (NODE_LOCATION_KEY.equals(argument.key())) {
lineRange = argument.valueAs(LineRange.class);
}
}
Optional<LineRange> lineRange = getLineRangeFromLocationKey(context);

if (lineRange == null) {
if (lineRange.isEmpty()) {
return Collections.emptyList();
}

SyntaxTree syntaxTree = context.currentDocument().syntaxTree();
TextDocument textDocument = syntaxTree.textDocument();

int start = textDocument.textPositionFrom(lineRange.startLine());
int end = textDocument.textPositionFrom(lineRange.endLine());
int start = textDocument.textPositionFrom(lineRange.get().startLine());
int end = textDocument.textPositionFrom(lineRange.get().endLine());

List<TextEdit> textEdits = new ArrayList<>();
textEdits.add(TextEdit.from(TextRange.from(start, end - start), "error?"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@
import io.ballerina.compiler.syntax.tree.ModulePartNode;
import io.ballerina.compiler.syntax.tree.NonTerminalNode;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.projects.plugins.codeaction.CodeActionArgument;
import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionInfo;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.LineRange;
import io.ballerina.tools.text.TextDocument;
import io.ballerina.tools.text.TextRange;

import java.util.List;
import java.util.Optional;

import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;

/**
* Utilities for code actions.
*/
Expand Down Expand Up @@ -90,4 +98,31 @@ public static boolean isWithinRange(LineRange lineRange, LinePosition pos) {
pos.line() == sLine && pos.offset() >= sCol
));
}

/**
* Get code action info with location.
*
* @param node Node
* @param title Title
* @return Code action info with location
*/
public static Optional<CodeActionInfo> getCodeActionInfoWithLocation(NonTerminalNode node, String title) {
CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY, node.location().lineRange());
return Optional.of(CodeActionInfo.from(title, List.of(locationArg)));
}

/**
* Get line range from location key.
*
* @param context Code action execution context
* @return Line range
*/
public static Optional<LineRange> getLineRangeFromLocationKey(CodeActionExecutionContext context) {
for (CodeActionArgument argument : context.arguments()) {
if (NODE_LOCATION_KEY.equals(argument.key())) {
return Optional.of(argument.valueAs(LineRange.class));
}
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.compiler.syntax.tree.TypeDescriptorNode;
import io.ballerina.projects.plugins.codeaction.CodeAction;
import io.ballerina.projects.plugins.codeaction.CodeActionArgument;
import io.ballerina.projects.plugins.codeaction.CodeActionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionExecutionContext;
import io.ballerina.projects.plugins.codeaction.CodeActionInfo;
Expand All @@ -55,8 +54,9 @@

import static io.ballerina.stdlib.http.compiler.HttpServiceContractResourceValidator.constructResourcePathName;
import static io.ballerina.stdlib.http.compiler.HttpServiceValidator.getServiceContractTypeDesc;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getCodeActionInfoWithLocation;
import static io.ballerina.stdlib.http.compiler.codeaction.CodeActionUtil.getLineRangeFromLocationKey;
import static io.ballerina.stdlib.http.compiler.codeaction.Constants.LS;
import static io.ballerina.stdlib.http.compiler.codeaction.Constants.NODE_LOCATION_KEY;

/**
* Represents a code action to implement all the resource methods from the service contract type.
Expand All @@ -77,27 +77,20 @@ public Optional<CodeActionInfo> codeActionInfo(CodeActionContext context) {
return Optional.empty();
}

CodeActionArgument locationArg = CodeActionArgument.from(NODE_LOCATION_KEY, node.location().lineRange());
return Optional.of(CodeActionInfo.from("Implement all the resource methods from service contract",
List.of(locationArg)));
return getCodeActionInfoWithLocation(node, "Implement all the resource methods from service contract");
}

@Override
public List<DocumentEdit> execute(CodeActionExecutionContext context) {
LineRange lineRange = null;
for (CodeActionArgument argument : context.arguments()) {
if (NODE_LOCATION_KEY.equals(argument.key())) {
lineRange = argument.valueAs(LineRange.class);
}
}
Optional<LineRange> lineRange = getLineRangeFromLocationKey(context);

if (lineRange == null) {
if (lineRange.isEmpty()) {
return Collections.emptyList();
}

SyntaxTree syntaxTree = context.currentDocument().syntaxTree();
SemanticModel semanticModel = context.currentSemanticModel();
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange);
NonTerminalNode node = CodeActionUtil.findNode(syntaxTree, lineRange.get());
if (!node.kind().equals(SyntaxKind.SERVICE_DECLARATION)) {
return Collections.emptyList();
}
Expand Down
42 changes: 22 additions & 20 deletions native/src/main/java/io/ballerina/stdlib/http/api/HttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ public class HttpService implements Service {

private static final Logger log = LoggerFactory.getLogger(HttpService.class);

protected static final BString BASE_PATH_FIELD = fromString("basePath");
protected static final BString CORS_FIELD = fromString("cors");
private static final BString VERSIONING_FIELD = fromString("versioning");
protected static final BString HOST_FIELD = fromString("host");
protected static final BString OPENAPI_DEF_FIELD = fromString("openApiDefinition");
protected static final BString MEDIA_TYPE_SUBTYPE_PREFIX = fromString("mediaTypeSubtypePrefix");
protected static final BString TREAT_NILABLE_AS_OPTIONAL = fromString("treatNilableAsOptional");
protected static final BString DATA_VALIDATION = fromString("validation");
private static final BString CORS_FIELD = fromString("cors");
private static final BString HOST_FIELD = fromString("host");
private static final BString OPENAPI_DEF_FIELD = fromString("openApiDefinition");
private static final BString MEDIA_TYPE_SUBTYPE_PREFIX = fromString("mediaTypeSubtypePrefix");
private static final BString TREAT_NILABLE_AS_OPTIONAL = fromString("treatNilableAsOptional");
private static final BString DATA_VALIDATION = fromString("validation");

private BObject balService;
private List<HttpResource> resources;
Expand Down Expand Up @@ -239,25 +237,29 @@ public URITemplate<Resource, HttpCarbonMessage> getUriTemplate() throws URITempl
public static HttpService buildHttpService(BObject service, String basePath) {
HttpService httpService = new HttpService(service, basePath);
BMap serviceConfig = getHttpServiceConfigAnnotation(service);
httpService.populateServiceConfig(serviceConfig);
return httpService;
}

protected void populateServiceConfig(BMap serviceConfig) {
if (checkConfigAnnotationAvailability(serviceConfig)) {
httpService.setCompressionConfig(
this.setCompressionConfig(
(BMap<BString, Object>) serviceConfig.get(HttpConstants.ANN_CONFIG_ATTR_COMPRESSION));
httpService.setChunkingConfig(serviceConfig.get(HttpConstants.ANN_CONFIG_ATTR_CHUNKING).toString());
httpService.setCorsHeaders(CorsHeaders.buildCorsHeaders(serviceConfig.getMapValue(CORS_FIELD)));
httpService.setHostName(serviceConfig.getStringValue(HOST_FIELD).getValue().trim());
httpService.setIntrospectionPayload(serviceConfig.getArrayValue(OPENAPI_DEF_FIELD).getByteArray());
this.setChunkingConfig(serviceConfig.get(HttpConstants.ANN_CONFIG_ATTR_CHUNKING).toString());
this.setCorsHeaders(CorsHeaders.buildCorsHeaders(serviceConfig.getMapValue(CORS_FIELD)));
this.setHostName(serviceConfig.getStringValue(HOST_FIELD).getValue().trim());
this.setIntrospectionPayload(serviceConfig.getArrayValue(OPENAPI_DEF_FIELD).getByteArray());
if (serviceConfig.containsKey(MEDIA_TYPE_SUBTYPE_PREFIX)) {
httpService.setMediaTypeSubtypePrefix(serviceConfig.getStringValue(MEDIA_TYPE_SUBTYPE_PREFIX)
this.setMediaTypeSubtypePrefix(serviceConfig.getStringValue(MEDIA_TYPE_SUBTYPE_PREFIX)
.getValue().trim());
}
httpService.setTreatNilableAsOptional(serviceConfig.getBooleanValue(TREAT_NILABLE_AS_OPTIONAL));
httpService.setConstraintValidation(serviceConfig.getBooleanValue(DATA_VALIDATION));
this.setTreatNilableAsOptional(serviceConfig.getBooleanValue(TREAT_NILABLE_AS_OPTIONAL));
this.setConstraintValidation(serviceConfig.getBooleanValue(DATA_VALIDATION));
} else {
httpService.setHostName(HttpConstants.DEFAULT_HOST);
this.setHostName(HttpConstants.DEFAULT_HOST);
}
processResources(httpService);
httpService.setAllAllowedMethods(DispatcherUtil.getAllResourceMethods(httpService));
return httpService;
processResources(this);
this.setAllAllowedMethods(DispatcherUtil.getAllResourceMethods(this));
}

protected static void processResources(HttpService httpService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.stdlib.http.api.nativeimpl.ModuleUtils;
import io.ballerina.stdlib.http.uri.DispatcherUtil;

import java.util.Objects;

Expand All @@ -40,7 +38,7 @@
*/
public class HttpServiceFromContract extends HttpService {

private ReferenceType serviceContractType;
private final ReferenceType serviceContractType;

protected HttpServiceFromContract(BObject service, String basePath, ReferenceType httpServiceContractType) {
super(service, basePath);
Expand All @@ -51,29 +49,19 @@ public static HttpService buildHttpService(BObject service, String basePath,
ReferenceType serviceContractType) {
HttpService httpService = new HttpServiceFromContract(service, basePath, serviceContractType);
BMap serviceConfig = getHttpServiceConfigAnnotation(serviceContractType);
httpService.populateServiceConfig(serviceConfig);
return httpService;
}

@Override
protected void populateServiceConfig(BMap serviceConfig) {
if (checkConfigAnnotationAvailability(serviceConfig)) {
Object basePathFromAnnotation = serviceConfig.get(HttpConstants.ANN_CONFIG_BASE_PATH);
if (Objects.nonNull(basePathFromAnnotation)) {
httpService.setBasePath(basePathFromAnnotation.toString());
}
httpService.setCompressionConfig(
(BMap<BString, Object>) serviceConfig.get(HttpConstants.ANN_CONFIG_ATTR_COMPRESSION));
httpService.setChunkingConfig(serviceConfig.get(HttpConstants.ANN_CONFIG_ATTR_CHUNKING).toString());
httpService.setCorsHeaders(CorsHeaders.buildCorsHeaders(serviceConfig.getMapValue(CORS_FIELD)));
httpService.setHostName(serviceConfig.getStringValue(HOST_FIELD).getValue().trim());
httpService.setIntrospectionPayload(serviceConfig.getArrayValue(OPENAPI_DEF_FIELD).getByteArray());
if (serviceConfig.containsKey(MEDIA_TYPE_SUBTYPE_PREFIX)) {
httpService.setMediaTypeSubtypePrefix(serviceConfig.getStringValue(MEDIA_TYPE_SUBTYPE_PREFIX)
.getValue().trim());
this.setBasePath(basePathFromAnnotation.toString());
}
httpService.setTreatNilableAsOptional(serviceConfig.getBooleanValue(TREAT_NILABLE_AS_OPTIONAL));
httpService.setConstraintValidation(serviceConfig.getBooleanValue(DATA_VALIDATION));
} else {
httpService.setHostName(HttpConstants.DEFAULT_HOST);
}
processResources(httpService);
httpService.setAllAllowedMethods(DispatcherUtil.getAllResourceMethods(httpService));
return httpService;
super.populateServiceConfig(serviceConfig);
}

public static BMap getHttpServiceConfigAnnotation(ReferenceType serviceContractType) {
Expand Down

0 comments on commit 8edb5e8

Please sign in to comment.