about bug: 8020216: Compile time error isn't shown when final class variable is not assigned
Alex Buckley
alex.buckley at oracle.com
Wed Aug 7 17:59:13 PDT 2013
Each of the three final instance variables is not definitely assigned
after the if-then-else, but JLS 16 only requires a compile-time error
when such a variable is accessed. There's no access in the code below,
so why is an error being given?
Alex
On 8/1/2013 10:57 AM, Vicente-Arturo Romero-Zaldivar wrote:
> Hi Alex,
>
> Regarding the mentioned bug entry, I have a patch that solves the issue
> but then I have the following example taken from javac code:
>
> private final TreePath treePath;
> private final DocCommentTree docComment;
> private final DocTreePath parent;
>
> public DocTreePath(DocTreePath p, DocTree t) {
> if (t.getKind() == DocTree.Kind.DOC_COMMENT) {
> throw new IllegalArgumentException("Use
> DocTreePath(TreePath, DocCommentTree) to construct DocTreePath for a
> DocCommentTree.");
> } else {
> treePath = p.treePath;
> docComment = p.docComment;
> parent = p;
> }
> leaf = t;
> }
>
> with the patch javac says:
>
> error: variable treePath might not have been initialized
> error: variable docComment might not have been initialized
> error: variable parent might not have been initialized
>
> so this means that whenever we have such a code we will have to do:
>
> if (t.getKind() == DocTree.Kind.DOC_COMMENT) {
> treePath = null;
> docComment = null;
> parent = null;
> throw new IllegalArgumentException("Use
> DocTreePath(TreePath, DocCommentTree) to construct DocTreePath for a
> DocCommentTree.");
> } else {
>
> another option is to set the fields to null at the end of the method.
>
> Is this what we want? I think the current state of flow analysis is
> preferable but I wanted to double check before going on.
>
> Thanks,
> Vicente
More information about the compiler-dev
mailing list