Merge branch 'pr-2695' into master

[java] Improve example for AvoidSynchronizedAtMethodLevel #2695
This commit is contained in:
Andreas Dangel
2020-08-21 10:57:22 +02:00

View File

@ -24,30 +24,50 @@ gets it.
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>//MethodDeclaration[@Synchronized= true()]</value>
<value>//MethodDeclaration[@Synchronized = true()]</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
// Try to avoid this:
synchronized void foo() {
}
// Prefer this:
void bar() {
synchronized(this) {
// Try to avoid this:
synchronized void foo() {
// code, that doesn't need synchronization
// ...
// code, that requires synchronization
if (!sharedData.has("bar")) {
sharedData.add("bar");
}
// more code, that doesn't need synchronization
// ...
}
}
// Try to avoid this for static methods:
static synchronized void fooStatic() {
}
// Prefer this:
static void barStatic() {
synchronized(Foo.class) {
// Prefer this:
void bar() {
// code, that doesn't need synchronization
// ...
synchronized(this) {
if (!sharedData.has("bar")) {
sharedData.add("bar");
}
}
// more code, that doesn't need synchronization
// ...
}
// Try to avoid this for static methods:
static synchronized void fooStatic() {
}
// Prefer this:
static void barStatic() {
// code, that doesn't need synchronization
// ...
synchronized(Foo.class) {
// code, that requires synchronization
}
// more code, that doesn't need synchronization
// ...
}
}
}
]]>
</example>