Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

A lightweight extension to BeanUtils that uses annotations for better readability and less errors.

Notifications You must be signed in to change notification settings

puzzle/propertiescopymachine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PropertiesCopyMachine

This lightweight extension of Apache BeanUtils enables the user to copy desired properties from one bean to another. It is similar to dozer. Instead of using constant strings of included/excluded properties (in code or xml/config), the PropertiesCopyMachine uses annotations. It is also possible to define custom modes for different copy operations for the same bean.

Example

Let’s say you have to copy some properties of an item. This is a very fictitious example but it should explain what you can do with it.


... @PropertiesCopyConfigured public class Item { ... private String name; private String description; private Date releaseDate; ...

@PropertiesCopyIncluded public String getName(){ return this.name; } @PropertiesCopyIncluded public String getDescription(){ return this.description; } public Date getReleaseDate(){ return this.releaseDate; } … // setters

}


Given the item class with some of the getters annotated with PropertiesCopyIncluded I’d like to copy only the name and the description of the item.
You can now start copying those properties from one object to another like this:

... PropertiesCopyMachine propertiesCopyMachine = new PropertiesCopyMachineImpl(); propertiesCopyMachine.setProperties(targetItem, sourceItem); ...

The PropertiesCopyMachine will copy all the annotated properties of the item class. As you can see, the item class itself is annotated with PropertiesCopyConfigured. The PropertiesCopyMachine will throw an exception if you pass an unannotated class because it is not sure that you have thought through your copy mechanism. This annotation is for now not inheritable (could be changed though) because you should think about copying properties on each hierarchy level.

Example Continued

If you wanted to be able to use the item class in different copy processes, you simply have to do this:


// custom mode for copying items public interface CopyItemMode extends PropertiesCopyMode { }


// custom mode for new versions of items public interface NewVersionItemMode extends PropertiesCopyMode { }


... @PropertiesCopyConfigured public class Item { ...

@PropertiesCopyIncluded({ CopyItemMode.class, NewVersionItemMode.class }) public String getName(){ return this.name; } @PropertiesCopyIncluded(CopyItemMode.class) public String getDescription(){ return this.description; } public Date getReleaseDate(){ return this.releaseDate; } …

}


Editing the annotations like this will enable the user to the following:

... PropertiesCopyMachine propertiesCopyMachine = new PropertiesCopyMachineImpl(); // this will copy only the properties that are annotated with the CopyItemMode.class // in this case name and description propertiesCopyMachine.setProperties(targetItem, sourceItem, CopyItemMode.class); ...

and:

... PropertiesCopyMachine propertiesCopyMachine = new PropertiesCopyMachineImpl(); // this will copy only the properties that are annotated with the NewVersionItemMode.class // in this case only the name will be copied propertiesCopyMachine.setProperties(targetItem, sourceItem, NewVersionItemMode.class); ...

About

A lightweight extension to BeanUtils that uses annotations for better readability and less errors.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages