This is the mail archive of the java-patches@sources.redhat.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: Patch: error->warning when assigning to final field


Mark Wielaard <mark@klomp.org> writes:

> It does violate JLS 4.5.4, 8.3.1.2, 15.14.1/2, 15.15.1/2, 15.26 and
> chapter 16 (see also the example in 6.5.6).

I'm sorry if my message was misleading.  Of course the compiler should
complain when compiling a .java source file to either native or .class.
(Javac does complain about this - at least I assume it does.)

However, this patch is for compiling .class files to source files.
The philosophy of Gcj is that the compiler should emit an error if it
sees something that violates the JVM spec.  In general, it should emit
an error if it sees something that would cause a Java verifier to
complain.  Any other valid bytecode must be accepted, even if it could
never be generated by javac.  Assigning to a final field outside an
initializer is neither prohibited by the JVM spec nor by Sun's
verifier.  Therefore, it must be allowed by Gcj.  However, emitting a
warning is reasonable, because such a .class file will not be portable.

So quoting from the JLS is irrelevant.

> It might also prevent some optimizations if final variables are suddenly
> not really final.

Not really, because the JVM spec *does* prohibit assigning to a final
field in classes other than defining class.  (This is checked for just
above the code I patched.)  So we can always determine if a final
variable "is not really final".
-- 
	--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]