/java/

Convert POJO to MAP. Java Custom Annotations Mapper Example.

2015-06-21 09:49:11

There is a plenty mapper library on the marker, but you can write your own base on custom @Annotations.

What we want to achieve

    Article article = new Article();
    SparrowMapper sparrowMapper = new SparrowMapper();
    Map

<string, object=""> stringObjectMap = sparrowMapper.asMap(article);

where stringObjectMap will be:

{
    TITLE=Lorem ipsum,
    TYPE:1,
    CATEGORY={
        NAME=CARs, 
        SUB_CATEGORY:{
            NAME=Black, 
            ARCHIVE=false
        }
    },
    SECTIONS=[
        {CONTENT=Lorem Ipsum, TITLE=Section 1},
        {CONTENT=Lorem Ipsum, TITLE=Section 2}, 
        {CONTENT=Lorem Ipsum, TITLE=Section 3}
    ]
}

How to do that ?

Article

package entities;

import lib.pl.btbw.mapper.annotation.BtListProperty;
import lib.pl.btbw.mapper.annotation.BtObjectProperty;
import lib.pl.btbw.mapper.annotation.BtStringProperty;

import java.util.ArrayList;
import java.util.List;

public class Article {

    private String title;

    private int type;

    private Category category;

    private List<section> sections = new ArrayList&lt;&gt;();

    public Article() {
    }

    @BtStringProperty(name = "TITLE")
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @BtStringProperty(name = "TYPE")
    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @BtObjectProperty(name = "CATEGORY")
    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    @BtListProperty(name = "SECTIONS")
    public List<section> getSections() {
        return sections;
    }

    public void setSections(List<section> sections) {
        this.sections = sections;
    }
}

other pojo object you can find on github

object to map

    List<section> sectionList = new ArrayList&lt;&gt;();
    sectionList.add(new Section("Section 1"));
    sectionList.add(new Section("Section 2"));
    sectionList.add(new Section("Section 3"));

    SubCategory subCategory = new SubCategory();
    subCategory.setName("Black");

    Category category = new Category();
    category.setName("CARs");
    category.setSubCategory(subCategory);

    Article article = new Article();
    article.setTitle("Lorem ipsum");
    article.setType(1);
    article.setCategory(category);
    article.setSections(sectionList);

annotation BtStringProperty

package lib.pl.btbw.mapper.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD})
public @interface BtStringProperty {
    String name();
}

annotation BtObjectProperty

package lib.pl.btbw.mapper.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD})
public @interface BtObjectProperty {
    String name();
}

annotation BtListProperty

package lib.pl.btbw.mapper.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD})
public @interface BtListProperty {
    String name();
}

SparrowMapper

package lib.pl.btbw.mapper;

import lib.pl.btbw.mapper.annotation.BtListProperty;
import lib.pl.btbw.mapper.annotation.BtObjectProperty;
import lib.pl.btbw.mapper.annotation.BtStringProperty;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SparrowMapper {

    public SparrowMapper() {

    }

    public Map<string, object=""> asMap(Object obj) {
        return objToMap(obj);
    }

    private Map<string, object=""> objToMap(Object obj) {

        Map<string, object=""> map = new HashMap&lt;&gt;();

        Class<? > aClass = obj.getClass();

        Method[] methods = aClass.getMethods();

        for (Method method : methods) {
            getStringProperty(obj, map, method);
            getObjectProperty(obj, map, method);
            getListProperty(obj, map, method);
        }

        return map;
    }

    private void getStringProperty(Object obj, Map<string, object=""> map, Method method) {

        BtStringProperty annotation = method.getAnnotation(BtStringProperty.class);

        if (annotation != null) {

            String key = annotation.name();
            Object value = methodInvoke(obj, method);

            map.put(key, value);
        }
    }

    private void getObjectProperty(Object obj, Map<string, object=""> map, Method method) {

        BtObjectProperty annotation = method.getAnnotation(BtObjectProperty.class);

        if (annotation != null) {

            Object subObj = methodInvoke(obj, method);
            if (subObj != null) {

                String name = annotation.name();
                Map<string, object=""> value = objToMap(subObj);

                map.put(name, value);
            }
        }

    }

    private void getListProperty(Object obj, Map<string, object=""> map, Method method) {

        BtListProperty annotation = method.getAnnotation(BtListProperty.class);

        if (annotation != null) {

            List subObj = (ArrayList) methodInvoke(obj, method);
            if (subObj != null) {

                String name = annotation.name();
                List values = new ArrayList();

                for (Object item : subObj) {
                    values.add(objToMap(item));
                }
                map.put(name, values);
            }
        }

    }

    private Object methodInvoke(Object obj, Method method) {
        try {
            return method.invoke(obj);
        } catch (IllegalAccessException | InvocationTargetException e) {
            return e.getMessage();
        }
    }

}