-
Notifications
You must be signed in to change notification settings - Fork 630
Multi-threaded code checks can be skipped by declaring @NotThreadSafe
#3399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi-threaded code checks can be skipped by declaring @NotThreadSafe
#3399
Conversation
@@ -57,6 +57,9 @@ private MultiThreadedCodeIdentifierUtils() { | |||
|
|||
public static boolean isPartOfMultiThreadedCode(ClassContext classContext) { | |||
JavaClass javaClass = classContext.getJavaClass(); | |||
if (notThreadSafe(javaClass.getAnnotationEntries())) { | |||
return false; | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to introduce a new method here: now the name of the method does not match with what it does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @gtoison , I am not sure I understand your request here. From my understanding, "multi-threaded code" here refers to code that is in use from multiple threads and should be checked.
The @NotThreadSafe
annotation is an explicit declaration from the user - "this code is not intended to be used from multiple threads". Therefore, any code marked @NotThreadSafe
is definitionally not "part of multi-threaded code", as the user has told us it is not.
Do you have a better method name to propose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuditKnoll previously indicated this would be a good place for the logic to live - perhaps they have a suggestion of the best way to resolve this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently isPartOfMultiThreadedCode()
means "that class does things that imply intent to use multiple threads".
On the other hand, @NotThreadSafe
means "this won't work when used from multiple threads".
I think something like isNotThreadSafe()
or isExplicitelyNotThreadSafe()
would convey that meaning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently this function is only used in the SharedVariableAtomicityDetector to decide whether the class should be visited and the bugs reported. To my best knowledge, there are no ongoing work (open PRs in SpotBugs) depending on this function.
From the detector's POV, it's true, it would prevent Fps, however there could be another usecase, where this annotation check is not necessary. (It's true that it could be easily refactored, if it's necessary, if the code depending on it is inside SpotBugs.)
Introducing a new method with the @NotThreadSafe annotation check and other functionalities of the function leaves open this option (of course with calling the new method from the SharedVariableAtomicityDetector).
Maybe isThreadSafeMultiThreadedClass
or shouldBeThreadSafeMultiThreadedClass
names could reflect this check as well?
bc6f4ef
to
7ad2b2b
Compare
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can see, the original NotThreadSafe
annotation (see javadoc of the jakarta version or a copy of the javax annotation) has some different annotations: @Documented
, @Target(TYPE)
and @Retention(Class)
.
If there is no particular reason to change the fake annotations, it's best to keep them the same as the original ones IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I updated this
@@ -57,6 +57,9 @@ private MultiThreadedCodeIdentifierUtils() { | |||
|
|||
public static boolean isPartOfMultiThreadedCode(ClassContext classContext) { | |||
JavaClass javaClass = classContext.getJavaClass(); | |||
if (notThreadSafe(javaClass.getAnnotationEntries())) { | |||
return false; | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently this function is only used in the SharedVariableAtomicityDetector to decide whether the class should be visited and the bugs reported. To my best knowledge, there are no ongoing work (open PRs in SpotBugs) depending on this function.
From the detector's POV, it's true, it would prevent Fps, however there could be another usecase, where this annotation check is not necessary. (It's true that it could be easily refactored, if it's necessary, if the code depending on it is inside SpotBugs.)
Introducing a new method with the @NotThreadSafe annotation check and other functionalities of the function leaves open this option (of course with calling the new method from the SharedVariableAtomicityDetector).
Maybe isThreadSafeMultiThreadedClass
or shouldBeThreadSafeMultiThreadedClass
names could reflect this check as well?
I've pushed a commit to illustrate what I meant, IMO the |
Sounds good to me. Let me know if I should rebase / squash the commit or anything. |
@@ -151,4 +151,18 @@ public static boolean isLocked(Method currentMethod, CFG currentCFG, LockDataflo | |||
} | |||
return false; | |||
} | |||
|
|||
/** | |||
* @return <code>true</code> if the class is explicitely annotated with <code>NotThreadSafe</code> to document that it is not thread sage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
Fixup on spotbugs#3399
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for the contribution!
@@ -57,6 +57,9 @@ private MultiThreadedCodeIdentifierUtils() { | |||
|
|||
public static boolean isPartOfMultiThreadedCode(ClassContext classContext) { | |||
JavaClass javaClass = classContext.getJavaClass(); | |||
if (notThreadSafe(javaClass.getAnnotationEntries())) { | |||
return false; | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently isPartOfMultiThreadedCode()
means "that class does things that imply intent to use multiple threads".
On the other hand, @NotThreadSafe
means "this won't work when used from multiple threads".
I think something like isNotThreadSafe()
or isExplicitelyNotThreadSafe()
would convey that meaning.
Fixes #3390
Before opening a 'pull request'
Make sure these boxes are checked before submitting your PR -- thank you!
CHANGELOG.md
if you have changed SpotBugs code