com.surelogic
Annotation Type AssumeFinal


@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.

Examples:

The below example, which is a highly questionable protocol, sets the lock once after object construction and uses 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++;
     }
   }
   ...
 }
 

Javadoc usage notes:

This annotation may placed in Javadoc, which can be useful for Java 1.4 code which does not include language support for annotations, via the @annotate tag.
 public class BadFinal {
   /**
    * @annotate AssumeFinal
    */
   private Object lock;
   ...
 }
 

See Also:
RegionLock



Copyright © 2010 Surelogic, Inc.. All Rights Reserved.