How can I ignore a superclass? - java

I'm trying to write a web service for the java.util.logging api. So I wrote a class MyLogRecord that inherits from LogRecord. I annotated this class with JAX-B annotations, including #XmlAccessorType(XmlAccessType.NONE) so it would ignore non-annotated fields and properties. When I start up tomcat, I get errors that java.util.logging.Level and other java.util.logging classes do not have a default constructor, but none of my annotated methods make any reference to the Level class or any of the other java.util.logging classes. These are referenced by the parent class.
My sub-class has everything it needs defined. How can I get JAX-B to ignore the parent class completely?
Update: I found another post on this, which suggests modifying the parent class. This is obviously not possible because I am extending a java.util class. IS there any way to do this without modifying the superclass?
Update2: I found a thread on java.net for a similar problem. That thread resulted in an enhancement request, which was marked as a duplicate of another issue, which resulted in the #XmlTransient annotation. The comments on these bug reports lead me to believe this is impossible in the current spec.

You need to mark the parent class #XmlTransient. Since the parent class is in the JRE and cannot be modified by you, you need an alternate mechanism.
The EclipseLink JAXB (MOXy) implementation offers a means of representing the metadata as XML that you could use:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML
You can specify some of the metadata using annotations, and the rest as XML. Below is what your document would look like:
<java-types>
<java-type name="java.util.logging.LogRecord" xml-transient="true"/>
</java-types>

Related

Jackson - Overriding #JsonIgnore in MixIn

I have a property that that is #JsonIgnored on a class. And I wanted to make it available on some particular flow. I know that on a subclass, one can just use #JsonIgnored(false), and it overrides the base class annotation.
However, this does not seem to work via the MixIn, what works though is adding the #JsonProperty in the MixIn Interface/Abstract class. However, I am not too sure if this is safe and is consistent behavior.
Thanks.

How jaxb inject values during unmarshalling process

Does anybody know what jaxb uses to inject xml values into objects?
I have tried to put a breakpoint on the setters but it doesn't seem to take care of them.
By default JAXB (JSR-222) implementations will access public fields and properties (get/set) method pairs. This access is normally done via reflection (but isn't required to be). It could also be done via something like byte code generated accessors.
You can change it to access fields directly using #XmlAccessorType(XmlAccessType.FIELD).
JAXB apparently uses direct field access by default, but you can make it use Setter methods via the #XmlAccessorType annotation.
Read this blog post for more info:
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
Jaxb at some point uses reflections, which suprised me when I tried to use it in uncertified applet (restrictions on java sandbox in browser).
But I am sure it used setters in my case. I did some processing in setters(setting other internal variable at the same time) and it worked.

Is there a #NonNullByDefault annotation in IDEA?

Eclipse has the #NonNullByDefault annotation, which treats all values as #NonNull unless you explicitly annotate them as #Nullable.
Is there an equivalent option in IntelliJ IDEA, or do you have to always use #Nonnull?
Idea version 14 will include support for the JSR 305 "#TypeQualifierDefault" annotation, which allows the user to create a custom annotation, to be used on a package declaration in a package-info.java file, that specifies that everything in that package (not just parameters, but method return values, local variables, etc.) will be implicitly annotated as not allowing null values.
Unfortunately, this doesn't (currently) recursively affect subpackages, so each subpackage has to have a package-info.java file too, declaring that subpackage to use the annotation.
See here for details and an example of use:
http://youtrack.jetbrains.com/issue/IDEA-125281
Note that this is already implemented in Early Access Program (EAP) builds.
No, it is currently not supported by IDEA.
As a proof, see lena's link about the open feature request to allow 'NotNull' as the default element behavior for a given class or package.
Maybe a similar feature will be become standard with JSR-305, which may include the #ParametersAreNonnullByDefault annotation and also the opposite annotation #ParametersAreNullableByDefault. Note that in contrast to #NonNullByDefault, return values are not covered by those two annotations. So, you still had to annotate the return value explicitely.
All that doesn't change the current state, though. Neither has JSR-305 become a standard, nor does IDEA implement it.

JAXB can't handle Interfaces as parameter and return type

Theres a work around to have jaxb handle interface classes by annotating your service Impl class with:
#XmlSeeAlso ({SomeImplClass.java})
and annotating the interface file with:
#XmlRootElement
#XmlJavaTypeAdapter(AnyTypeAdapter.class)
however, this is a problem for me since the interface i'm using isn't editable and creating an Impl class for it is not doable since the interface has tons and tons of nested interfaces.
Is there a solution or workaround for this?
If you think from the JAXB perspective: It marshalls and unmarshalls the JAVA classes to XML, so there cant be any workaround to this.
As if I am JAXB and some one provided me an interface to marshall then I would have to guess which concrete implementation the user is referring to and it cannot do that. That's why I don't think it will be possible.

Adding a field to Java class

Looked at using CGLib, ASM, BCEL (aspect) and Javassist to add a field to a class during runtime....
Just to get my head straight it looks like these bytecode manipulators don't update the actual class rather allow the user to only dump the modification (like with CGLib and the writeFile method). Was hoping I would find a solution that (a) loaded the class (rather than doing an InputStream with BCEL) and (b) updated the class.
Maybe this is normal? Do people usually create a proxy and pass the proxy around?
What I want to do is to add a field (note: not a property via get/set methods) before passing the object along to a framework that looks for fields (not properties) with a particular annotation. So "clients" are creating my target classes that I want to inject with an extra field. Intercepting with AOP calls to a service layer where I want to manipulate these objects.
You can redefine classes with Intrumentation. However a common limiation is that you cannot change the fields used. This is because you cannot change the contents of a object (or add to it) once it has been created.
In your case you can,
create a proxy as you suggest, however proxies need to be interfaces.
create a subclass which has the additional field(s)
add the field before the class has loaded.

Categories

Resources