Skip to content

Commit

Permalink
Fix Spotbugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow-Devil committed Dec 4, 2024
1 parent 9c39917 commit bfd9500
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit bfd9500

Please sign in to comment.