Instantiation via Reflection

J

Jason Cavett

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.
 
T

Thomas Kellerer

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.

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
 
J

Jason Cavett

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.

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- Hide quoted text -

- 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.
 
S

stefanomnn

HI!
in previous solution,
you could check value1,value2 and value3 != null;
il null you could throw a nullPointerException!

Stefano


Jason Cavett ha scritto:
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.

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- Hide quoted text -

- 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.
 
M

Mike Schilling

Jason Cavett said:
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.

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- Hide quoted text -

- 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?

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.
 
S

Steven Simpson

Jason said:
Each parser is a subclass of
"FileParser." Additionally, each parser has a single constructor that
accepts three pieces of information (all the same information).
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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top