Q:

How to get the actual object of an XML abstract element in Java

I have the following XSD file:
msg.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
        attributeFormDefault="unqualified"
        elementFormDefault="qualified"
        targetNamespace="http://xxx.yy/zz/cb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:cb="http://xxx.yy/zz/cb">

    <xs:complexType name="message">
        <xs:sequence>
            <xs:element ref="cb:abstractGroup" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="baseType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="fullName" use="required">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="40" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="actualType1">
        <xs:simpleContent>
            <xs:extension base="cb:baseType">
                <xs:attribute name="title" use="required">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="40" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:complexType name="actualType2">
        <xs:simpleContent>
            <xs:extension base="cb:baseType">
                <xs:attribute name="subject" use="required">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="255" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:element type="cb:actualType1" name="actual1" substitutionGroup="cb:abstractGroup" />
    <xs:element type="cb:actualType2" name="actual2" substitutionGroup="cb:abstractGroup" />

    <xs:element name="abstractGroup" abstract="true" />

    <xs:element name="msg" type="cb:message" />
</xs:schema>

When I generate Java classes from this XSD in IntellJ IDEA using JAXB I get the following classes:

ActualType1
ActualType2
BaseType
Message
ObjectFactory

Message class looks like this:
Message.java
package messages;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlType;

/**
 * <p>Java class for message complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="message">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{http://xxx.yy/zz/cb}abstractGroup"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "message", namespace = "http://xxx.yy/zz/cb", propOrder = {
    "abstractGroup"
})
public class Message {

    @XmlElementRef(name = "abstractGroup", namespace = "http://xxx.yy/zz/cb", type = JAXBElement.class)
    protected JAXBElement<?> abstractGroup;

    /**
     * Gets the value of the abstractGroup property.
     * 
     * @return
     *     possible object is
     *     {@link JAXBElement }{@code <}{@link Object }{@code >}
     *     {@link JAXBElement }{@code <}{@link ActualType1 }{@code >}
     *     {@link JAXBElement }{@code <}{@link ActualType2 }{@code >}
     *     
     */
    public JAXBElement<?> getAbstractGroup() {
        return abstractGroup;
    }

    /**
     * Sets the value of the abstractGroup property.
     * 
     * @param value
     *     allowed object is
     *     {@link JAXBElement }{@code <}{@link Object }{@code >}
     *     {@link JAXBElement }{@code <}{@link ActualType1 }{@code >}
     *     {@link JAXBElement }{@code <}{@link ActualType2 }{@code >}
     *     
     */
    public void setAbstractGroup(JAXBElement<?> value) {
        this.abstractGroup = value;
    }

}

I have a sample msg.xml file:
msg.xml 
<cb:msg xmlns:cb="http://xxx.yy/zz/cb">
      <cb:actual1 fullName="bla bla" title="kuku">anyType</cb:actual1>
</cb:msg>

After unmarshalling it I want to get the real object representing the actual1 tag. What I can do is: message.getAbstractGroup() but that returns an instance of JAXBElement<?> which can be either JAXBElement<Object>, or JAXBElement<ActualType1> or JAXBElement<ActualType2>.
Now my questions is:
How can I get the real object of type ActualType1 or at least BaseType or how to transform JAXBElement<?> to ActualType1, ActualType2 or BaseType?

A:

The answer is something like:
BaseType type = (BaseType) message.getAbstractGroup().getValue();

if (type instanceof ActualType1) {
     System.out.println(((ActualType1) type).getTitle());
} else if (type instanceof ActualType2) {
     System.out.println(((ActualType2) type).getSubject());
}

