How jaxb inject values during unmarshalling process - java

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.

Related

Is there a way to annotate the INSTANCE field of kotlin objects?

I have a Kotlin object that has several fields exposed as static #JvmFields. The parser that I use (which I cannot edit or change) looks for public static fields and creates a configuration file based on those. Since the INSTANCE field is public too, the parser generates a new category called instance. Is there a way to add actual annotations to the INSTANCE field? I would want to add the #Ingore annotation to it so the parser does not use the INSTANCE field.
Basically, the answer is no, Kotlin does not allow annotating or altering the INSTANCE fields in any other way. If you believe this could be a useful feature, please file a feature request at kotl.in/issue.
The valid solutions to this problem are:
Make the bytecode analyzing tool Kotlin-aware, i.e. make it behave correctly with Kotlin declarations. Though this requires non-trivial job to be done and does not seem possible in your case, it could be a valuable time investment.
Create another ad-hoc tool that post-processes the classes produced by the Kotlin compiler and adds the annotations you need, then include that tool into your build.

Changing names of properties while Serializing to JSON without source code

Need to serialize java objects to JSON while doing compression such as name change, exclusion etc. Objects use class from jar, source code of which is not available.
Looked through many libraries(Jackson , Gson), but found none solving this particular problem. Most of them are annotations based, which I can't use given I don't have source code.
One way to solve this problems is, use reflection and recursively go through object until you find a property name of which should be replaced or object is excluded in serialized JSON.
Need solution for this. Better if it is already implemented and tested.
You can also have a look at Genson library http://code.google.com/p/genson/.
You can rename and filter with quite concise code:
// renames all "fieldOfName" to "toName", excludes from serialization
// and deserialization fields named "fieldNamed" and declared in DefinedInClass
// and uses fields with all visibility (protected, private, etc)
Genson genson = new Genson.Builder().rename("fieldOfName", "toName")
.exclude("fieldNamed", DefinedInClass.class)
.setFieldFilter(VisibilityFilter.ALL)
.create();
genson.serialize(myObject);
If you want to do some more complex filtering (based on annotations for example) you can implement BeanMutatorAccessorResolver or extend BaseResolver.
Same for property renaming you can implement PropertyNameResolver and have full control.
And finally if you want to filter fields, methods or constructors according to their modifiers you can define your own VisiblityFilter.
Concerning performances of filtering/renaming there should be no problem as it is done only once per class and then cached.
To start using Genson you can have a look at the Getting Started Guide.
Found solution to the problem.
Google gson has class called GsonBuilder which has methods for exclusion strategy and naming strategy.
Using these two methods implemented a custom solution, where all the mapping and exclusion rules are stored using a xml and used at the time of serialization and de-serialization.
Works perfectly, though not sure about the performance of same.

What is the purpose of Dynamic Bean in ATG

I've read documentation, but there is no definition of the main purpose of Dynamic Bean. I understand how to implement this but dont know why this approach so good.
So could someone tell the situation when it's good to use Dynamic Bean?
Thanks
Dynamic beans typically allow you to get and set fields which may not be explicit members. The most direct comparison is a map - maps allow you to get and set fields without defining them beforehand. However, a dyanamic bean conforms to standard java idioms (getters/setters).
Unlike a hashmap, however, dyanbeans can enforce constraints more readily (and they hide the underlying data structure implementation, so they can be lazy, or make data connections when being set, etc... ) . For example, you can easily add a getter or setter to your dynabean that is explicit, and the code would read very idiomatically and cleanly interact with other bean apis.
public int getCost()
{
if(this.get("cost")==null)
return -1;
return Integer.parseInt(super.get("cost"));
}
The most useful part about dynamic beans in ATG is providing additional DynamicPropertyMapper classes for classes that aren't already covered by it. First, note that you can use the DynamicBeans.setPropertyValue(object, property, value) and DynamicBeans.getPropertyValue(object, property) static methods to set or get properties on an object that don't necessarily correspond with Java bean properties. If the object you're using isn't registered with dynamic beans, it'll try to use Java bean properties by default. Support is provided out of the box to do that with repository items (properties correspond to repository item properties; also applies to the Profile object, naturally), DynamoHttpServletRequest objects (correspond to servlet parameters), maps/dictionaries (correspond to keys), and DOM Node objects (correspond to element attributes followed by the getters/setters of Node).
To add more classes to this, you need to create classes that extend DynamicPropertyMapper. For instance, suppose you want to make HttpSession objects work similarly using attributes with a fallback to the getters and setters of HttpSession. Then you'd implement the three methods from DynamicPropertyMapper, and the getBeanInfo(object) class can be easily implemented using DynamicBeans.getBeanInfo(object) if you don't have any custom BeanInfo or DynamicBeanInfo classes for the object you're implementing this for.
Once you have a DynamicPropertyMapper, you can register it with DynamicBeans.registerPropertyMapper(mapper). Normally this would be put into a static initialization block for the class you're writing the property mapper for. However, if you're making a property mapper for another class out of your control (like HttpSession), you'll want to make a globally-scoped generic service that simply calls the register method in its doStartService(). Then you can add that service to your initial services.

JAXB cannot marshall and unmarshall Opensaml and Openws objects with non-arg constructors?

I am currently building my own custom Security Token Services using the Opensaml and Openws APIs.
I am using CXF with Spring, this technology facilitates interceptors that allow me to manipulate and insert elements (SAML Assertions etc) into the soap header.
However, I would like to insert an OpenWS RequestSecurityToken object into the soap body, via my web service method.
The OpenWS RequestSecurityToken object itself does no have a non-argument constructor defined, and neither does it's superclasses. Root class being org.w3c.xml.Element which also does not have an empty constructor.
JAXB throws an exception, complaining about this, saying it can't handle interfaces - even though these classes are not interfaces!
I do not have the source code and so am not able to add constructors easily. Much has been posted on the internet about this, with pointers to custom annotations, adapter classes and custom marshallers, but I cannot work out which approach to adopt.
BTW: OpenWS and Opensaml does come with Marshallers and Unmarshallers for each object. I was wondering if anyone has managed to force JAXB to use custom marshallers at all?
You can use an XmlAdapter, check out:
http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html
http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html

When Using Dynamic Proxies, how do I access the underlying object's Annotations?

When Using Dynamic Proxies, how do I access the underlying object's Annotations?
Specifically I'm annotating settings of a ORM object with #Column("client_id") and then making a Dynamic Proxy keep track of when the annotated setters are called, but...
It doesn't seem that the annotated proxy keeps any of the underlying annotations so short of performing reflection on every invocation, how do I make the proxy have the annotations of the class it's Proxying?
Thank you,
Allain
AFAIK, it depends on your bytecode injection lib. Also, remember that typically annotations are not inherited (imposed by the Java spec). If you want to access the original class, and are using CGLIB, you can use this snippet:
if (Enhancer.isEnhanced(getClass())) {
currClass = UnEnhancer.unenhance(getClass());
} else {
// else, let's get the original class directly
currClass = getClass();
}

Categories

Resources