8306854: javac with '-source 7' should honor default methods in implemented Java 8 interfaces
Volker Simonis
volker.simonis at gmail.com
Wed Apr 26 12:54:48 UTC 2023
On Tue, Apr 25, 2023 at 9:23 PM Alex Buckley <alex.buckley at oracle.com> wrote:
>
> To put my earlier mail in concrete terms:
>
> On 4/25/2023 8:33 AM, Volker Simonis wrote:
> > Instead of relying on the *global* `-source` setting, I think
> > `firstUnimplementedAbstractImpl()` should rather rely on the class
> > file version of the corresponing class (i.e. `B` in this case) and if
> > that class file version is >= 8, it should consider its default method
> > implementations. I even think that the check for `allowDefaultMethods`
> > could be completely removed, because if the class dependency `B`
> > wasn't compiled before, it will be compiled from source and fail for
> > `-source 7` because it contains a default method. If on the other
> > hand, the class dependency `B` was already compiled to a class file
> > with `-source 8` it is OK to use it's default method.
>
> It's not OK to consider, use, or interpret a default method in B.class
> (version >= Java 8) when compiling C.java with `-source 7`.
>
> Arguably, javac with `-source 7` shouldn't even read class files whose
> version is > Java 7. Such class files will contain artifacts that have
> no meaning to a program written in the Java language specified by JLS7.
That sounds good in an ideal world, but it is not how it has worked
for more than a decade. javac from JDK N running with '-source M'
(with M < N) has always accepted class files up to the class file
version N.
I'm just asking how to pragmatically handle an apparently unspecified
conflict which arises when javac with `-source 7` is referring to
class files of version 8 which contain default methods. As we've seen,
the handling is already inconsistent in OpenJDK's javac implementation
itself (i.e. compiling C.java from my example fails while compiling
E.java succeeds) and differs even more between independent
implementations of javac like e.g. ecj.
What would be the downside of making the simple change I've proposed
and accept default methods in Java 8+ classes as implementations for
interface methods if compiling with '-source 7'? That change would
only impact code generated with '-source 7' anyway and apparently
other javac implementation already allow this without any issues since
quite some time. Or am I missing some major problem?
Thanks a lot for your comments,
Volker
>
> Alex
More information about the compiler-dev
mailing list