InspectionGadgets question about synchronized contexts

I have a question about an inspection called 'Field accessed in both synchronized and unsynchronized contexts'

If in the synchronized block I access the boolean b directly, the inspection warns me that the field is accessed in both synchronized and unsynchronized contexts.
However, if I access the boolean b through the method isB(), the inspection don't flags anything.

isB() is public so it could be access by other objects.


What I would like to know is if the inspection is right not to put a warning when using the getter to access the boolean b. Accessing a value directly or throught a getter within a synchronized block isn't the same thing(just a cosmetic change)?


Below is a little example to illustrate this. Copy and paste this code and comment/uncomment System.out to see the effect (you probably have to save so the inspection is triggered again).

import java.util.List;
import java.util.ArrayList;

public class Test {

private boolean b = true;
private final List list = new ArrayList();

public Test() {}

public void execute() {
synchronized(list) {
System.out.println("b = " + b);
// System.out.println("b = " + isB());
}
}

public boolean isB() {
return b;
}
}

2 comments
Comment actions Permalink

This looks like a bug to me. You should file a JIRA issue.

urddd wrote:

I have a question about an inspection called 'Field accessed in both synchronized and unsynchronized contexts'

If in the synchronized block I access the boolean b directly, the inspection warns me that the field is accessed in both synchronized and unsynchronized contexts.
However, if I access the boolean b through the method isB(), the inspection don't flags anything.

isB() is public so it could be access by other objects.


What I would like to know is if the inspection is right not to put a warning when using the getter to access the boolean b. Accessing a value directly or throught a getter within a synchronized block isn't the same thing(just a cosmetic change)?


Below is a little example to illustrate this. Copy and paste this code and comment/uncomment System.out to see the effect (you probably have to save so the inspection is triggered again).

import java.util.List;
import java.util.ArrayList;

public class Test {

private boolean b = true;
private final List list = new ArrayList();

public Test() {}

public void execute() {
synchronized(list) {
System.out.println("b = " + b);
// System.out.println("b = " + isB());
}
}

public boolean isB() {
return b;
}
}

0
Comment actions Permalink

urddd wrote:

Below is a little example to illustrate this. Copy and paste this code and
comment/uncomment System.out to see the effect (you probably have to save so
the inspection is triggered again).


Inspection isn't normally triggered by saving. For me it's triggered
by any modification of the class. If modifying the text and waiting for the
inspection doesn't work, you should file another issue.

0

Please sign in to leave a comment.