Skip to content

Commit

Permalink
Fix more spotbugs errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow-Devil committed Oct 4, 2024
1 parent 2e0c066 commit e2b7dc6
Show file tree
Hide file tree
Showing 43 changed files with 128 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public boolean evaluate(String source) {
IncompleteInputFinder incompleteInputFinder = new IncompleteInputFinder();
Node parsedNode = NodeParser.parseExpression(source);
return !parsedNode.hasDiagnostics() || !parsedNode.apply(incompleteInputFinder)
|| nextInValidator.evaluate(source);
|| (nextInValidator != null && nextInValidator.evaluate(source));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public boolean evaluate(String source) {
// Sample testcase : if (x == y) { x = x + 1; x = x + 1;
if (lastNode.kind() == SyntaxKind.FUNCTION_DEFINITION) {
return !lastNode.hasDiagnostics() || !lastNode.apply(incompleteInputFinder)
|| nextInValidator.evaluate(lastNode.toSourceCode());
|| (nextInValidator != null && nextInValidator.evaluate(lastNode.toSourceCode()));
}
}

return !node.imports().isEmpty() || !parsedNode.hasDiagnostics() || !parsedNode.apply(incompleteInputFinder)
|| nextInValidator.evaluate(source);
|| (nextInValidator != null && nextInValidator.evaluate(source));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public boolean evaluate(String source) {
IncompleteInputFinder incompleteInputFinder = new IncompleteInputFinder();
Node parsedNode = NodeParser.parseBlockStatement("{" + source + "}");
return !parsedNode.hasDiagnostics() || !parsedNode.apply(incompleteInputFinder)
|| nextInValidator.evaluate(source);
|| (nextInValidator != null && nextInValidator.evaluate(source));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public void notifyFailure(BError error) {
* @param returnValueSupplier Suppler used to set the final return value for the parent function invocation.
* @param scheduler The scheduler for invoking functions
*/
public static void invokeFunctionPointerAsyncIteratively(BFunctionPointer<Object[], ?> func, @Nullable String strandName,
public static void invokeFunctionPointerAsyncIteratively(BFunctionPointer<Object[], ?> func,
@Nullable String strandName,
StrandMetadata metadata, int noOfIterations,
Supplier<Object[]> argsSupplier,
Consumer<Object> futureResultConsumer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ public FutureValue schedule(
* @param metadata meta data of new strand
* @return Reference to the scheduled task
*/
public FutureValue schedule(Object[] params, Function<Object[], ?> function, @Nullable Strand parent, Callback callback,
String strandName, @Nullable StrandMetadata metadata) {
public FutureValue schedule(Object[] params, Function<Object[], ?> function, @Nullable Strand parent,
Callback callback, String strandName, @Nullable StrandMetadata metadata) {
FutureValue future = createFuture(parent, callback, null, PredefinedTypes.TYPE_NULL, strandName, metadata);
return schedule(params, function, future);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class BFunctionType extends BAnnotatableType implements FunctionType {
@Nullable
public Type retType;
public long flags;
@Nullable
public Parameter[] parameters;

public BFunctionType(Module pkg) {
Expand All @@ -52,7 +51,7 @@ public BFunctionType(Module pkg) {

public BFunctionType(Module pkg, long flags) {
super("function", pkg, Object.class);
this.parameters = null;
this.parameters = new Parameter[0];
this.retType = null;
this.flags = flags;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,6 @@ private static void initToolPackage(Path path, String packageName) throws IOExce
Files.writeString(balToolToml, balToolManifest);
}

@Nullable
private static Optional<PackageVersion> findLatest(List<PackageVersion> packageVersions) {
if (packageVersions.isEmpty()) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ private Optional<TypeSymbol> getExpectedTypeFromFunction(BLangNode bLangNode, To
}

if (langLibInvocation) {
if (bLangInvocation.expr.getBType().getKind() == TypeKind.ARRAY) {
if (bLangInvocation.expr != null && bLangInvocation.expr.getBType().getKind() == TypeKind.ARRAY) {
return Optional.ofNullable(typesFactory.getTypeDescriptor
(((BArrayType) bLangInvocation.expr.expectedType).eType));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.ballerinalang.spi;

import org.ballerinalang.repository.PackageRepository;
import org.jetbrains.annotations.Nullable;
import org.wso2.ballerinalang.compiler.packaging.repo.Repo;

import java.net.URI;
Expand All @@ -32,12 +31,11 @@
*/
public interface SystemPackageRepositoryProvider {

@Nullable
static URI getClassUri(Object obj) {
try {
return obj.getClass().getProtectionDomain().getCodeSource().getLocation().toURI();
} catch (URISyntaxException ignore) {
return null;
} catch (URISyntaxException e) {
throw new RuntimeException("Error while getting the URI of the class", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
*/
public class BInvokableSymbol extends BVarSymbol implements InvokableSymbol {

@Nullable
public List<BVarSymbol> params;
@Nullable
public BVarSymbol restParam;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void testDependenciesIrregularPath() throws TomlException, IOException {

Manifest manifest = ManifestProcessor.parseTomlContentFromString(this.validProjectBlock + "[dependencies] \n " +
"string-utils = {path = '" + balaPath + "', version = \"1.1.5\"} \n");
Path manifestPath = manifest.getDependencies().get(0).getMetadata().getPath();
Path manifestPath = manifest.getDependencies().get(0).getMetadata().getPath().orElseThrow();
if (manifestPath.toString().contains("\\")) {
manifestPath = Path.of(manifestPath.toString().replace("\\", "/"));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Class to be extended by any parser error handler class.
Expand Down Expand Up @@ -159,7 +160,7 @@ private void validateSolution(Result bestMatch, ParserRuleContext currentCtx, ST
}

Solution firstFix = bestMatch.popFix();
Solution secondFix = bestMatch.peekFix();
Solution secondFix = bestMatch.peekFix().orElseThrow();
bestMatch.pushFix(firstFix);

if (secondFix.action == Action.REMOVE && secondFix.depth == 1) {
Expand Down Expand Up @@ -314,22 +315,20 @@ public void switchContext(ParserRuleContext context) {
*
* @return head of the stack
*/
@Nullable
protected ParserRuleContext getParentContext() {
return this.ctxStack.peek();
return Objects.requireNonNull(this.ctxStack.peek());
}

/**
* Returns the second element of the context stack.
*
* @return second element of the stack
*/
@Nullable
protected ParserRuleContext getGrandParentContext() {
ParserRuleContext parent = this.ctxStack.pop();
ParserRuleContext grandParent = this.ctxStack.peek();
this.ctxStack.push(parent);
return grandParent;
return Objects.requireNonNull(grandParent);
}

/**
Expand Down Expand Up @@ -422,8 +421,8 @@ protected Result seekInAlternativesPaths(int lookahead, int currentDepth, int cu
}

if (currentMatchRemoveFixes == bestMatchRemoveFixes) {
Solution currentSol = bestMatch.peekFix();
Solution foundSol = currentMatch.peekFix();
Solution currentSol = bestMatch.peekFix().orElseThrow();
Solution foundSol = currentMatch.peekFix().orElseThrow();
if (currentSol.action == Action.REMOVE && foundSol.action == Action.INSERT) {
bestMatch = currentMatch;
}
Expand Down Expand Up @@ -480,7 +479,7 @@ protected Result fixAndContinue(ParserRuleContext currentCtx, int lookahead, int
// i.e: do not increment the match count by 1;

if (isEntryPoint) {
fixedPathResult.solution = fixedPathResult.peekFix();
fixedPathResult.solution = fixedPathResult.peekFix().orElseThrow();
} else {
fixedPathResult.solution =
new Solution(Action.KEEP, currentCtx, getExpectedTokenKind(currentCtx), currentCtx.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* A LL(k) lexer for ballerina.
Expand All @@ -45,7 +46,7 @@ public BallerinaLexer(CharReader charReader) {
*/
@Override
public STToken nextToken() {
STToken token = switch (this.mode) {
STToken token = switch (Objects.requireNonNull(this.mode, "Lexer mode was null, an underflow occurred")) {
case TEMPLATE -> readTemplateToken();
case REGEXP -> readRegExpTemplateToken();
case INTERPOLATION -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.tools.diagnostics.DiagnosticCode;
import io.ballerina.tools.text.CharReader;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayDeque;
Expand Down Expand Up @@ -19016,12 +19017,12 @@ private List<STNode> getBindingPatternsList(List<STNode> ambibuousList, boolean
return bindingPatterns;
}

@Nullable
@Contract("null, _ -> null; !null, _ -> !null")
private STNode getBindingPattern(STNode ambiguousNode, boolean isListBP) {
DiagnosticCode errorCode = DiagnosticErrorCode.ERROR_INVALID_BINDING_PATTERN;

if (isEmpty(ambiguousNode)) {
return ambiguousNode;
return null;
}

switch (ambiguousNode.kind) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ public class BallerinaParserErrorHandler extends AbstractParserErrorHandler {
private static final ParserRuleContext[] OPTIONAL_TOP_LEVEL_SEMICOLON =
{ ParserRuleContext.TOP_LEVEL_NODE, ParserRuleContext.SEMICOLON };

private static final ParserRuleContext[] TUPLE_MEMBER =
private static final ParserRuleContext[] TUPLE_MEMBER =
{ ParserRuleContext.ANNOTATIONS, ParserRuleContext.TYPE_DESC_IN_TUPLE };

public BallerinaParserErrorHandler(@Nullable AbstractTokenReader tokenReader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
* A LL(k) lexer for documentation in ballerina.
Expand Down Expand Up @@ -63,7 +64,7 @@ public DocumentationLexer(CharReader charReader,
*/
@Override
public STToken nextToken() {
STToken token = switch (this.mode) {
STToken token = switch (Objects.requireNonNull(this.mode, "Lexer mode was null, an underflow occurred")) {
case DOC_LINE_START_HASH -> {
processLeadingTrivia();
yield readDocLineStartHashToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.ballerina.tools.text.CharReader;

import java.util.ArrayList;
import java.util.Objects;

/**
* A LL(k) lexer for RegExp in ballerina.
Expand All @@ -45,7 +46,7 @@ public RegExpLexer(CharReader charReader) {
@Override
public STToken nextToken() {
STToken token;
switch (this.mode) {
switch (Objects.requireNonNull(this.mode, "Lexer mode was null, an underflow occurred")) {
case RE_DISJUNCTION:
case RE_CHAR_CLASS:
token = readTokenInReDisjunction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
*/
package io.ballerina.compiler.internal.parser;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayDeque;
import java.util.Optional;

/**
* Represent a result of a token-sequence-search in a sub-tree. The result will contain the fixes required to
Expand Down Expand Up @@ -58,9 +57,8 @@ public Result(ArrayDeque<AbstractParserErrorHandler.Solution> fixes, int matches
this.matches = matches;
}

@Nullable
protected AbstractParserErrorHandler.Solution peekFix() {
return this.fixes.peek();
protected Optional<AbstractParserErrorHandler.Solution> peekFix() {
return Optional.ofNullable(this.fixes.peek());
}

protected AbstractParserErrorHandler.Solution popFix() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* A LL(k) lexer for XML in ballerina.
Expand All @@ -45,7 +46,7 @@ public XMLLexer(CharReader charReader) {
*/
@Override
public STToken nextToken() {
STToken token = switch (this.mode) {
STToken token = switch (Objects.requireNonNull(this.mode, "Lexer mode was null, an underflow occurred")) {
case XML_CONTENT -> readTokenInXMLContent();
case XML_ELEMENT_START_TAG -> {
processLeadingXMLTrivia();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ TextLine textLine(int line) {
LinePosition linePositionFrom(int position) {
positionRangeCheck(position);
TextLine textLine = findLineFrom(position);
if (textLine == null) {
throw new IllegalArgumentException("Cannot find a line with the character offset '" + position + "'");
}
return LinePosition.from(textLine.lineNo(), position - textLine.startOffset());
}

Expand Down Expand Up @@ -98,7 +101,6 @@ private TextLine findLineFrom(int position) {
return textLines[length - 1];
}

TextLine foundTextLine = null;
int left = 0;
int right = length - 1;
while (left <= right) {
Expand All @@ -109,14 +111,13 @@ private TextLine findLineFrom(int position) {
int startOffset = textLines[middle].startOffset();
int endOffset = textLines[middle].endOffsetWithNewLines();
if (startOffset <= position && position < endOffset) {
foundTextLine = textLines[middle];
break;
return textLines[middle];
} else if (endOffset <= position) {
left = middle + 1;
} else {
right = middle - 1;
}
}
return foundTextLine;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public static void fillNodeAtCursor(TomlCompletionContext context) {
context.setCursorPositionInTree(txtPos);
TextRange range = TextRange.from(txtPos, 0);
NonTerminalNode nonTerminalNode = ((DocumentNode) st.get().rootNode()).findNode(range);
while (nonTerminalNode.parent() != null && !TomlSyntaxTreeUtil.withinTextRange(txtPos, nonTerminalNode)) {
while (nonTerminalNode != null &&
nonTerminalNode.parent() != null && !TomlSyntaxTreeUtil.withinTextRange(txtPos, nonTerminalNode)) {
//Takes the top-level Node of the immediate cursor position
nonTerminalNode = nonTerminalNode.parent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextEdit;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -226,7 +225,6 @@ public String getName() {
return NAME;
}

@Nullable
private Node getStatementNode(Node node) {
Node statementNode = node;
while (statementNode != null && !(statementNode instanceof StatementNode)
Expand Down
Loading

0 comments on commit e2b7dc6

Please sign in to comment.