0

This statement throws IllegalAnnotationException. I would like to know why. I'm having a hard time finding the issue myself because I'm unfamiliar with JAXB and there is no information in the error thrown apart from the name.

JAXBContext context = JAXBContext.newInstance(Article.class);

Here's the Article class:

@XmlRootElement(name = "article")
@XmlAccessorType(XmlAccessType.FIELD)
public class Article {

    @XmlElement(name = "meta")
    private Metadata metadata;

    @XmlElement
    private Body body;

    @XmlElement(name = "tags")
    private Tags tags;

    public Metadata getMetadata() {
        return metadata;
    }

    public Body getBody() {
        return body;
    }

    public Tags getTags() {
        return tags;
    }

    public void setTags(Tags tags) {
        this.tags = tags;
    }

    public List<Paragraph> getParagraphs() {
        return body.getParagraphs();
    }
}

The Tag class:

@XmlAccessorType(XmlAccessType.FIELD)
public class Tags {

    @XmlElement(name = "headword")
    private List<Headword> headword;

    @XmlElement(name = "formParenthesis")
    private List<FormParenthesis> formParenthesis;

    @XmlElement(name = "etymology")
    private List<Etymology> etymology;

    @XmlElement(name = "overview")
    private List<Overview> overview;

    @XmlElement(name = "connections")
    private List<Connections> connections;

    @XmlElement(name = "compounds")
    private List<Compounds> compounds;

    @XmlElement(name = "derivations")
    private List<Derivations> derivations;

    @XmlElement(name = "reference")
    private List<Reference> reference;

    @XmlElementWrapper(name = "sources")
    @XmlElement(name = "source")
    private List<Source> sources;

    @XmlElementWrapper(name = "definitions")
    @XmlElement(name = "definition")
    private List<Definition> definitions;

    @XmlElementWrapper(name = "professions")
    @XmlElement(name = "profession")
    private List<Profession> professions;

    @XmlElementWrapper(name = "usabilities")
    @XmlElement(name = "usability")
    private List<Usability> usabilities;

    @XmlElementWrapper(name = "remarks")
    @XmlElement(name = "remark")
    private List<Commentary> commentaries;

    @XmlElementWrapper(name = "languageTests")
    @XmlElement(name = "languageTest")
    private List<LanguageTest> languageTests;

    public List<Headword> getHeadword() {
        return headword;
    }

    public List<FormParenthesis> getFormParenthesis() {
        return formParenthesis;
    }

    public List<Etymology> getEtymology() {
        return etymology;
    }

    public List<Overview> getOverview() {
        return overview;
    }

    public List<Connections> getConnections() {
        return connections;
    }

    public List<Compounds> getCompounds() {
        return compounds;
    }

    public List<Derivations> getDerivations() {
        return derivations;
    }

    public List<Reference> getReference() {
        return reference;
    }

    public List<Source> getSources() {
        return sources;
    }

    public void setSources(List<Source> sources) {
        this.sources = sources;
    }

    public boolean hasSources() {
        return sources != null;
    }

    public List<Definition> getDefinitions() {
        return definitions;
    }

    public List<Definition> setDefinitions(List<Definition> definitions) {
        this.definitions = definitions;
        return this.definitions;
    }

    public boolean hasDefinitions() {
        return definitions != null;
    }

    public List<Profession> getProfessions() {
        return professions;
    }

    public List<Profession> setProfessions(List<Profession> professions) {
        this.professions = professions;
        return this.professions;
    }

    public boolean hasProfessions() {
        return professions != null;
    }

    public List<Usability> getUsabilities() {
        return usabilities;
    }

    public List<Usability> setUsabilities(List<Usability> usabilities) {
        this.usabilities = usabilities;
        return this.usabilities;
    }

    public boolean hasUsabilities() {
        return usabilities != null;
    }

    public List<Commentary> getCommentaries() {
        return commentaries;
    }

    public List<Commentary> setCommentaries(List<Commentary> commentaries) {
        this.commentaries = commentaries;
        return this.commentaries;
    }

    public boolean hasCommentaries() {
        return commentaries != null;
    }

    public List<LanguageTest> getLanguageTests() {
        return languageTests;
    }

    public List<LanguageTest> setLanguageTests(List<LanguageTest> languageTests) {
        this.languageTests = languageTests;
        return this.languageTests;
    }

    public boolean hasLanguageTests() {
        return languageTests != null;
    }

}

What is Illegal about these annotations?

Please let me know if there is some other part of the code you would like to see.

UPDATE: I've identified three tags that cause the error. I ran with just one of them not commented out:

Tags.java:

@XmlElement(name = "derivations")
private List<Derivations> derivations;

and here is the stack trace:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:445)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:277)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
    at com.mycom.ap.processing.io.utils.ArticleSerializationHelper.fromFile(ArticleSerializationHelper.java:33)
    at com.mycom.ap.processing.ArticleProcessingWrapper.run(ArticleProcessingWrapper.java:34)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

Derivations.java:

public class Derivations extends PositionalIds {

    @XmlElement(name = "derivation")
    List<Derivation> derivations;

    public List<Derivation> getDerivations() {
        return derivations;
    }
}

PositionalIds.java

@XmlTransient
public abstract class PositionalIds {

    @XmlAttribute(name = "start")
    private String startId;

    @XmlAttribute(name = "end")
    private String endId;

    public String getStartId() {
        return startId;
    }

    public String getEndId() {
        return endId;
    }
}

UDPATE:

Changing Derivations.java to:

@XmlAccessorType(XmlAccessType.FIELD)
public class Derivations extends PositionalIds {

    @XmlElement(name = "derivation")
    List<Derivation> derivations;

    public List<Derivation> getDerivations() {
        return derivations;
    }
}

does not solve the problem. The same error remains.

Sahand
  • 7,980
  • 23
  • 69
  • 137
  • I can't test you code at the moment, but... if you don; t understand which an annotation causes the exception, you can try to comment annotations one by one (or doping a binary partiion ;-) ) in order to find the problematic one... – Pietro Martinelli Feb 21 '19 at 16:27
  • Possible duplicate of [I can't understand why this JAXB IllegalAnnotationException is thrown](https://stackoverflow.com/questions/10795793/i-cant-understand-why-this-jaxb-illegalannotationexception-is-thrown) – Lakshan Feb 21 '19 at 16:36
  • Maybe you want to share the actual exception trace? – pirho Feb 21 '19 at 16:43
  • @PietroMartinelli, I've identified the three annotations that give the error. They're given in an edit. – Sahand Feb 21 '19 at 17:15
  • @pirho, I've shared the stack trace in an edit – Sahand Feb 21 '19 at 17:15
  • Show yet the `Derivations` class – pirho Feb 21 '19 at 17:23

1 Answers1

0

Make sure also your Derivations (and other possible similar classes) has:

@XmlAccessorType(XmlAccessType.FIELD)

Otherwise you will show JAXB same field two times because you also have getter.

pirho
  • 11,565
  • 12
  • 43
  • 70
  • Thanks for your answer. Unfortunately it doesn't work. I've shown exactly what I did in an update. – Sahand Feb 21 '19 at 17:38
  • @Sahand so you checked also your `Derivation` class? Also your `Derivations/Derivation` realtionship is a bit odd. Usually you use `@XmlElementWrpper` or so. – pirho Feb 21 '19 at 17:50