Java Bean Projection - java

Are there any libraries that easily allow projections of Java beans?
I have a bean written with getters and setters as per the Javabean convention, and at runtime in different places I want to take a fully-populated bean, and create a new instance with only a subset of its properties populated from the original.
Could something like QueryDSL be used for this? Jackson has views that could be used in the context of JSON serialization, but I was looking for a Java-to-Java solution.

Dozer is a bean-mapper that will copy properties using reflection. It can be configured to include only a subset of properties if you want.

Check out Apache Commons BeanUtils

Related

Is it possible to deserialize a yaml document with setters that are not void?

I am working on a project that has a fair amount of data objects that use a "fluent interface" or "method chaining" on their setters so all of the setters in each data object return this. I have looked around and found this question, but it is unclear to me whether this can be used with Yaml as the annotations specifically mention JSON, and also this seems to enable mapping to objects using an actual builder pattern, which is a little different. The project is currently using SnakeYaml, but that can be tossed away if some other lib like Jackson can do this.
It turns out that this is in fact possible. Jackson supports yaml and will work with builder style setters by default without any extra configuration.

Inject JCR Properties with colon in Sling Model

I'm working with sling models for a project in AEM. I have my sling model setup to inject values to variables. I can setup getters to retrieve properties from the adapted resource as long as the properties have names that don't break java syntax. (For example: title)
My problem is that there are properties I need that break java syntax (for instance jcr:title,etc). I know for the get servlet for image servlet uses underscores in place of periods (file named img.GET.java, class named img_GET) and was curious if there was some character(s) I can use that will be valid java syntax that the injection strategy recognizes to replace with colons.
I know that I can retrieve these properties in other ways but I was wondering if there is a way to retrieve them using injection to keep my code dry.
Check out the following blog post:
http://labs.sixdimensions.com/blog/2014-11-21/sling-models-for-fun-and-profit/
In this, Dan provides an example of injecting (ironically enough) the jcr:title property of a Resource. You can simply use the #Named annotation and give it the name of the property you wish to inject.

when jackson auto detect setter, can I force it to use the pascal case?

Currently when jackson object mapper auto detetc setter, it defaults to lower case. Can I change the mapping to Pascal case?
It is possible to create custom AnnotationIntrospector that can find setters/getters that use different naming convention. But there is no automatic way to define mapping between external (in JSON) names to bean properties, using other name-mangling schemes. At least not yet; adding support is planned for 1.8.
Until then, one needs to use #JsonProperty annotation, or override internal classes (aside from AnnotationIntrospector there are other extension points that would work, but that's more of discussion for mailing lists).
I'm not sure what "Pascal case" is but the answer is no. All Java object mappers have to comply to the Java Beans API which clearly defines the case for fields, setters and getters and how one name can be derived from another.

Tools for merging java beans

It's easy to merge two simple, flat java beans using introspection:
BeanInfo info = Introspector.getBeanInfo( ContactBean.class );
PropertyDescriptor pDescArr[] = info.getPropertyDescriptors();
for(PropertyDescriptor pDesc : pDescArr){
//copy properties and check for conflicts here
}
However, it gets a little more complicated when the properties contain nested beans, or collections. Is there a smart tool somewhere which will handle a deep merge of complex beans?
Some more specifics on just how I'd like the merge to work:
Given a collection of source beans, and an empty target bean, simple properties should be copied over from the source to the target, unless there's a conflict. If there's a conflict, the field should be left empty. If a property is of a collection type, the values of the source beans should be combined, excluding duplicates, and copied to the destination property. These rules should apply recursively to properties which are beans themselves.
Dozer or Smooks. Dozer is the winner if you just want bean merge. If you are looking for other usecases like csv to pojo etc then take a look at Smooks.
You can use apache common beanutils. There is no built in method to do what you are trying to do, but you can use the helper methods in there to achieve the same

JAXB is good until I need to do something complex. What are the alternatives?

JAXB works well until I need to do something like serialize beans for which I cannot modify the source. If the bean doesn't have a default constructor or if it refers to objects I want to mark transient then I'm stuck writing a separate bean which I can annotate and then manually copy the information over from the other bean.
For instance, I wanted to serialize exception objects, but found that the only way to do that was use a hack that required using com.sun.* classes.
So, what alternatives are there? What's the next most popular xml serializing api? It would be nice to be able to do things like:
Choose at serialization time whether to include certain fields in the result. (marking things transient when running the serializer).
Handle loops in the object graph by using references or something other than just dying.
Perhaps annotate an object so that in version 1 it serializes things in one way and in version 2 it serializes them in another. Then when serializing I just choose which version of the object ot serialize.
Have a way to generate XSDs from annotations on an object.
Basically I just want more flexibility than I currently have with JAXB.
Well the standard answer for wanting a uber configurable serialisation framework is xstream.
JAXB is a spec, so you can pick from different implementations. EclipseLink JAXB (MOXy) has extensions for what you are asking:
Externalized Metadata
Useful when dealing with classes for which you cannot annotate the source or to apply multiple mappings to an object model.
http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML
XPath Based Mapping
For true meet-in-the-middle OXM mapping:
http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html
http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/MOXyExtensions
JPA Compatibility
Including support for bi-directional relationships.
http://bdoughan.blogspot.com/2010/07/jpa-entities-to-xml-bidirectional.html
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA
Also look at JIBX. It's a good xml<->object mapper. My experience is though that if your objects have a somewhat funky relationships it's often easier to create a wrapper object that hides that complexity and then map that object with JIBX.
XStream is a popular XML serialisation library that claims to be able to serialize just about anyting, regardless of constructors or other problems (even deserialize final fields). Give it a try.
Requires no modifications to objects. Serializes internal fields, including private and final. Supports non-public and inner classes. Classes are not required to have default constructor.

Categories