|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT | ||||||||
@Documented
@Target(value={FIELD,PARAMETER})
public @interface AssumeFinal
Declares that the field or parameter to which this annotation is applied
should be treated as if it is declared final, despite the fact that
it is not.
It is a modeling error if the field or parameter to which this annotation is
applied is declared to be final.
This annotation is trusted, i.e., it is not verified by analysis.
Its only valid use is to annotate a field that is used as a lock where, for
some reason, the field can't be declared to be final. Perhaps due to
a (questionable) protocol forced upon the program by a library or framework.
Because this annotation is trusted it is not recommended for use. Changing
your code such that the reference can be declared to be final is
nearly always the best approach.
AssumeFinal to state this
policy.
@Region("private BadFinalRegion")
@RegionLock("BadFinalLock is lock protects BadFinalRegion")
public class BadFinal {
@AssumeFinal
private Object lock;
// Called only once at startup
public void setLock(Object value) {
lock = value;
}
...
@InRegion("BadFinalRegion")
private int x, y;
public void tick() {
synchronized (lock) {
x++;
y++;
}
}
...
}
An improved implementation would set the lock via the constructor and avoid
the assumption about the lock reference. In the example below, the BadFinal code is changed so that the lock field can be declared to be
final (and checked by the compiler).
@Region("private GoodFinalRegion")
@RegionLock("GoodFinalLock is lock protects GoodFinalRegion")
public class GoodFinal {
private final Object lock;
public GoodFinal(Object value) {
lock = value;
}
...
@InRegion("GoodFinalRegion")
private int x, y;
public void tick() {
synchronized (lock) {
x++;
y++;
}
}
...
}
@annotate tag.
public class BadFinal {
/**
* @annotate AssumeFinal
*/
private Object lock;
...
}
RegionLock
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT | ||||||||