How to exclude Observable in XSD file generated by JAXB - java

I have an abstract class extending java.util.Observable. When i generate xsd using Jaxb, there appears a complex type observable in the xsd file.
#XmlAccessorType(XmlAccessType.NONE)
public abstract class MyClass extends Observable implements Runnable, Serializable
I would use #XmlTransient on super type if it was a class i have written. Don't know how to exclude Observable.
I tried creating java.util package with package-info.java class to annotate package, as it was mentioned in this post JAXB: #XmlTransient on third-party or external super class :
#javax.xml.bind.annotation.XmlAccessorType(javax.xml.bind.annotation.XmlAccessType.NONE)
package java.util;
Did not work. Also tried it with XMLTransient:
#javax.xml.bind.annotation.XmlTransient
package java.util;
Again no luck. Anyone has any solution for this ?

Related

Java: process annotation inherited from .class file

I have an annotation that is inherited and has class-retention:
#Target({ElementType.TYPE})
#Retention(RetentionPolicy.CLASS)
#Inherited
public #interface MyAnnotation {}
It is used like this:
#MyAnnotation
public class Parent {}
public class Child extends Parent {}
If these classes are within the same project, my annotation processor is executed on both classes because the annotation is inherited correctly.
If Parent is in a separate project the .jar of which is included in the classpath of the project of Child, it doesn't work. The annotation processor is not executed on Child.
Is this by design or is there a way to fix this?

QueryDSL JPAAnnotation Processor generate QModel for a single class

I have a class that's located in a package. In that package I have several JPAEntities. However all I want for the JPAAnnotationProcessor to do is to generate QModel for a single class. Not for all classes inside that package. Is that possible without moving the class to another package?
You can use #QueryExclude annotation on top of the classes for which you don't want QClass to be generated.
import com.mysema.query.annotations.QueryExclude
#Entity
#QueryExclude
public class MyEntity {}
Unfortunately it is not possible to solve the problem in package level atm. If you mark all package with #QueryExclude and single class with #QueryEntity, still the whole package will be excluded. Therefore you have to mark each class separately.

JAXB, abstract classes, and multi-module projects

Let's say I have a JAR xxx-core.jar with the following classes:
#XmlRootElement
#XmlSeeAlso({Imp1.class, Imp2.class})
public abstract class Abst {...}
#XmlRootElement
public class Imp1 extends Abst {...}
#XmlRootElement
public class Imp2 extends Abst {...}
public class Main {
#XmlElement
private Abst abst;
public static void load(File file) {
// unmarshal this
}
public void save(File file) {
// marshal this
}
}
So far, so good. Main can be marshalled and unmarshalled, and the correct implementation of Abst is used.
Now, what happens when somebody else comes along and creates another project xxx-extension.jar that uses xxx-core.jar, but contains the following class:
#XmlRootElement
public class ExtensionImp extends Abst {...}
and assigns an instance of this new implementation to Main's member variable? Since it's not explicitly given in the XmlSeeAlso annotation, how can I make sure that ExtensionImp will be correctly marshalled/unmarshalled? (I've played with the class list in JAXBContext.newInstance(), but that doesn't seem to solve the problem.)
The #XmlSeeAlso annotation is just a mechanism that lets your JAXB (JSR-222) impl know to look at other classes. Alternatively you could just include then in the classes used to bootstrap the JAXBContext. When you create your JAXBContext you just need to do:
JAXBContext jc = JAXBContext.newInstance(Abst.class, ExtensionImp.class);
UPDATE 1
I've played with the class list in JAXBContext.newInstance(), but that
doesn't seem to solve the problem.
This should definitely solve the problem, what happens when you do this.
UPDATE 2
I suspect your issue is due to the document you are unmarshalling and not how you are bootstrapping. The following should help.
Inheritance - xsi:type
With the way that you currently have your mappings, you need to ensure that your XML looks like the following to have an instance of Imp2 instantiated and populated into the abst field on the Main class.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<main>
<abst xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="imp2"/>
</main>
For more info see:
http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html
Inheritance Substitution Groups
If you would rather unmarshal an XML document like the following:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<main>
<imp2/>
</main>
Then you need to leverage the #XmlElementRef annotation.
import javax.xml.bind.annotation.*;
#XmlRootElement
public class Main {
#XmlElementRef
Abst abst;
}
For more info see:
http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html
I solved similar problem using #XmlAnyElement(lax=true). Try to put this annotation instead of #XmlElement into main class.

How to make my root jaxb generated class extend my own class

I want my xjc generated root class extend one of my class A, which is nothing but a wrapper around the root class. Can i do that without modifying the xsd file (how to force schema compiled classes to extend specific class outside schema)
You can define a super-root class (see http://jaxb.java.net/nonav/2.0.2/docs/vendorCustomizations.html). Every generated class (in that xjc run) will be extending that root class.
The required global binding can be declared in an external binding file instead of inside the xsd (see http://java.sun.com/webservices/docs/1.4/tutorial/doc/JAXBUsing4.html#wp148515). Then you can pass it to the xjc generator together with the xsd.

Registering XmlAdapter globally for any classes in different packages

I'm using CXF 2.4 with JAXB.
Could I have a global XmlAdapter for all instances of my owm class (e.g. LWDate)?
I wrote a class:
public class LWDateAdapter extends XmlAdapter<Date, LWDate>
Right now I have to add #XmlJavaTypeAdapter on each param, method or package that I plan to use with CXF. E.g.
#WebMethod void test (#WebParam(name="Birthdate") #XmlJavaTypeAdapter(LWDateAdapter.class) LWDate pBirthdate){}
I wish to ask CXF/JAXB always bind my class LWDate to java.util.Date is it possible?
UPDATE: #XmlJavaTypeAdapter works on a package level staring from version 2.4.4 according to that issue.
For your use case using the #XmlJavaTypeAdapter annotation at the package level is your best option. Below is a post where I use this strategy for the Joda-Time classes:
http://blog.bdoughan.com/2011/05/jaxb-and-joda-time-dates-and-times.html
If you have a domain class that you always want to be handled with an XmlAdapter you can use the #XmlJavaTypeAdapter annotation at the type level:
http://blog.bdoughan.com/2010/12/jaxb-and-immutable-objects.html

Categories

Resources