This is the mail archive of the java-discuss@sourceware.cygnus.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: initialization of final fields


Godmar Back <gback@cs.utah.edu> writes:

> My suggestion: until GCJ can prove that an assignment to a final 
> variable is indeed illegal, could you make the error in   
> expr.c:expand_java_field_op suppressable?

I think we can say that a method may assign to a final field
iff the method is private and is only called (directly or indirectly)
by the initializer.

You can verify this with two extra bits per method
METHOD_CALLED_BY_NON_INIT (the method is called directly or indirectly
by a non-private method wich is not "<init>" or <clinit>"),
and METHOD_SETS_FINAL_FIELD (the method sets a final field, or
it calls directly or indirectly a method which sets a final field).
These are mutually exclusive in correct code, and
only apply to private methods.

The tricky part is to set these during a single scan of the bytecode.
Something like this should work:

If method A sets a final field:
  if (METHOD_CALLED_BY_NON_INIT (A))  error(...);
  METHOD_SETS_FINAL_FIELD (A) = 1;

If method A calls method B:
  oldflag = METHOD_CALLED_BY_NON_INIT (B);
  if (METHOD_CALLED_BY_NON_INIT (A)
      || (A is not "<init>" && ! METHOD_PRIVATE (A)))
    METHOD_CALLED_BY_NON_INIT (B) = 1;
  if (METHOD_SETS_FINAL_FIELD (B))
     if (METHOD_CALLED_BY_NON_INIT (B) && ! oldflag) error(...);
    METHOD_SETS_FINAL_FIELD (A) = 1;

(Checking oldflag is only to avoid duplicate error messages.)
-- 
	--Per Bothner
per@bothner.com   http://www.bothner.com/~per/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]