Warnings
John Neffenger
john at status6.com
Sun Dec 4 19:53:26 UTC 2022
On 12/4/22 8:22 AM, John Hendrikx wrote:
> - Avoiding indirect access to static methods and fields
>
> class A { static final int ONE = 1; }
> class B { }
> class C extends B {
> int one = ONE; // unqualified access to A.ONE
> }
Did you mean the following?
class B extends A { }
There's a related trap in this category: inaccessible members can be
unintentionally made public by an intermediate subclass. For example:
package pkg1;
interface A { static final int ONE = 1; } // ONE has package access
package pkg1;
public class B implements A { } // ONE is now public in B
package pkg2;
import pkg1.B;
class C extends B {
int one = ONE; // ONE is accessible in pkg2.C
}
Through this chain, members with package access (not just constants) can
inadvertently end up in a public API. There are 423 occurrences of this
in the Java API. There's a fix in JDK 20 that should let us find them in
the JavaFX API, too. I don't think there's much we can do about it
except be aware of them.
I was really surprised by this, but see the classes in the 'pkg1' and
'pkg2' directories below for an example that you can compile and run:
https://github.com/openjdk/jdk/tree/master/test/langtools/jdk/javadoc/doclet/testIndexInherited
John
More information about the openjfx-dev
mailing list