aspose file tools*
The moose likes Java in General and the fly likes Dynamically declaring and initializing variables in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Dynamically declaring and initializing variables in Java" Watch "Dynamically declaring and initializing variables in Java" New topic
Author

Dynamically declaring and initializing variables in Java

kranthi chamarthi
Ranch Hand

Joined: May 08, 2007
Posts: 95
Hi,

I am developing a small tool that helps in automation testing. There is a requirement like, to read from an xml file the variable name, variable data type and its value, then using these values declaring and initializing variable in my java class dynamically. Please suggest me ways of doing this. Can i accomplish this using Reflection api of Java. I tried googling it, but was not able to find any concrete examples for this.

Thanks,
Kranthi
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4522
    
    5

Consider using a Map<String, Object>


luck, db
There are no new questions, but there may be new answers.
kranthi chamarthi
Ranch Hand

Joined: May 08, 2007
Posts: 95
Hi Darryl,

Thanks for the reply, but is there any other way of doing this... I think this is a common requirement, especially in case of tools that have to read the values from some configuration files and do the initialization. If i am not wrong, even in struts and hibernate, they do this...In case of struts 1.3, it reads from struts-config.xml, any entries for beans if it encounters, it automatically creates the bean with all properties, Spring also have this capability i believe, just want to check how these frameworks accomplish this..

Regards,
Kranthi
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7014
    
  16

kranthi chamarthi wrote:Thanks for the reply, but is there any other way of doing this... I think this is a common requirement, especially in case of tools that have to read the values from some configuration files and do the initialization.

Not at all. Such files would probably be read in as Properties (see java.util.Properties). The requirement to initialize objects whose type is unknown until runtime is VERY rare; and actually goes against Java's strongly-typed ethos. Therefore, you might want to consider if Java is actually the right language to use.

If i am not wrong, even in struts and hibernate, they do this...In case of struts 1.3, it reads from struts-config.xml, any entries for beans if it encounters, it automatically creates the bean with all properties, Spring also have this capability i believe, just want to check how these frameworks accomplish this..

Then you're in for a LOT of study. Both tools were developed by teams of programmers over a long period of time.

My advice: Back up and explain WHAT you're trying to do, not HOW you want to do it. That way, we might be able to suggest alternatives that are simpler.

I suspect very strongly that you're over-engineering this right now; but if indeed you really must create dynamic objects from XML files, I suggest you look at established protocols like SOAP.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
kranthi chamarthi
Ranch Hand

Joined: May 08, 2007
Posts: 95
My requirement is, i have an input xml to my tool in format



In my java code, I should be able to get the OrderNumber, whose value is 123456 and substitute it as value for Order/@OrderNo in my InputXml for api1. I can have number of api whose input xml may require this value of OrderNumber. I should be able to substitute this value in all those input xml using my java code. I can try using HashMap as Daryl suggested, but i want to know, is there any other standard approach for doing this.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

Could you explain what your problem is with the method suggested? Do you have a strong reason not to follow it?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7014
    
  16

kranthi chamarthi wrote:My requirement is, i have an input xml to my tool in format

Questions:
1. Is this format based on any particular standard, or just one that you dreamed up?
2. Is there a related DTD? It seems to me that otherwise there's huge scope for someone to just bash in
any old rubbish they like, and you'll spend half your life trying to validate what they've handed you.
3. Have you researched any of the automation frameworks for something like JUnit? I'm quite sure there are tons.

I suspect strongly that
(a) You're over-engineering this (at least at this stage).
(b) You may be re-inventing the wheel - and furthermore, doing it in a proprietary way,
which may not help you down the road if you want to add extra features.

HIH

Winston

PS: Those enormous lines in your XML make your thread very hard to read. I suggest you read the
DontWriteLongLines page and edit your post accordingly. Thanks.
kranthi chamarthi
Ranch Hand

Joined: May 08, 2007
Posts: 95
Format is specific to tool that i am developing. I will be coding a XSD to validate xml that is mentioned in my post above. This tool that i am developing is for testing functionality of an ERP package. I am going ahead with whatever Darryl suggested above.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamically declaring and initializing variables in Java
 
Similar Threads
Instance Variabe
Memory allcocation at time of loading class..
initialisation
Array questions
shadowing variables