Confusion over UnderInitialization

243 views
Skip to first unread message

Jon Schewe

unread,
Sep 26, 2020, 5:12:16 PM9/26/20
to Checker Framework Discussions, Michael Ernst
I'm having some issues understanding the errors about initialization. I've read over the section in the manual a couple of times and I thought I had it figured out, but the compiler doesn't agree with me.

I have the following class:

public class DefaultBean implements Bean, Serializable {
  /**
   * Base constructor. Source is {@code this}.
   */
  public DefaultBean() {
    this.propertyListener = new PropertyChangeSupport(this);
}
  private final PropertyChangeSupport propertyListener;
}

I understand that "this" is under initialization when it's passed to the constructor for PropertyChangeSupport. I believe that the appropriate annotation is @UnderInitialization(Object.class).

    @SafeEffect
    public PropertyChangeSupport(@UnderInitialization(Object.class) @PolyUI Object sourceBean) {
...
}


However I'm still getting errors. In addition to the specified class, I'm not clear on when to use ".class" and when not to in these annotations.

/home/jpschewe/projects/infra/working-dir/src/main/java/net/mtu/eggplant/util/DefaultBean.java:42: warning: [assignment.type.incompatible] incompatible types in assignment.
    this.propertyListener = new PropertyChangeSupport(this);
                            ^
  found   : @UnderInitialization(java.beans.PropertyChangeSupport.class) @NonNull PropertyChangeSupport
  required: @Initialized @NonNull PropertyChangeSupport
/home/jpschewe/projects/infra/working-dir/src/main/java/net/mtu/eggplant/util/DefaultBean.java:42: warning: [argument.type.incompatible] incompatible argument for parameter sourceBean of <init>.
    this.propertyListener = new PropertyChangeSupport(this);
                                                      ^
  found   : @UnderInitialization @NonNull DefaultBean
  required: @Initialized @NonNull Object


Thanks,
Jon

Michael Ernst

unread,
Sep 27, 2020, 10:09:03 PM9/27/20
to Jon Schewe, Checker Framework Discussions
Jon-

Please see section 3.8.6, "Initialization of circular data structures", in the Checker Framework manual.
If you write "@NotOnlyInitialized" on the propertyListener field, then the Nullness Checker will approve your code.
Below is a complete example.

Mike



import org.checkerframework.checker.initialization.qual.NotOnlyInitialized;
import org.checkerframework.checker.initialization.qual.UnknownInitialization;

class DefaultBean {
  @NotOnlyInitialized private final PropertyChangeSupport propertyListener;


  public DefaultBean() {
    this.propertyListener = new PropertyChangeSupport(this);
  }
}

class PropertyChangeSupport {
  public PropertyChangeSupport(@UnknownInitialization(Object.class) Object sourceBean) {}
}


Jon Schewe

unread,
Sep 27, 2020, 10:24:44 PM9/27/20
to Michael Ernst, Checker Framework Discussions
Mike,

Thanks for the pointer. However it doesn't appear to be working for me. I needed to add @NotOnlyInitialized to PropertyChangeListener.source as well.

public class DefaultBean implements Bean, Serializable {
  /**
   * Base constructor. Source is {@code this}.
   */
  public DefaultBean() {
    this.propertyListener = new PropertyChangeSupport(this);
  }
  private final @NotOnlyInitialized PropertyChangeSupport propertyListener;
}


@PolyUIType
public @UsesObjectEquals class PropertyChangeSupport implements Serializable {
 @SafeEffect
    public PropertyChangeSupport(@PolyUI @UnknownInitialization(Object.class) Object sourceBean) {
        if (sourceBean == null) {
            throw new NullPointerException();
        }
        source = sourceBean;
    }

    private @NotOnlyInitialized Object source;
}

Without @NotOnlyInitialized on PropertyChangeSupport.source I get the error below.


/home/jpschewe/projects/infra/working-dir/src/main/java/net/mtu/eggplant/util/DefaultBean.java:45: warning: [argument.type.incompatible] incompatible argument for parameter sourceBean of <init>.
    this.propertyListener = new PropertyChangeSupport(this);
                                                      ^
  found   : @UnderInitialization(java.lang.Object.class) @NonNull DefaultBean
  required: @Initialized @NonNull Object
Reply all
Reply to author
Forward
0 new messages