From e0eef2ce019202d32982525a69678640cc1b4a7f Mon Sep 17 00:00:00 2001 From: Sustech-yx <59305237+Sustech-yx@users.noreply.github.com> Date: Wed, 25 May 2022 23:00:28 +0800 Subject: [PATCH 1/3] Enhancement for issue #2061. --- CHANGELOG.md | 3 + .../cs/findbugs/ba/ResourceValueAnalysis.java | 11 +-- .../cs/findbugs/ba/ResourceValueFrame.java | 87 +++++++++++-------- .../ba/ResourceValueFrameModelingVisitor.java | 10 +-- .../cs/findbugs/detect/FindOpenStream.java | 9 +- .../findbugs/detect/FindUnreleasedLock.java | 18 ++-- .../detect/StreamFrameModelingVisitor.java | 16 ++-- 7 files changed, 90 insertions(+), 64 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5187196580..8c3c9578aeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ This is the changelog for SpotBugs. This follows [Keep a Changelog v1.0.0](http: Currently the versioning policy of this project follows [Semantic Versioning v2.0.0](http://semver.org/spec/v2.0.0.html). ## Unreleased - 2022-??-?? +### Changed +- Rewrite some member in `ResourceValueFrame.java` to Enum([#2061](https://github.com/spotbugs/spotbugs/issues/2061)) + ### Fixed - Fixed False positives for `RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE` on try-with-resources with interface references ([#1931](https://github.com/spotbugs/spotbugs/issues/1931)) diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java index 8fb4b4c957e..cc846f3dbf5 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java @@ -102,10 +102,10 @@ public void meetInto(ResourceValueFrame fact, Edge edge, ResourceValueFrame resu return; } - if (fact.getStatus() == ResourceValueFrame.OPEN) { + if (fact.getStatus() == ResourceValueFrame.ResourceValueEnum.OPEN) { // If status is OPEN, downgrade to OPEN_ON_EXCEPTION_PATH tmpFact = modifyFrame(fact, null); - tmpFact.setStatus(ResourceValueFrame.OPEN_ON_EXCEPTION_PATH); + tmpFact.setStatus(ResourceValueFrame.ResourceValueEnum.OPEN_ON_EXCEPTION_PATH); } if (fact.isValid()) { @@ -122,7 +122,7 @@ public void meetInto(ResourceValueFrame fact, Edge edge, ResourceValueFrame resu && resourceTracker.isResourceClose(fallThroughSuccessor, exceptionThrower, methodGen.getConstantPool(), resource, fact)) { tmpFact = modifyFrame(fact, tmpFact); - tmpFact.setStatus(ResourceValueFrame.CLOSED); + tmpFact.setStatus(ResourceValueFrame.ResourceValueEnum.CLOSED); if (DEBUG) { System.out.print("(failed attempt to close)"); } @@ -192,7 +192,7 @@ else if (lastInSource instanceof IFNULL || lastInSource instanceof IFNONNULL) { if ((isNullCheck && edgeType == IFCMP_EDGE) || (isNonNullCheck && edgeType == FALL_THROUGH_EDGE)) { // System.out.println("**** making resource nonexistent on edge "+edge.getId()); tmpFact = modifyFrame(fact, tmpFact); - tmpFact.setStatus(ResourceValueFrame.NONEXISTENT); + tmpFact.setStatus(ResourceValueFrame.ResourceValueEnum.NONEXISTENT); } } } @@ -213,7 +213,8 @@ protected void mergeInto(ResourceValueFrame frame, ResourceValueFrame result) th super.mergeInto(frame, result); // Merge status - result.setStatus(Math.min(result.getStatus(), frame.getStatus())); + result.setStatus(result.getStatus().getType() > frame.getStatus().getType() ? + frame.getStatus() : result.getStatus()); } @Override diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrame.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrame.java index fe8faf315ce..7c57f519b4c 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrame.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrame.java @@ -20,50 +20,63 @@ package edu.umd.cs.findbugs.ba; public class ResourceValueFrame extends Frame { - /** - * The resource escapes the method. - */ - public static final int ESCAPED = 0; - - /** - * The resource is open (or locked, etc) on paths that include only normal - * control flow. - */ - public static final int OPEN = 1; - - /** - * The resource is open (or locked, etc) on paths that include exception - * control flow. - */ - public static final int OPEN_ON_EXCEPTION_PATH = 2; - - /** - * The resource is closed (or unlocked, etc). - */ - public static final int CLOSED = 3; - - /** - * The resource has been created, but is not open. - */ - public static final int CREATED = 4; - - /** - * The resource doesn't exist. - */ - public static final int NONEXISTENT = 5; - - private int status; + + public enum ResourceValueEnum { + /** + * The resource escapes the method. + */ + ESCAPED(0), + + /** + * The resource is open (or locked, etc) on paths that include only normal + * control flow. + */ + OPEN(1), + + /** + * The resource is open (or locked, etc) on paths that include exception + * control flow. + */ + OPEN_ON_EXCEPTION_PATH(2), + + /** + * The resource is closed (or unlocked, etc). + */ + CLOSED(3), + + /** + * The resource has been created, but is not open. + */ + CREATED(4), + + /** + * The resource doesn't exist. + */ + NONEXISTENT(5); + + ResourceValueEnum(int type) { + this.type = type; + } + + private final int type; + + public int getType() { + return type; + } + } + + private ResourceValueEnum status; public ResourceValueFrame(int numSlots) { super(numSlots); - this.status = NONEXISTENT; + this.status = ResourceValueEnum.NONEXISTENT; } - public int getStatus() { + public ResourceValueEnum getStatus() { return status; } - public void setStatus(int status) { + public void setStatus(ResourceValueEnum status) { this.status = status; } @@ -89,7 +102,7 @@ public void copyFrom(Frame other_) { @Override public String toString() { - return super.toString() + statusList[status]; + return super.toString() + statusList[status.getType()]; } } diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrameModelingVisitor.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrameModelingVisitor.java index 8330ccdd038..82a0e7ead51 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrameModelingVisitor.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrameModelingVisitor.java @@ -63,7 +63,7 @@ private void handleFieldStore(FieldInstruction ins) { ResourceValueFrame frame = getFrame(); ResourceValue topValue = frame.getTopValue(); if (topValue.equals(ResourceValue.instance())) { - frame.setStatus(ResourceValueFrame.ESCAPED); + frame.setStatus(ResourceValueFrame.ResourceValueEnum.ESCAPED); } } catch (DataflowAnalysisException e) { throw new InvalidBytecodeException("Stack underflow", e); @@ -86,7 +86,7 @@ private void handleArrayStore(ArrayInstruction ins) { ResourceValueFrame frame = getFrame(); ResourceValue topValue = frame.getTopValue(); if (topValue.equals(ResourceValue.instance())) { - frame.setStatus(ResourceValueFrame.ESCAPED); + frame.setStatus(ResourceValueFrame.ResourceValueEnum.ESCAPED); } } catch (DataflowAnalysisException e) { throw new InvalidBytecodeException("Stack underflow", e); @@ -135,7 +135,7 @@ private void handleInvoke(InvokeInstruction inv) { } if (instanceArgNum >= 0 && instanceEscapes(inv, instanceArgNum)) { - frame.setStatus(ResourceValueFrame.ESCAPED); + frame.setStatus(ResourceValueFrame.ResourceValueEnum.ESCAPED); } handleNormalInstruction(inv); @@ -150,7 +150,7 @@ public void visitCHECKCAST(CHECKCAST obj) { topValue = frame.getTopValue(); if (topValue.equals(ResourceValue.instance())) { - frame.setStatus(ResourceValueFrame.ESCAPED); + frame.setStatus(ResourceValueFrame.ResourceValueEnum.ESCAPED); } } catch (DataflowAnalysisException e) { AnalysisContext.logError("Analysis error", e); @@ -183,7 +183,7 @@ public void visitARETURN(ARETURN ins) { ResourceValueFrame frame = getFrame(); ResourceValue topValue = frame.getTopValue(); if (topValue.equals(ResourceValue.instance())) { - frame.setStatus(ResourceValueFrame.ESCAPED); + frame.setStatus(ResourceValueFrame.ResourceValueEnum.ESCAPED); } } catch (DataflowAnalysisException e) { throw new InvalidBytecodeException("Stack underflow", e); diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindOpenStream.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindOpenStream.java index 822236060ab..17a25fd7220 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindOpenStream.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindOpenStream.java @@ -488,21 +488,22 @@ public void inspectResult(ClassContext classContext, MethodGen methodGen, CFG cf } ResourceValueFrame exitFrame = dataflow.getResultFact(cfg.getExit()); - int exitStatus = exitFrame.getStatus(); - if (exitStatus == ResourceValueFrame.OPEN || exitStatus == ResourceValueFrame.OPEN_ON_EXCEPTION_PATH) { + ResourceValueFrame.ResourceValueEnum exitStatus = exitFrame.getStatus(); + if (exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN || + exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN_ON_EXCEPTION_PATH) { // FIXME: Stream object should be queried for the // priority. String bugType = stream.getBugType(); int priority = NORMAL_PRIORITY; - if (exitStatus == ResourceValueFrame.OPEN_ON_EXCEPTION_PATH) { + if (exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN_ON_EXCEPTION_PATH) { bugType += "_EXCEPTION_PATH"; priority = LOW_PRIORITY; } potentialOpenStreamList.add(new PotentialOpenStream(bugType, priority, stream)); - } else if (exitStatus == ResourceValueFrame.CLOSED) { + } else if (exitStatus == ResourceValueFrame.ResourceValueEnum.CLOSED) { // Remember that this stream was closed on all paths. // Later, we will mark all of the streams in its equivalence class // as having been closed. diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindUnreleasedLock.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindUnreleasedLock.java index 3a1dac5c7c8..1c7953793c5 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindUnreleasedLock.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindUnreleasedLock.java @@ -108,7 +108,8 @@ public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock) final ConstantPoolGen cpg = getCPG(); final ResourceValueFrame frame = getFrame(); - int status = -1; + ResourceValueFrame.ResourceValueEnum status = ResourceValueFrame.ResourceValueEnum.NONEXISTENT; + boolean updated = false; if (DEBUG) { System.out.println("PC : " + handle.getPosition() + " " + ins); @@ -123,12 +124,14 @@ public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock) // Is a lock acquired or released by this instruction? Location creationPoint = lock.getLocation(); if (handle == creationPoint.getHandle() && basicBlock == creationPoint.getBasicBlock()) { - status = ResourceValueFrame.OPEN; + status = ResourceValueFrame.ResourceValueEnum.OPEN; + updated = true; if (DEBUG) { System.out.println("OPEN"); } } else if (resourceTracker.isResourceClose(basicBlock, handle, cpg, lock, frame)) { - status = ResourceValueFrame.CLOSED; + status = ResourceValueFrame.ResourceValueEnum.CLOSED; + updated = true; if (DEBUG) { System.out.println("CLOSE"); } @@ -166,7 +169,7 @@ public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock) } // If needed, update frame status - if (status != -1) { + if (updated) { frame.setStatus(status); } if (DEBUG) { @@ -431,12 +434,13 @@ public void inspectResult(ClassContext classContext, MethodGen methodGen, CFG cf if (DEBUG) { System.out.println("Resource value at exit: " + exitFrame); } - int exitStatus = exitFrame.getStatus(); + ResourceValueFrame.ResourceValueEnum exitStatus = exitFrame.getStatus(); - if (exitStatus == ResourceValueFrame.OPEN || exitStatus == ResourceValueFrame.OPEN_ON_EXCEPTION_PATH) { + if (exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN || + exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN_ON_EXCEPTION_PATH) { String bugType; int priority; - if (exitStatus == ResourceValueFrame.OPEN) { + if (exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN) { bugType = "UL_UNRELEASED_LOCK"; priority = HIGH_PRIORITY; } else { diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/StreamFrameModelingVisitor.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/StreamFrameModelingVisitor.java index f52c5ec1c3e..d558be9cf9c 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/StreamFrameModelingVisitor.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/StreamFrameModelingVisitor.java @@ -57,7 +57,8 @@ public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock) final Instruction ins = handle.getInstruction(); final ResourceValueFrame frame = getFrame(); - int status = -1; + ResourceValueFrame.ResourceValueEnum status = ResourceValueFrame.ResourceValueEnum.NONEXISTENT; + boolean updated = false; boolean created = false; // Is a resource created, opened, or closed by this instruction? @@ -65,28 +66,31 @@ public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock) if (handle == creationPoint.getHandle() && basicBlock == creationPoint.getBasicBlock()) { // Resource creation if (stream.isOpenOnCreation()) { - status = ResourceValueFrame.OPEN; + status = ResourceValueFrame.ResourceValueEnum.OPEN; stream.setOpenLocation(location); resourceTracker.addStreamOpenLocation(location, stream); } else { - status = ResourceValueFrame.CREATED; + status = ResourceValueFrame.ResourceValueEnum.CREATED; } + updated = true; created = true; } else if (resourceTracker.isResourceOpen(basicBlock, handle, cpg, stream, frame)) { // Resource opened - status = ResourceValueFrame.OPEN; + status = ResourceValueFrame.ResourceValueEnum.OPEN; + updated = true; stream.setOpenLocation(location); resourceTracker.addStreamOpenLocation(location, stream); } else if (resourceTracker.isResourceClose(basicBlock, handle, cpg, stream, frame)) { // Resource closed - status = ResourceValueFrame.CLOSED; + status = ResourceValueFrame.ResourceValueEnum.CLOSED; + updated = true; } // Model use of instance values in frame slots analyzeInstruction(ins); // If needed, update frame status - if (status != -1) { + if (updated) { frame.setStatus(status); if (created) { frame.setValue(frame.getNumSlots() - 1, ResourceValue.instance()); From 50629590055b0bb311458029188376f91e3ec4fc Mon Sep 17 00:00:00 2001 From: Sustech-yx <59305237+Sustech-yx@users.noreply.github.com> Date: Thu, 26 May 2022 03:22:54 +0800 Subject: [PATCH 2/3] Modify the enum name. --- .../umd/cs/findbugs/ba/ResourceValueAnalysis.java | 8 ++++---- .../edu/umd/cs/findbugs/ba/ResourceValueFrame.java | 12 ++++++------ .../ba/ResourceValueFrameModelingVisitor.java | 10 +++++----- .../edu/umd/cs/findbugs/detect/FindOpenStream.java | 10 +++++----- .../umd/cs/findbugs/detect/FindUnreleasedLock.java | 14 +++++++------- .../detect/StreamFrameModelingVisitor.java | 10 +++++----- 6 files changed, 32 insertions(+), 32 deletions(-) diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java index cc846f3dbf5..1fef1d9e89e 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java @@ -102,10 +102,10 @@ public void meetInto(ResourceValueFrame fact, Edge edge, ResourceValueFrame resu return; } - if (fact.getStatus() == ResourceValueFrame.ResourceValueEnum.OPEN) { + if (fact.getStatus() == ResourceValueFrame.State.OPEN) { // If status is OPEN, downgrade to OPEN_ON_EXCEPTION_PATH tmpFact = modifyFrame(fact, null); - tmpFact.setStatus(ResourceValueFrame.ResourceValueEnum.OPEN_ON_EXCEPTION_PATH); + tmpFact.setStatus(ResourceValueFrame.State.OPEN_ON_EXCEPTION_PATH); } if (fact.isValid()) { @@ -122,7 +122,7 @@ public void meetInto(ResourceValueFrame fact, Edge edge, ResourceValueFrame resu && resourceTracker.isResourceClose(fallThroughSuccessor, exceptionThrower, methodGen.getConstantPool(), resource, fact)) { tmpFact = modifyFrame(fact, tmpFact); - tmpFact.setStatus(ResourceValueFrame.ResourceValueEnum.CLOSED); + tmpFact.setStatus(ResourceValueFrame.State.CLOSED); if (DEBUG) { System.out.print("(failed attempt to close)"); } @@ -192,7 +192,7 @@ else if (lastInSource instanceof IFNULL || lastInSource instanceof IFNONNULL) { if ((isNullCheck && edgeType == IFCMP_EDGE) || (isNonNullCheck && edgeType == FALL_THROUGH_EDGE)) { // System.out.println("**** making resource nonexistent on edge "+edge.getId()); tmpFact = modifyFrame(fact, tmpFact); - tmpFact.setStatus(ResourceValueFrame.ResourceValueEnum.NONEXISTENT); + tmpFact.setStatus(ResourceValueFrame.State.NONEXISTENT); } } } diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrame.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrame.java index 7c57f519b4c..7471468cac3 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrame.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrame.java @@ -21,7 +21,7 @@ public class ResourceValueFrame extends Frame { - public enum ResourceValueEnum { + public enum State { /** * The resource escapes the method. */ @@ -54,7 +54,7 @@ public enum ResourceValueEnum { */ NONEXISTENT(5); - ResourceValueEnum(int type) { + State(int type) { this.type = type; } @@ -65,18 +65,18 @@ public int getType() { } } - private ResourceValueEnum status; + private State status; public ResourceValueFrame(int numSlots) { super(numSlots); - this.status = ResourceValueEnum.NONEXISTENT; + this.status = State.NONEXISTENT; } - public ResourceValueEnum getStatus() { + public State getStatus() { return status; } - public void setStatus(ResourceValueEnum status) { + public void setStatus(State status) { this.status = status; } diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrameModelingVisitor.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrameModelingVisitor.java index 82a0e7ead51..aa5e2ce6548 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrameModelingVisitor.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueFrameModelingVisitor.java @@ -63,7 +63,7 @@ private void handleFieldStore(FieldInstruction ins) { ResourceValueFrame frame = getFrame(); ResourceValue topValue = frame.getTopValue(); if (topValue.equals(ResourceValue.instance())) { - frame.setStatus(ResourceValueFrame.ResourceValueEnum.ESCAPED); + frame.setStatus(ResourceValueFrame.State.ESCAPED); } } catch (DataflowAnalysisException e) { throw new InvalidBytecodeException("Stack underflow", e); @@ -86,7 +86,7 @@ private void handleArrayStore(ArrayInstruction ins) { ResourceValueFrame frame = getFrame(); ResourceValue topValue = frame.getTopValue(); if (topValue.equals(ResourceValue.instance())) { - frame.setStatus(ResourceValueFrame.ResourceValueEnum.ESCAPED); + frame.setStatus(ResourceValueFrame.State.ESCAPED); } } catch (DataflowAnalysisException e) { throw new InvalidBytecodeException("Stack underflow", e); @@ -135,7 +135,7 @@ private void handleInvoke(InvokeInstruction inv) { } if (instanceArgNum >= 0 && instanceEscapes(inv, instanceArgNum)) { - frame.setStatus(ResourceValueFrame.ResourceValueEnum.ESCAPED); + frame.setStatus(ResourceValueFrame.State.ESCAPED); } handleNormalInstruction(inv); @@ -150,7 +150,7 @@ public void visitCHECKCAST(CHECKCAST obj) { topValue = frame.getTopValue(); if (topValue.equals(ResourceValue.instance())) { - frame.setStatus(ResourceValueFrame.ResourceValueEnum.ESCAPED); + frame.setStatus(ResourceValueFrame.State.ESCAPED); } } catch (DataflowAnalysisException e) { AnalysisContext.logError("Analysis error", e); @@ -183,7 +183,7 @@ public void visitARETURN(ARETURN ins) { ResourceValueFrame frame = getFrame(); ResourceValue topValue = frame.getTopValue(); if (topValue.equals(ResourceValue.instance())) { - frame.setStatus(ResourceValueFrame.ResourceValueEnum.ESCAPED); + frame.setStatus(ResourceValueFrame.State.ESCAPED); } } catch (DataflowAnalysisException e) { throw new InvalidBytecodeException("Stack underflow", e); diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindOpenStream.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindOpenStream.java index 17a25fd7220..b2ac87390e2 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindOpenStream.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindOpenStream.java @@ -488,22 +488,22 @@ public void inspectResult(ClassContext classContext, MethodGen methodGen, CFG cf } ResourceValueFrame exitFrame = dataflow.getResultFact(cfg.getExit()); - ResourceValueFrame.ResourceValueEnum exitStatus = exitFrame.getStatus(); - if (exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN || - exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN_ON_EXCEPTION_PATH) { + ResourceValueFrame.State exitStatus = exitFrame.getStatus(); + if (exitStatus == ResourceValueFrame.State.OPEN || + exitStatus == ResourceValueFrame.State.OPEN_ON_EXCEPTION_PATH) { // FIXME: Stream object should be queried for the // priority. String bugType = stream.getBugType(); int priority = NORMAL_PRIORITY; - if (exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN_ON_EXCEPTION_PATH) { + if (exitStatus == ResourceValueFrame.State.OPEN_ON_EXCEPTION_PATH) { bugType += "_EXCEPTION_PATH"; priority = LOW_PRIORITY; } potentialOpenStreamList.add(new PotentialOpenStream(bugType, priority, stream)); - } else if (exitStatus == ResourceValueFrame.ResourceValueEnum.CLOSED) { + } else if (exitStatus == ResourceValueFrame.State.CLOSED) { // Remember that this stream was closed on all paths. // Later, we will mark all of the streams in its equivalence class // as having been closed. diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindUnreleasedLock.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindUnreleasedLock.java index 1c7953793c5..830e7be276f 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindUnreleasedLock.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/FindUnreleasedLock.java @@ -108,7 +108,7 @@ public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock) final ConstantPoolGen cpg = getCPG(); final ResourceValueFrame frame = getFrame(); - ResourceValueFrame.ResourceValueEnum status = ResourceValueFrame.ResourceValueEnum.NONEXISTENT; + ResourceValueFrame.State status = ResourceValueFrame.State.NONEXISTENT; boolean updated = false; if (DEBUG) { @@ -124,13 +124,13 @@ public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock) // Is a lock acquired or released by this instruction? Location creationPoint = lock.getLocation(); if (handle == creationPoint.getHandle() && basicBlock == creationPoint.getBasicBlock()) { - status = ResourceValueFrame.ResourceValueEnum.OPEN; + status = ResourceValueFrame.State.OPEN; updated = true; if (DEBUG) { System.out.println("OPEN"); } } else if (resourceTracker.isResourceClose(basicBlock, handle, cpg, lock, frame)) { - status = ResourceValueFrame.ResourceValueEnum.CLOSED; + status = ResourceValueFrame.State.CLOSED; updated = true; if (DEBUG) { System.out.println("CLOSE"); @@ -434,13 +434,13 @@ public void inspectResult(ClassContext classContext, MethodGen methodGen, CFG cf if (DEBUG) { System.out.println("Resource value at exit: " + exitFrame); } - ResourceValueFrame.ResourceValueEnum exitStatus = exitFrame.getStatus(); + ResourceValueFrame.State exitStatus = exitFrame.getStatus(); - if (exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN || - exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN_ON_EXCEPTION_PATH) { + if (exitStatus == ResourceValueFrame.State.OPEN || + exitStatus == ResourceValueFrame.State.OPEN_ON_EXCEPTION_PATH) { String bugType; int priority; - if (exitStatus == ResourceValueFrame.ResourceValueEnum.OPEN) { + if (exitStatus == ResourceValueFrame.State.OPEN) { bugType = "UL_UNRELEASED_LOCK"; priority = HIGH_PRIORITY; } else { diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/StreamFrameModelingVisitor.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/StreamFrameModelingVisitor.java index d558be9cf9c..14c59b2e899 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/StreamFrameModelingVisitor.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/StreamFrameModelingVisitor.java @@ -57,7 +57,7 @@ public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock) final Instruction ins = handle.getInstruction(); final ResourceValueFrame frame = getFrame(); - ResourceValueFrame.ResourceValueEnum status = ResourceValueFrame.ResourceValueEnum.NONEXISTENT; + ResourceValueFrame.State status = ResourceValueFrame.State.NONEXISTENT; boolean updated = false; boolean created = false; @@ -66,23 +66,23 @@ public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock) if (handle == creationPoint.getHandle() && basicBlock == creationPoint.getBasicBlock()) { // Resource creation if (stream.isOpenOnCreation()) { - status = ResourceValueFrame.ResourceValueEnum.OPEN; + status = ResourceValueFrame.State.OPEN; stream.setOpenLocation(location); resourceTracker.addStreamOpenLocation(location, stream); } else { - status = ResourceValueFrame.ResourceValueEnum.CREATED; + status = ResourceValueFrame.State.CREATED; } updated = true; created = true; } else if (resourceTracker.isResourceOpen(basicBlock, handle, cpg, stream, frame)) { // Resource opened - status = ResourceValueFrame.ResourceValueEnum.OPEN; + status = ResourceValueFrame.State.OPEN; updated = true; stream.setOpenLocation(location); resourceTracker.addStreamOpenLocation(location, stream); } else if (resourceTracker.isResourceClose(basicBlock, handle, cpg, stream, frame)) { // Resource closed - status = ResourceValueFrame.ResourceValueEnum.CLOSED; + status = ResourceValueFrame.State.CLOSED; updated = true; } From e21c70873833d4d37dc26ca52482b5db81dace6a Mon Sep 17 00:00:00 2001 From: Sustech-yx <59305237+Sustech-yx@users.noreply.github.com> Date: Tue, 31 May 2022 11:09:36 +0800 Subject: [PATCH 3/3] Fix build error. --- .../java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java index 1fef1d9e89e..22349859f9c 100644 --- a/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java +++ b/spotbugs/src/main/java/edu/umd/cs/findbugs/ba/ResourceValueAnalysis.java @@ -213,8 +213,7 @@ protected void mergeInto(ResourceValueFrame frame, ResourceValueFrame result) th super.mergeInto(frame, result); // Merge status - result.setStatus(result.getStatus().getType() > frame.getStatus().getType() ? - frame.getStatus() : result.getStatus()); + result.setStatus(result.getStatus().getType() > frame.getStatus().getType() ? frame.getStatus() : result.getStatus()); } @Override