简体   繁体   中英

ClassCastException when adding new POJO using javax.xml

I've created a new POJO class that uses javax.xml.bind.annotation.* to get its schema generated to an XSD file.

This is the class:

package <<my_package>>;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import <<MyOtherClass>>;

@XmlRootElement(name = "MyPojo", namespace="<<my_namespace>>")
@XmlAccessorType(XmlAccessType.FIELD)
public class MyPojo {
    @XmlElement(name = "MyOtherClass")
    private MyOtherClass myOtherClass;

    @XmlElement(name = "Message", required = true)
    private String message;

    public MyOtherClass getMyOtherClass() {
        return myOtherClass;
    }

    public void setMyOtherClass(MyOtherClass myOtherClass) {
        this.myOtherClass = myOtherClass;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String essage) {
        this.message = message;
    }
}

However, when trying to compile it with maven , I get the following error:

java.lang.ClassCastException: com.sun.tools.apt.mirror.type.ClassTypeImpl cannot be cast to com.sun.mirror.type.AnnotationType
    at com.sun.tools.apt.mirror.declaration.AnnotationMirrorImpl.getAnnotationType(AnnotationMirrorImpl.java:100)
    at com.sun.tools.jxc.apt.InlineAnnotationReaderImpl.getAllAnnotations(InlineAnnotationReaderImpl.java:113)
    at com.sun.tools.jxc.apt.InlineAnnotationReaderImpl.getAllMethodAnnotations(InlineAnnotationReaderImpl.java:102)
    at com.sun.tools.jxc.apt.InlineAnnotationReaderImpl.getAllMethodAnnotations(InlineAnnotationReaderImpl.java:64)
    at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.ensureNoAnnotation(ClassInfoImpl.java:1126)
    at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.collectGetterSetters(ClassInfoImpl.java:1039)
    at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.findGetterSetterProperties(ClassInfoImpl.java:939)
    at com.sun.xml.bind.v2.model.impl.ClassInfoImpl.getProperties(ClassInfoImpl.java:310)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:243)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:209)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:315)
    at com.sun.xml.bind.v2.model.impl.TypeRefImpl.calcRef(TypeRefImpl.java:92)
    at com.sun.xml.bind.v2.model.impl.TypeRefImpl.getTarget(TypeRefImpl.java:69)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:74)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:77)
    at java.util.AbstractList$Itr.next(AbstractList.java:358)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:255)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:209)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:315)
    at com.sun.xml.bind.v2.model.impl.TypeRefImpl.calcRef(TypeRefImpl.java:92)
    at com.sun.xml.bind.v2.model.impl.TypeRefImpl.getTarget(TypeRefImpl.java:69)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:74)
    at com.sun.xml.bind.v2.model.impl.ElementPropertyInfoImpl$1.get(ElementPropertyInfoImpl.java:77)
    at java.util.AbstractList$Itr.next(AbstractList.java:358)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:255)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getClassInfo(ModelBuilder.java:209)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:315)
    at com.sun.xml.bind.v2.model.impl.ModelBuilder.getTypeInfo(ModelBuilder.java:330)
    at com.sun.tools.xjc.api.impl.j2s.JavaCompilerImpl.bind(JavaCompilerImpl.java:90)
    at com.sun.tools.jxc.apt.SchemaGenerator$1.process(SchemaGenerator.java:115)
    at com.sun.mirror.apt.AnnotationProcessors$CompositeAnnotationProcessor.process(AnnotationProcessors.java:84)
    at com.sun.tools.apt.comp.Apt.main(Apt.java:480)
    at com.sun.tools.apt.main.AptJavaCompiler.compile(AptJavaCompiler.java:270)
    at com.sun.tools.apt.main.Main.compile(Main.java:1127)
    at com.sun.tools.apt.main.Main.compile(Main.java:989)
    at com.sun.tools.apt.Main.processing(Main.java:113)
    at com.sun.tools.apt.Main.process(Main.java:103)
    at com.sun.tools.apt.Main.process(Main.java:85)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.tools.jxc.AptBasedTask$InternalAptAdapter.execute(AptBasedTask.java:97)
    at com.sun.tools.jxc.AptBasedTask.compile(AptBasedTask.java:144)
    at com.sun.tools.jxc.maven2.SchemaGenAdapter.execute(SchemaGenAdapter.java:111)
    at com.sun.tools.jxc.maven2.SchemaGenMojo.execute(SchemaGenMojo.java:161)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Why am I getting this error and how can I fix it?

I figured it out after a while. In my POJO class I was referencing MyOtherClass , which had the following code:

package <<my_package>>;

import java.math.BigDecimal;

public class MyOtherClass {

    private int id;
    private BigDecimal quantity;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }
}

This file doesn't have XML annotations so Java didn't know how to generate its schema as part of the XSD . This was however required as my POJO needed to reference it in the XSD .


The fix is adding all the relevant XML annotations needed to generate the XSD schema for all referenced object. In my case, MyOtherClass became this:

package <<my_package>>;

import javax.xml.bind.annotation.*;
import java.math.BigDecimal;

@XmlType(name = "MyOtherClass", namespace = "<<my_namespace>>")
@XmlAccessorType(XmlAccessType.FIELD)
public class MyOtherClass {

    @XmlAttribute(name = "id")
    private int id;

    @XmlAttribute(name = "quantity")
    @XmlSchemaType(name = "decimal")
    private BigDecimal quantity;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM