Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Instantiation via Reflection

Thread view: 
Jason Cavett - 24 May 2007 15:18 GMT
I'm creating a system to parse various different files.  Each file has
a certain format, so I have different parsers to handle each format
which is determined at runtime.  Each parser is a subclass of
"FileParser."  Additionally, each parser has a single constructor that
accepts three pieces of information (all the same information).

I am also creating an XML config file that holds information on the
parsers that are important to the application (as some applications
want certain parsers and others want other types of parsers - this
makes it simple to switch out the config file and so the correct
parsers are used).

What I'm wondering is, how do I instantiate these classes via Java
reflection?  Normally when I use Java reflection, I have a default (no
arg) constructor.  In this case, I have to pass in some information
before I can instantiate.  Is this even possible?

I've searched for code examples online, but I can't find the
information I'm looking for.  Any help would be much appreciated.
Thomas Kellerer - 24 May 2007 15:26 GMT
> I'm creating a system to parse various different files.  Each file has
> a certain format, so I have different parsers to handle each format
[quoted text clipped - 15 lines]
> I've searched for code examples online, but I can't find the
> information I'm looking for.  Any help would be much appreciated.

To make live easier I would create a setter to provide the three values
in the FileParser base class. The instance variables could be made
protected so that the subclasses can access them.
Then create an instance via reflection (default constructor), cast that
to FileParser and call the setter to pass the three values.

But you can also get a constructor with a certain number of parameters:

Class cls = Class.forName("ConcreteFileParser");
Class[] types = new Class[] { FirstParameter.class,
SecondParameter.class, ThirdParm.class };
Constructor cons = cls.getConstructor(types);
Object[] args = new Object[] { value1, value2, value3 };
FileParser p = (FileParser)cons.newInstance(args);

(Not tested)

Regards
Thomas
Jason Cavett - 24 May 2007 16:13 GMT
On May 24, 10:26 am, Thomas Kellerer <JUAXQOSZF...@spammotel.com>
wrote:

> > I'm creating a system to parse various different files.  Each file has
> > a certain format, so I have different parsers to handle each format
[quoted text clipped - 37 lines]
>
> - Show quoted text -

Your second method works (after some tweaking, of course).  Thank you.

However, I had also been thinking about the first solution.  But, I'm
trying to think about it this way - I won't be working on this code
forever, and I want to ensure that those values will be populated by
the next developer.  (If they aren't, the FileParsing won't work.)

There's no way to ensure that those methods are called (or that the
variables are initialized) no matter what...is there?

Anyway, thank you for your help.  Much appreciated.
stefanomnn - 24 May 2007 16:17 GMT
HI!
in previous solution,
you could check value1,value2 and value3 != null;
il null you could throw a nullPointerException!

Stefano

Jason Cavett ha scritto:

> On May 24, 10:26 am, Thomas Kellerer <JUAXQOSZF...@spammotel.com>
> wrote:
[quoted text clipped - 52 lines]
>
> Anyway, thank you for your help.  Much appreciated.
Mike Schilling - 24 May 2007 18:45 GMT
> On May 24, 10:26 am, Thomas Kellerer <JUAXQOSZF...@spammotel.com>
> wrote:
[quoted text clipped - 50 lines]
> There's no way to ensure that those methods are called (or that the
> variables are initialized) no matter what...is there?

You could write a checkInitialized() method that throws an exception if they
haven't been initialized, and call it at the start of every public method.
But I think that doing construction of the object in the constructor is
clearer.
Steven Simpson - 24 May 2007 20:21 GMT
> Each parser is a subclass of
> "FileParser."  Additionally, each parser has a single constructor that
[quoted text clipped - 4 lines]
> before I can instantiate.  Is this even possible?
>  

Don't you want a factory for this?  Define (say):

interface FileParserFactory {
 FileParser createParser(int info1, char info2, String info3);
}

...plus various implementations for your various file formats.  Your XML
config file would now identify factory implementations, rather than
FileParser subclasses.

Identify the appropriate factory class for your file format, instantiate
it with Class.newInstance(), and pass the info to the method.  It then
calls the appropriate constructor without further reflection.

Signature

ss at comp dot lancs dot ac dot uk |



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.