From bfd9500dcdf685c8beafabd8ded31c5cd094d475 Mon Sep 17 00:00:00 2001
From: Felix Schnabel <f.schnabel@tum.de>
Date: Wed, 4 Dec 2024 02:11:00 +0100
Subject: [PATCH] Fix Spotbugs

---
 .../debugadapter/JBallerinaDebugServer.java   | 100 ++++++++++--------
 .../debugadapter/variable/types/BRecord.java  |  31 +++---
 2 files changed, 75 insertions(+), 56 deletions(-)

diff --git a/misc/debug-adapter/modules/debug-adapter-core/src/main/java/org/ballerinalang/debugadapter/JBallerinaDebugServer.java b/misc/debug-adapter/modules/debug-adapter-core/src/main/java/org/ballerinalang/debugadapter/JBallerinaDebugServer.java
index 2b75da6f752f..c03afff94af5 100755
--- a/misc/debug-adapter/modules/debug-adapter-core/src/main/java/org/ballerinalang/debugadapter/JBallerinaDebugServer.java
+++ b/misc/debug-adapter/modules/debug-adapter-core/src/main/java/org/ballerinalang/debugadapter/JBallerinaDebugServer.java
@@ -829,7 +829,7 @@ && isBalStrand(threadReference)
     private static boolean isBalStrand(ThreadReference threadReference) {
         // Todo - Refactor to use thread proxy implementation
         try {
-            return isBalStackFrame(threadReference.frames().get(0));
+            return isBalStackFrame(threadReference.frames().getFirst());
         } catch (Exception e) {
             return false;
         }
@@ -960,13 +960,14 @@ private boolean isNoDebugMode() {
 
     private Variable[] computeGlobalScopeVariables(VariablesArguments requestArgs) {
         int stackFrameReference = requestArgs.getVariablesReference();
-        String classQName = PackageUtils.getQualifiedClassName(suspendedContext, INIT_CLASS_NAME);
-        List<ReferenceType> cls = suspendedContext.getAttachedVm().classesByName(classQName);
+        String classQName = PackageUtils.getQualifiedClassName(
+                Objects.requireNonNull(suspendedContext), INIT_CLASS_NAME);
+        List<ReferenceType> cls = Objects.requireNonNull(suspendedContext).getAttachedVm().classesByName(classQName);
         if (cls.size() != 1) {
             return new Variable[0];
         }
         List<CompletableFuture<Variable>> scheduledVariables = new ArrayList<>();
-        ReferenceType initClassReference = cls.get(0);
+        ReferenceType initClassReference = cls.getFirst();
         for (Field field : initClassReference.allFields()) {
             String fieldName = Utils.decodeIdentifier(field.name());
             if (!field.isPublic() || !field.isStatic() || fieldName.startsWith(GENERATED_VAR_PREFIX)) {
@@ -991,7 +992,7 @@ private Variable[] computeGlobalScopeVariables(VariablesArguments requestArgs) {
     }
 
     private Variable[] computeLocalScopeVariables(VariablesArguments args) throws Exception {
-        StackFrameProxyImpl stackFrame = suspendedContext.getFrame();
+        StackFrameProxyImpl stackFrame = Objects.requireNonNull(suspendedContext).getFrame();
         List<CompletableFuture<Variable>> scheduledVariables = new ArrayList<>();
         List<CompletableFuture<Variable[]>> scheduledLambdaMapVariables = new ArrayList<>();
         List<LocalVariableProxyImpl> localVariableProxies = stackFrame.visibleVariables();
@@ -1073,16 +1074,19 @@ private CompletableFuture<Variable[]> fetchLocalVariablesFromMap(VariablesArgume
     private CompletableFuture<Variable> computeVariableAsync(String name, Value value, Integer stackFrameRef) {
         return CompletableFuture.supplyAsync(() -> {
             BVariable variable = VariableFactory.getVariable(suspendedContext, name, value);
-            if (variable == null) {
-                return null;
-            }
-            if (variable instanceof BSimpleVariable) {
-                variable.getDapVariable().setVariablesReference(0);
-            } else if (variable instanceof BCompoundVariable) {
-                int variableReference = nextVarReference.getAndIncrement();
-                variable.getDapVariable().setVariablesReference(variableReference);
-                loadedCompoundVariables.put(variableReference, (BCompoundVariable) variable);
-                updateVariableToStackFrameMap(stackFrameRef, variableReference);
+            switch (variable) {
+                case null -> {
+                    return null;
+                }
+                case BSimpleVariable bSimpleVariable -> bSimpleVariable.getDapVariable().setVariablesReference(0);
+                case BCompoundVariable bCompoundVariable -> {
+                    int variableReference = nextVarReference.getAndIncrement();
+                    variable.getDapVariable().setVariablesReference(variableReference);
+                    loadedCompoundVariables.put(variableReference, bCompoundVariable);
+                    updateVariableToStackFrameMap(stackFrameRef, variableReference);
+                }
+                default -> {
+                }
             }
             return variable.getDapVariable();
         }, variableExecutor);
@@ -1128,15 +1132,19 @@ private Variable[] createVariableArrayFrom(VariablesArguments args, Map<String,
             String name = entry.getKey();
             Value value = entry.getValue();
             BVariable variable = VariableFactory.getVariable(suspendedContext, name, value);
-            if (variable == null) {
-                return null;
-            } else if (variable instanceof BSimpleVariable) {
-                variable.getDapVariable().setVariablesReference(0);
-            } else if (variable instanceof BCompoundVariable) {
-                int variableReference = nextVarReference.getAndIncrement();
-                variable.getDapVariable().setVariablesReference(variableReference);
-                loadedCompoundVariables.put(variableReference, (BCompoundVariable) variable);
-                updateVariableToStackFrameMap(args.getVariablesReference(), variableReference);
+            switch (variable) {
+                case null -> {
+                    return null;
+                }
+                case BSimpleVariable bSimpleVariable -> bSimpleVariable.getDapVariable().setVariablesReference(0);
+                case BCompoundVariable bCompoundVariable -> {
+                    int variableReference = nextVarReference.getAndIncrement();
+                    variable.getDapVariable().setVariablesReference(variableReference);
+                    loadedCompoundVariables.put(variableReference, bCompoundVariable);
+                    updateVariableToStackFrameMap(args.getVariablesReference(), variableReference);
+                }
+                default -> {
+                }
             }
             return variable.getDapVariable();
         }).filter(Objects::nonNull).toArray(Variable[]::new);
@@ -1149,15 +1157,19 @@ private Variable[] createVariableArrayFrom(VariablesArguments args, List<Value>
         return varMap.stream().map(value -> {
             String name = String.format("[%d]", index.getAndIncrement());
             BVariable variable = VariableFactory.getVariable(suspendedContext, name, value);
-            if (variable == null) {
-                return null;
-            } else if (variable instanceof BSimpleVariable) {
-                variable.getDapVariable().setVariablesReference(0);
-            } else if (variable instanceof BCompoundVariable) {
-                int variableReference = nextVarReference.getAndIncrement();
-                variable.getDapVariable().setVariablesReference(variableReference);
-                loadedCompoundVariables.put(variableReference, (BCompoundVariable) variable);
-                updateVariableToStackFrameMap(args.getVariablesReference(), variableReference);
+            switch (variable) {
+                case null -> {
+                    return null;
+                }
+                case BSimpleVariable bSimpleVariable -> bSimpleVariable.getDapVariable().setVariablesReference(0);
+                case BCompoundVariable bCompoundVariable -> {
+                    int variableReference = nextVarReference.getAndIncrement();
+                    variable.getDapVariable().setVariablesReference(variableReference);
+                    loadedCompoundVariables.put(variableReference, bCompoundVariable);
+                    updateVariableToStackFrameMap(args.getVariablesReference(), variableReference);
+                }
+                default -> {
+                }
             }
             return variable.getDapVariable();
         }).filter(Objects::nonNull).toArray(Variable[]::new);
@@ -1171,15 +1183,19 @@ private Variable[] createVariableArrayFrom(VariablesArguments args, List<Value>
      */
     private EvaluateResponse constructEvaluateResponse(EvaluateArguments args, BVariable evaluationResult) {
         EvaluateResponse response = new EvaluateResponse();
-        if (evaluationResult == null) {
-            return response;
-        } else if (evaluationResult instanceof BSimpleVariable) {
-            evaluationResult.getDapVariable().setVariablesReference(0);
-        } else if (evaluationResult instanceof BCompoundVariable) {
-            int variableReference = nextVarReference.getAndIncrement();
-            evaluationResult.getDapVariable().setVariablesReference(variableReference);
-            loadedCompoundVariables.put(variableReference, (BCompoundVariable) evaluationResult);
-            updateVariableToStackFrameMap(args.getFrameId(), variableReference);
+        switch (evaluationResult) {
+            case null -> {
+                return response;
+            }
+            case BSimpleVariable bSimpleVariable -> bSimpleVariable.getDapVariable().setVariablesReference(0);
+            case BCompoundVariable bCompoundVariable -> {
+                int variableReference = nextVarReference.getAndIncrement();
+                evaluationResult.getDapVariable().setVariablesReference(variableReference);
+                loadedCompoundVariables.put(variableReference, bCompoundVariable);
+                updateVariableToStackFrameMap(args.getFrameId(), variableReference);
+            }
+            default -> {
+            }
         }
 
         Variable dapVariable = evaluationResult.getDapVariable();
diff --git a/misc/debug-adapter/modules/debug-adapter-core/src/main/java/org/ballerinalang/debugadapter/variable/types/BRecord.java b/misc/debug-adapter/modules/debug-adapter-core/src/main/java/org/ballerinalang/debugadapter/variable/types/BRecord.java
index 6f2ff4381f2a..524ee4fef653 100644
--- a/misc/debug-adapter/modules/debug-adapter-core/src/main/java/org/ballerinalang/debugadapter/variable/types/BRecord.java
+++ b/misc/debug-adapter/modules/debug-adapter-core/src/main/java/org/ballerinalang/debugadapter/variable/types/BRecord.java
@@ -88,7 +88,7 @@ public Map<String, Value> computeChildVariables() {
     @Nullable
     private Map<Value, Value> getRecordFields() {
         try {
-            loadAllKeys();
+            ArrayReference loadedKeys = loadAllKeys();
             Map<Value, Value> recordFields = new LinkedHashMap<>();
             if (loadedKeys == null) {
                 return null;
@@ -105,19 +105,22 @@ private Map<Value, Value> getRecordFields() {
         }
     }
 
-    private void loadAllKeys() {
-        if (loadedKeys == null) {
-            try {
-                Optional<Method> getKeysMethod = VariableUtils.getMethod(jvmValue, METHOD_GET_KEYS,
-                        GETKEYS_METHOD_SIGNATURE_PATTERN);
-                Value keyArray = ((ObjectReference) jvmValue).invokeMethod(
-                        context.getOwningThread().getThreadReference(), getKeysMethod.get(), Collections.emptyList(),
-                        ObjectReference.INVOKE_SINGLE_THREADED);
-                loadedKeys = (ArrayReference) keyArray;
-            } catch (Exception ignored) {
-                loadedKeys = null;
-            }
+    @Nullable
+    private ArrayReference loadAllKeys() {
+        if (loadedKeys != null) {
+            return loadedKeys;
+        }
+        try {
+            Optional<Method> getKeysMethod = VariableUtils.getMethod(jvmValue, METHOD_GET_KEYS,
+                    GETKEYS_METHOD_SIGNATURE_PATTERN);
+            Value keyArray = ((ObjectReference) jvmValue).invokeMethod(
+                    context.getOwningThread().getThreadReference(), getKeysMethod.get(), Collections.emptyList(),
+                    ObjectReference.INVOKE_SINGLE_THREADED);
+            loadedKeys = (ArrayReference) keyArray;
+        } catch (Exception ignored) {
+            loadedKeys = null;
         }
+        return loadedKeys;
     }
 
     @Nullable
@@ -140,7 +143,7 @@ public int getChildrenCount() {
             if (!(jvmValue instanceof ObjectReference)) {
                 return 0;
             }
-            loadAllKeys();
+            ArrayReference loadedKeys = loadAllKeys();
             return loadedKeys == null ? 0 : loadedKeys.length();
         } catch (Exception ignored) {
             return 0;