Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamically declaring and initializing variables in Java

 
kranthi chamarthi
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider using a Map<String, Object>
 
kranthi chamarthi
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kranthi chamarthi
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you explain what your problem is with the method suggested? Do you have a strong reason not to follow it?
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic