• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ClassLoader question

 
Ranch Hand
Posts: 333
1
Mac Eclipse IDE Safari
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I my application I control both the access (security) and structure of the menus that users see when the logon to the application by storing everything in a database. Each JInternalFrame is assigned a logical tag/module_id and when the user logs on the menu only shows them which options they are allowed to see. That works fine. However I currenly have a class called MenuLauncher which logically consists of a list like :-

If (module_id = 'ABC')
{
// create a new instance of JABCInternalFrame etc.
}

If (module_id = '123')
{
// create a new instance of J123InternalFrame etc.
}

Some of my frames have constructures with parameters, some not.

I would like to extent my 'modules' database so that against each logical module name there is the actual class name. That would mean that the MenuLauncher class would be far more generic and not have hard coded references to each class that it could potentially need to load.

Can I achieve what I want by using the standard classloader and simply passing a string to my menulaucher with the class name in it ?

How do I manage those classes that I need to instantiate with a parameter ?

I've not found too many (any) basic clear examples on how this might be achieved so would be greatful for any advice or examples recieved.

Many thanks

Dave.
 
author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David, here's how you can load the bytecode for a class, then instantiate it, based on its name as a String.

Suppose that we have declared a Person class and various subclasses of Person -- Butcher, Baker, CandlestickMaker, and so forth. We want to determine at run time which type of Person to instantiate, by passing in (as a parameter, perhaps) the name of the Person subclass that we wish to instantiate.

Here's how we'd do it:


Ask whatever questions you need to ask for this to make sense ...

Regards,

Jacquie
 
David Garratt
Ranch Hand
Posts: 333
1
Mac Eclipse IDE Safari
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've not tried it yet, but I assume in my case "Person" would be JInternalFrame and "personSubtype" would be the actual name of one of my own internal frames ?

If I wanted to pass something to the constructor it would be in the form personSubtype.newInstance(myparam) I suppose.

Would I be able to use the routine as is if the app allowed more than one
instance of the same JInternalFrame to be loaded ?

Many thanks again

Dave.
 
David Garratt
Ranch Hand
Posts: 333
1
Mac Eclipse IDE Safari
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still a little stuck on how I would use this technique to instantiate an object that requires a parameter as part of it's constructor. If you can let me know how this would look (if it's possible) I would be very grateful.

Many thanks

Dave.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The basic outline: you ask the Class object for the constructors it supports (one of the getConstructors() methods.) Then you invoke the Constructor object using its newInstance() method (the name having been chosen to remind you of Class.newInstance(), I suppose, although I find this just confuses people.)

But give some very serious consideration to using the standard design pattern for this sort of thing: the classes must have a public no-arg constructor, to be used in Class.newInstance(), followed by a call to a standard initialization function defined in an interface the classes must support. For example, look at how the Servlet API is designed.
 
David Garratt
Ranch Hand
Posts: 333
1
Mac Eclipse IDE Safari
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my first try. basically this routine expects to be called with either 0 (null) or 1 parameters. It searches for a constructor which has a single parameter of a matching type and then invokes it. I'll probably only remember how it works until the morning !!

try
{
myInternameFrame = Class.forName(myClassName);

if (isLoaded(myInternameFrame))
setVisible(myInternameFrame);
else
{
if (Param != null)
{
Constructor[] theConstructors = myInternameFrame.getConstructors();
for (int i = 0; i < theConstructors.length; i++)
{
Class[] parameterTypes = theConstructors[i].getParameterTypes();

if (parameterTypes.length == 1)
{
String parameterString = parameterTypes[0].getName();
if (parameterString == Param.getClass().getName())
{
theConstructors[i].newInstance(Param);
p =(JInternalFrame) theConstructors[i].newInstance(Param);
displayForm(p,optionName);
}
}

}
}
else
{
p = (JInternalFrame) myInternameFrame.newInstance();
displayForm(p,optionName);
}

}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,e.getMessage(),"Form Load Error",JOptionPane.ERROR_MESSAGE);
}
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try a factory method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic