Skip to content

Commit

Permalink
Fix spotbugs errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow-Devil committed Nov 5, 2024
1 parent 6e2fc18 commit 3eaf480
Show file tree
Hide file tree
Showing 112 changed files with 343 additions and 400 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 @@ -570,7 +570,7 @@ private BSymbol getInternalSymbol(Symbol symbol) {

private BPackageSymbol getModuleSymbol(BLangCompilationUnit compilationUnit) {
return compilationUnit.getSourceKind() == REGULAR_SOURCE ? bLangPackage.symbol :
bLangPackage.getTestablePkg().symbol;
bLangPackage.getTestablePkg().orElseThrow().symbol;
}

private void addToCompiledSymbols(Set<Symbol> compiledSymbols, Scope.ScopeEntry scopeEntry, Location cursorPos,
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 All @@ -1207,7 +1207,9 @@ private Optional<TypeSymbol> getExpectedTypeFromFunction(BLangNode bLangNode, To
}

BLangExpression bLangExpression = bLangInvocation.argExprs.get(argumentIndex);
if (bLangExpression.toString().startsWith("$missingNode$")) {
// Spotbugs thinks this can be nullable for some reason
String bLangExpressionString = bLangExpression.toString();
if (bLangExpressionString != null && bLangExpressionString.startsWith("$missingNode$")) {
return getParamType(bLangInvocation, argumentIndex, namedArgs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.wso2.ballerinalang.compiler.tree.BLangSimpleVariable;
import org.wso2.ballerinalang.compiler.tree.BLangTableKeySpecifier;
import org.wso2.ballerinalang.compiler.tree.BLangTableKeyTypeConstraint;
import org.wso2.ballerinalang.compiler.tree.BLangTestablePackage;
import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable;
import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition;
import org.wso2.ballerinalang.compiler.tree.BLangXMLNS;
Expand Down Expand Up @@ -230,11 +229,7 @@ class NodeFinder extends BaseVisitor {

BLangNode lookup(BLangPackage module, LineRange range) {
List<TopLevelNode> topLevelNodes = new ArrayList<>(module.topLevelNodes);
BLangTestablePackage tests = module.getTestablePkg();

if (tests != null) {
topLevelNodes.addAll(tests.topLevelNodes);
}
module.getTestablePkg().ifPresent(tests -> topLevelNodes.addAll(tests.topLevelNodes));

return lookupTopLevelNodes(topLevelNodes, range);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@

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

import static org.ballerinalang.model.symbols.SymbolOrigin.VIRTUAL;

Expand Down Expand Up @@ -275,7 +276,7 @@ public void visit(BLangPackage pkgNode) {
.toList());

if (!(pkgNode instanceof BLangTestablePackage)) {
find(pkgNode.getTestablePkg());
find(pkgNode.getTestablePkg().orElseThrow());
}
}

Expand Down Expand Up @@ -1509,7 +1510,7 @@ private void findRefsInResourceAccessPathSegments(BLangInvocation.BLangResourceA
}

BLangLiteral literal = (BLangLiteral) expr;
if (literal.value.equals(pathSymbol.name.value) && addIfSameSymbol(pathSymbol, expr.pos)) {
if (Objects.equals(literal.value, pathSymbol.name.value) && addIfSameSymbol(pathSymbol, expr.pos)) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.ballerinalang.model.symbols.AnnotationAttachmentSymbol;
import org.ballerinalang.model.symbols.SymbolKind;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.wso2.ballerinalang.compiler.semantics.analyzer.Types;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BAttachedFunction;
Expand Down Expand Up @@ -378,7 +379,7 @@ private boolean isTypeParam(BType type) {
return type == this.typeParam;
}

@Nullable
@Contract("null, _ -> null")
private BVarSymbol createNewVarSymbol(BVarSymbol symbol, @Nullable BType newType) {

if (symbol == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.ballerina.compiler.api.symbols.TypeSymbol;
import io.ballerina.tools.diagnostics.Location;
import org.ballerinalang.model.symbols.SymbolOrigin;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Contract;
import org.wso2.ballerinalang.compiler.semantics.model.Scope;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.types.BParameterizedType;
Expand Down Expand Up @@ -199,7 +199,7 @@ private boolean isAnonOrg(ModuleID moduleID) {
return ANON_ORG.equals(moduleID.orgName());
}

@Nullable
@Contract("null -> null")
private BType getReferredType(BType type) {
if (type == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public SingletonTypeSymbol build() {
if (value == null) {
throw new IllegalArgumentException("The value provided to the singleton type can not be null");
}
if (valueTypeSymbol == null) {
throw new IllegalArgumentException("The type symbol provided to the singleton type can not be null");
}

if (!isValidValueType(value, valueTypeSymbol)) {
throw new IllegalArgumentException("Type of value provided doesn't match with the provided type symbol");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.ballerina.tools.text.TextDocuments;
import org.apache.commons.compress.utils.IOUtils;
import org.ballerinalang.compiler.BLangCompilerException;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.wso2.ballerinalang.util.RepoUtils;

Expand Down Expand Up @@ -236,7 +237,7 @@ private static Boolean isAllPlatformDepsGraalvmCompatible(Map<String, PackageMan
for (PackageManifest.Platform platform: platforms.values()) {
if (platform.isPlatfromDepsGraalvmCompatible() == null) {
isAllDepsGraalvmCompatible = null;
} else if (!platform.isPlatfromDepsGraalvmCompatible()) {
} else if (Boolean.FALSE.equals(platform.isPlatfromDepsGraalvmCompatible())) {
return false;
}
}
Expand Down Expand Up @@ -541,23 +542,21 @@ protected abstract Optional<JsonArray> addPlatformLibs(ZipOutputStream balaOutpu

// Following function was put in to handle a bug in windows zipFileSystem
// Refer https://bugs.openjdk.java.net/browse/JDK-8195141
@Nullable
@Contract("null -> null")
private String convertPathSeperator(Path file) {
if (file == null) {
return null;
} else {
if (File.separatorChar == '\\') {
String replaced;
// Following is to evade spotbug issue if file is null
replaced = Optional.ofNullable(file.getFileName()).orElse(Path.of("")).toString();
Path parent = file.getParent();
while (parent != null) {
replaced = parent.getFileName() + UNIX_FILE_SEPARATOR + replaced;
parent = parent.getParent();
}
return replaced;
}
}
if (File.separatorChar != '\\') {
return file.toString();
}
// Following is to evade spotbug issue if file is null
String replaced = Optional.ofNullable(file.getFileName()).orElse(Path.of("")).toString();
Path parent = file.getParent();
while (parent != null) {
replaced = parent.getFileName() + UNIX_FILE_SEPARATOR + replaced;
parent = parent.getParent();
}
return replaced;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ public List<JarConflict> conflictedJars() {
}

// TODO Can we move this method to Module.displayName()
@Nullable
private String getJarFileName(ModuleContext moduleContext) {
String jarName;
if (moduleContext.project().kind() == ProjectKind.SINGLE_FILE_PROJECT) {
Expand Down Expand Up @@ -644,7 +643,8 @@ private PlatformLibrary codeGeneratedLibrary(PackageId packageId,
Package pkg = packageCache.getPackageOrThrow(packageId);
ProjectEnvironment projectEnvironment = pkg.project().projectEnvironmentContext();
CompilationCache compilationCache = projectEnvironment.getService(CompilationCache.class);
String jarFileName = getJarFileName(pkg.packageContext().moduleContext(moduleName)) + fileNameSuffix;
String jarFileName = getJarFileName(pkg.packageContext().moduleContext(moduleName).orElseThrow())
+ fileNameSuffix;
Optional<Path> platformSpecificLibrary = compilationCache.getPlatformSpecificLibrary(
this, jarFileName);
return new JarLibrary(platformSpecificLibrary.orElseThrow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private void addModuleDependencies(ModuleDescriptor moduleDescriptor,
Collection<ModuleDescriptor> directDependencies = new HashSet<>(pkg.get().moduleDependencyGraph().
getDirectDependencies(moduleDescriptor));

ModuleContext moduleCtx = pkg.get().packageContext().moduleContext(moduleDescriptor.name());
ModuleContext moduleCtx = pkg.get().packageContext().moduleContext(moduleDescriptor.name()).orElseThrow();
for (ModuleDependency moduleDependency : moduleCtx.dependencies()) {
PackageId dependentPkgId = moduleDependency.packageDependency().packageId();
if (dependentPkgId == pkg.get().packageId()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,15 @@ private static ByteArrayOutputStream generateBIR(ModuleContext moduleContext, Co
// Can we improve this logic
ByteArrayOutputStream birContent = new ByteArrayOutputStream();
try {
CompiledBinaryFile.BIRPackageFile birPackageFile = moduleContext.bLangPackage.symbol.birPackageFile;
BLangPackage bLangPackage = moduleContext.bLangPackage;
if (bLangPackage == null) {
throw new IllegalStateException("BLangPackage is null");
}
CompiledBinaryFile.BIRPackageFile birPackageFile = bLangPackage.symbol.birPackageFile;
if (birPackageFile == null) {
birPackageFile = new CompiledBinaryFile
.BIRPackageFile(new BIRBinaryWriter(moduleContext.bLangPackage.symbol.bir).serialize());
moduleContext.bLangPackage.symbol.birPackageFile = birPackageFile;
.BIRPackageFile(new BIRBinaryWriter(bLangPackage.symbol.bir).serialize());
bLangPackage.symbol.birPackageFile = birPackageFile;
}
byte[] pkgBirBinaryContent = PackageFileWriter.writePackage(birPackageFile);
birContent.writeBytes(pkgBirBinaryContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,16 @@ public Module module(@Nullable ModuleId moduleId) {
return this.moduleMap.computeIfAbsent(moduleId, this.populateModuleFunc);
}

@Nullable
public Module module(ModuleName moduleName) {
for (Module module : this.moduleMap.values()) {
if (module.moduleName().equals(moduleName)) {
return module;
}
}

ModuleContext moduleContext = this.packageContext.moduleContext(moduleName);
if (moduleContext != null) {
return module(moduleContext.moduleId());
}

return null;
return this.packageContext.moduleContext(moduleName)
.map(moduleContext -> module(moduleContext.moduleId()))
.orElseThrow(() -> new IllegalArgumentException("Module not found: " + moduleName));
}

public boolean containsModule(ModuleId moduleId) {
Expand Down Expand Up @@ -752,7 +748,9 @@ private void deleteCaches(ResolvedPackageDependency dependency,
}

private void updatePackageManifest() {
ManifestBuilder manifestBuilder = ManifestBuilder.from(this.ballerinaTomlContext.tomlDocument(),
ManifestBuilder manifestBuilder = ManifestBuilder.from(
Optional.ofNullable(this.ballerinaTomlContext)
.map(TomlDocumentContext::tomlDocument).orElse(null),
Optional.ofNullable(this.compilerPluginTomlContext)
.map(TomlDocumentContext::tomlDocument).orElse(null),
Optional.ofNullable(this.balToolTomlContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ ModuleContext moduleContext(ModuleId moduleId) {
return moduleContextMap.get(moduleId);
}

@Nullable ModuleContext moduleContext(ModuleName moduleName) {
Optional<ModuleContext> moduleContext(ModuleName moduleName) {
for (ModuleContext moduleContext : moduleContextMap.values()) {
if (moduleContext.moduleName().equals(moduleName)) {
return moduleContext;
return Optional.of(moduleContext);
}
}
return null;
return Optional.empty();
}

ModuleContext defaultModuleContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,7 @@ static Optional<ModuleContext> findModuleInPackage(PackageContext resolvedPackag
}
moduleName = ModuleName.from(packageName, moduleNamePart);
}
ModuleContext resolvedModule = resolvedPackage.moduleContext(moduleName);
if (resolvedModule == null) {
return Optional.empty();
}

// TODO convert this to a debug log
return Optional.of(resolvedModule);
return resolvedPackage.moduleContext(moduleName);
}

private DependencyGraph<ResolvedPackageDependency> buildPackageGraph(DependencyGraph<DependencyNode> depGraph,
Expand Down Expand Up @@ -705,10 +699,9 @@ public Optional<ModuleContext> getModule(PackageOrg packageOrg, String moduleNam

PackageName packageName;
// TODO remove the null check and else block once the new resolution is fully done
packageName = importModuleResponse.packageDescriptor().name();
packageName = importModuleResponse.packageDescriptor().orElseThrow().name();

Optional<Package> optionalPackage = getPackage(packageOrg,
packageName);
Optional<Package> optionalPackage = getPackage(packageOrg, packageName);
if (optionalPackage.isEmpty()) {
return Optional.empty();
// This branch cannot be executed since the package is resolved before hand
Expand Down
Loading

0 comments on commit 3eaf480

Please sign in to comment.