JDK-6607320 : Listeners/policies may cause loading of unneeded classes
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P5
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2007-09-20
  • Updated: 2021-07-13
Related Reports
Relates :  
Description
Some generic parts of Swing code heavily use instanceof which can requires classes to be resolved
and will trigger loading of tested classes. 

E.g. Notepad.jar loads JTable, JPasswordField, JTextField, JComboBox, JInternalFrame and some other unneeded classes. This mostly happens because of instanceof checks in the LayoutFocusTraversalPolicy.

This problem lead to increased size of minimal Swing core needed to run simple Swing applications/applets.
Total size of these (not needed) Swing class files is about 100k for Notepad.
Running 
  appletviewer -J-verbose:class demo\applets\ArcTest\example1.html 
shows that JComponent/JPanel are loaded.

This creates additional unneeded hard dependency between different subsystsems.
Also JComponent class is fairly large (over 40k).

We should avoid loading classes unless they are needed.

Comments
JDK-4354492 was reverted, which means we still need this fix. similar to JDK-6190052
06-02-2020

Description of this bug is strange, intanceof does not load classes since jdk1.4.0 JDK-4354492
06-02-2020

EVALUATION I woundn't make additional checks to fix that simple problem, I hope JVM guys can fix instanceof not to load unnecessary classes assinged to Swing performance team
21-09-2007

EVALUATION Loading happens because of the instanceof ckecks (see 6592631). Obviosly result of such instanceof will be false if classes were not loaded yet. Avoiding of loading is possible in following way: 1) Add new class to track initialization status of Swing (e.g. com.sun.swing.InitializationStatus) 1) Add boolean fields for every class in question (e.g. isJTableLoaded) 2) Add static blocks to each of classes we track to initialize these fields to true (e.g. com.sun.swing.InitializationStatus.isJTableLoaded=true) 3) rewrite instanceof conditions as if (com.sun.swing.InitializationStatus.isJTableLoaded && x instanceof JTable) Alternatively it is possible to use AWT approach (see Component.isInstanceOf()).
20-09-2007