Forums Register Login

Dynamically declaring and initializing variables in Java

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Consider using a Map<String, Object>
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
Could you explain what your problem is with the method suggested? Do you have a strong reason not to follow it?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
today's feeble attempt to support the empire
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1518 times.
Similar Threads
shadowing variables
Array questions
Memory allcocation at time of loading class..
initialisation
Instance Variabe
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:06:53.