Weird Question Regarding JVM and Packages

Sean Carrick sean at pekinsoft.com
Mon Apr 26 04:05:49 UTC 2021


Greetings, All!

I have an old project that I maintain, which uses an ancient library.
The library it uses does an excellent job for the project, but lacks one
thing: the ability to save session state of JInternalFrame windows. I
have figured a fix for this, but, I am using a plugin for my IDE that
relies on the original library. So, let me explain the situation and
then ask the question...

The library in question provides for application life-cycle and session
state saving/restoring. It works great for top-level windows, such as
JFrame and JDialog windows. It also is able to save/restore session
state for items such as JSplitPane, JTabbedPane, and JTable controls
within the windows. However, my project uses the JDesktopPane and
JInternalPane windows, and I have clients asking (practically begging)
for me to get the project to save the state of the JInternalPanes.

The library that I am using is completely dead: there has not been any
active maintenance on it in over a decade. The project that is using it
does not generate enough revenue to justify the work that would be
involved by moving the project to a newer framework. But, I have figured
out a solution to my issue, though I do not know if it will work as I
think it will...

My project has its packages, such as com.mycompany.project, with many
sub-packages below that. The library has its packages, such as
com.library.lib. My question involves how the JVM parses out this
information. The reason I am curious about this is because the solution
that I discovered involves subclassing one of the old library classes.
However, that class has package private and private methods that I would
not be able to reach if I included my subclass within my own package
structure.

Therefore, I was thinking, "What's in a name?" I got to wondering, if I
placed my new version of the library class on the root of my source
tree, mimicking the original library's package structure, would I get
access to the original class' private methods and members, if I named my
class the same, but was just adding what is missing from the original class.

For example, if the original library class' absolute classname was
com.library.lib.SessionStorage, and I recreated that package structure
on the root of my project source folder: com.library.lib.SessionStorage,
but only added to it the missing requirements, would the JVM merge them
at runtime?

To further explain, let's say the original SessionStorage class has all
of the functionality I need, except for the ability to save the session
state of the JInternalFrames within my application, would I be able to
create a class called SessionStorage in a package named the same as the
original library's and only include the ability to get the session state
from the JInternalFrames, would this work.

Let's say that the original SessionStorage class contained the following
methods and classes:

  * public void save(Component root)
  * private void saveTree(List<Component> roots, Map<String, Object>
    stateMap)
  * public void restore(Component root)
  * private void restoreTree(List<Component> roots, Map<String, Object>
    stateMap)
  * public interface Property
  * public JSplitPaneProperty
  * public JSplitPaneState
  * public JTabbedPaneProperty
  * public JTabbedPaneState
  * public WindowProperty
  * public WindowState

Then, let's say that I create the package structure
'MyProject/src/com/library/lib/', then create the class
JInternalSessionState within that package, which contains only the
following:

  * public JInternalFrameProperty
  * public JInternalFrameState

Let's go one step further and say that my JInternalSessionState class
extends SessionState. Would what I believe would be the case actually
happen?

Would the JVM simply use my class as an overridden version of the
SessionState class, even though they are in completely different
projects, because their packages are named the same? For example, would
I be able to replace the private saveTree and restoreTree methods with
custom methods that would use the two new classes that I created,
without the need of overriding the two public methods, save and restore?

I hope that I have explained my thought process well enough for someone
to give me an answer, even if it is just laughter for thinking way too
far on this. I appreciate any responses that I may get, and thank you in
advance.

-SC



More information about the jdk-dev mailing list