I am trying to create an XMLAdapter
class for one of my objects. I need to access Getters
from another class so that some of the objects can be populated from that class's Getters` but I am unable to do so.
Basically, I would like to access my Child
class Getter
methods within the XMLAdapter
. I can create an Object of the Child
class and access but it results in NULL
. Initially, I am populating the Objects
in the Child
and Parent
class. I would like to access the same value within XML Adapter
so I am wondering if there is a way to pass reference of a class to my XMLAdapter
.
I have the following Parent
class which will have the values:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlTransient
@XmlSeeAlso({Child.class})
public class Parent{
private String eventID;
}
Following is my Child
class which will be used during marshalling to create XML
:
@XmlRootElement(name = "child")
@XmlType(name = "child", propOrder = { "name","extension"})
public class Child extends Parent
{
@XmlElement (name = "name")
private String name;
@JsonIgnore
@XmlJavaTypeAdapter (ExtensionAdapter.class)
@XmlElement (name = "extension")
private Extension extension;
}
Following is my Extension class:
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Extension {
private String eventID;
}
Following is my XMLAdapter
class:
public class ExtensionAdapter extends XmlAdapter<Child, Extension> {
@Override
public Extension unmarshal(Child valueType) throws Exception {
System.out.println("UN-MARSHALLING");
return null;
}
@Override
public Child marshal(Extension boundType) throws Exception {
System.out.println("MARSHALLING");
System.out.println(boundType);
//If I create the New child type then I am unable to access the eventID from its getter as Child class is nulll
Child be = new Child();
be.setEventID(boundType.getEventID());
return be;
}
}
Basically, I would like to access the Child
glass Getter
methods within the XMLAdapter
so that I can populate my Extension
class EventID
with that. I tried a lot of things but nothing worked so I am posting here.
Is there a way I can pass my Child
class as a parameter to XMLAdapter
so that I can access the Getter
methods from it? Because if I create a new Object of Child
class then all of its Getter
methods are null. However at the start I am populating all the fields in Parent
and Child
class (name and eventID). I just want to populate the same value within the Extension
class eventID
using the XMLAdapter
.
I hope I was able to explain the need. If you have any suggestions or sample code then it would be really useful.
Please Note: I have used very sample code here. I am aware that whatever I am trying to achieve can be achieved using the proper POJO and JAXB Annotations but my actual class is complex and I am not allowed to modify it. All I can add is the new Annotations. Hence, I am trying to achive this using the XMLAdapter
.
Adapter
class. – Ganny