aspose file tools*
The moose likes Java in General and the fly likes Marshalling and unmarshallilng java Objects(castoring) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Marshalling and unmarshallilng java Objects(castoring)" Watch "Marshalling and unmarshallilng java Objects(castoring)" New topic
Author

Marshalling and unmarshallilng java Objects(castoring)

Ajay Xavier
Ranch Hand

Joined: Jan 03, 2005
Posts: 109
hi,

Can I castor(Marshal and unMarshal) an HashMap whose key is of type java.lang.reflect.Method and value is of type String . while castoring it throws an exception as

The marshaller cannot marshal/unmarshal types of Void.class, Class.class or Obje
ct.class


here is the code


Project.java

import java.util.HashMap;

public class Project
{
private HashMap aliasMap=new HashMap();

public void setAliasMap(Object method,Object name)
{
aliasMap.put(method,name);
}

public HashMap getAliasMap()
{
return aliasMap;
}

}




XMLWriter.java:

import java.io.*;
import org.exolab.castor.xml.*;
import java.util.*;
import java.lang.reflect.*;
public class XMLWriter
{
public static void main(String[] args)
{
Class loadedClass=null;
Project project=new Project();
try
{
loadedClass=Class.forName("com.ibze.framework.common.rat.core.ProjectStructure");
}
catch(Exception e)
{
e.printStackTrace();
}

Method[] loadingMethods= loadedClass.getMethods();

HashMap aliasMap=new HashMap();

for(int i=0;i<loadingMethods.length;i++)
{

project.setAliasMap(loadingMethods[i],loadingMethods[i].toString());

}


try {

// write it out as XML
File file = new File("test.xml");
Writer writer = new FileWriter(file);
Marshaller.marshal(project, writer);
System.out.println("marshalled");
// now restore the value and list what we get
Reader reader = new FileReader(file);
Project read = (Project)
Unmarshaller.unmarshal(Project.class, reader);
System.out.println(read.getAliasMap().size());
System.out.println("unmarshalled");

} catch (Exception ex) {
System.out.println("11111");
ex.printStackTrace(System.err);
}



}
}


is there any alternative for storing the hashmap in a permanent location say XML file.while retriveing i need the key of type java.lang.reflect.Method if so suggest me plz.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marshalling and unmarshallilng java Objects(castoring)