SHKR
asked on
Display contents of hetrogeneous objects in HashMap or DistributedMap.
I am trying to write a small util to display the contents of the objects in the HashMap/ DistributedMap. Please guide or provide some sample code to achieve this.
HashMap will have differnet types of objects and writing if then else instanceof operator code block will be cumbersome and need to make changes to the code for each class type added , I am looking to make this dynamic by configuring the class type and methodName and variable Names in XML files. I will add detail of the any new classes in the XML file.
HashMap will have differnet types of objects and writing if then else instanceof operator code block will be cumbersome and need to make changes to the code for each class type added , I am looking to make this dynamic by configuring the class type and methodName and variable Names in XML files. I will add detail of the any new classes in the XML file.
public String displayObjectData(Object obj){
StringBuffer outPutJsp = new StringBuffer() ;
String className = obj.getClass().getName();
ClassType classType = null ;
if(classMap.containsKey(className)){
classType = (ClassType)classMap.get(className);
outPutJsp.append("<table> <th> <td>cachInstanceName : ["+this.cachInstanceName +"]</td>" +
"<td>Object classType["+ className +"]"+
",Object value["+ obj.toString() +"]</td>");
/// this point we need create reference based on the class type and then cast and display the contents by calling the methods listed in classMap.
///stuck here.
return outPutJsp.toString();
}
Something like
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Map.Entry e = (Map.Entry)i.next();
buffer.append("<tr>");
buffer.append("<td class='key'>");
buffer.append(e.getKey().toString());
buffer.append("</td>");
buffer.append("<td class='value'>");
buffer.append(e.getValue().toString());
buffer.append("</td>");
buffer.append("</tr>");
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for giving me the details. I went through some of the methods in java.lang.reflect package , and found useful methods,
Now I am looking for some help on How to get the contents of the obj if it is an arrary with reference to objects.
Now I am looking for some help on How to get the contents of the obj if it is an arrary with reference to objects.
Below are the smaple code snippets:
public String displayObjectData(Object obj){
StringBuffer outPutJsp = new StringBuffer();
try{
Field[] fields = obj.getClass().getFields();
for(int i=0; i < fields.length ; i++){
fields[i].setAccessible(true);
Class<?> eClass = fields[i].getType();
// Ojbect is prmitive type i.e Integer , Long etc.
if(Integer.class.isInstance(obj)){
outPutJsp.append("<tr><td>"+fields[i].getName()+"</td><td>"+fields[i].getInt(obj)+"</td></tr>");
}
// do.....similar as above for all other primitive types
// if it is collection call again the display method on each inidividual object
else if(HashMap.class.isInstance(obj)){
outPutJsp.append("<tr><td>"+fields[i].getName()+"</td><td>"+fields[i].getShort(obj)+"</td></tr>");
HashMap map = (HashMap)obj ;
Iterator itr = map.keySet().iterator();
while(itr.hasNext()){
outPutJsp.append(displayObjectData(itr.next()));
}
}
else if(eClass.isArray()){
if (eClass == byte[].class)
outPutJsp.append((byte[])obj);
else if (eClass == short[].class)
outPutJsp.append(Arrays.toString((short[])obj));
else if (eClass == int[].class)
outPutJsp.append(Arrays.toString((int[]) obj));
else if (eClass == long[].class)
outPutJsp.append(Arrays.toString((long[]) obj));
else if (eClass == char[].class)
outPutJsp.append(Arrays.toString((char[]) obj));
else if (eClass == float[].class)
outPutJsp.append(Arrays.toString((float[]) obj));
else if (eClass == double[].class)
outPutJsp.append(Arrays.toString((double[]) obj));
else if (eClass == boolean[].class)
outPutJsp.append(Arrays.toString((boolean[]) obj));
else {
// obj is an array of object references , how can we cast and handle the objects.
//????????
}
}
You can do
else if(eClass.isArray()) {
String arrayElements = java.util.Arrays.toString(array);
}
same way u deal with the others
else {
outPutJsp.append(Arrays.to String((Ob ject[]) obj));
that will use the toString() method of each object.
if thats not enough then you'll need to loop through the elements and call displayObjectData() on each object
else {
outPutJsp.append(Arrays.to
that will use the toString() method of each object.
if thats not enough then you'll need to loop through the elements and call displayObjectData() on each object
ASKER
java.util.Arrays.toString( Object[] anArray) method returns the string repressentation of the contents of the "anArray" elements.
The above method return value is same as below and will not give the value of instance variables.
anArray[o].toString()+anAr ray[1].toS tring ........+anArray[anArray.l ength -1].toString
Using
Class<?> eClass = fields[i].getType(); we can find the type of the array at runtime.
but how to get reference to array , and display the contents of the Object members.
As java doesn't allow arrays casting, is there any alternative approach for displaying object contents by checking the array type at runtime.
The above method return value is same as below and will not give the value of instance variables.
anArray[o].toString()+anAr
Using
Class<?> eClass = fields[i].getType(); we can find the type of the array at runtime.
but how to get reference to array , and display the contents of the Object members.
As java doesn't allow arrays casting, is there any alternative approach for displaying object contents by checking the array type at runtime.
>>returns the string repressentation of the contents of the "anArray" elements.
Isn't that what you want?
Isn't that what you want?
It won't work if the element doesn't have a meaningful toString method, though, and it if doesn't you'll have to do things 'manually'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
http://java.sun.com/javase/6/docs/api/java/util/AbstractMap.html#entrySet()