GithubHelp home page GithubHelp logo

csckcac / mapstruct-metadata-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tobsef/mapstruct-metadata-example

0.0 0.0 0.0 12 KB

Example setup that uses ASM to provide MapStruct mapping info on runtime

Java 100.00%

mapstruct-metadata-example's Introduction

MapStruct Meta Data Example

BuildTool Java-1.8 MapStruct

Example setup that provides MapStruct mapping info on runtime.

What's the target field of a mapped source field?

Why?

Because we use MapStruct for mappings between a business-model and our ui-model. When a UI client want's to get sorted data, it can specify a field from the ui-model. Our MapStructParser can get the corresponding business-model field-name and create the needed Criteria to sort it.

Example

class BusinessModel{
   String zip; 
}

class UiModel{
   String plz; 
}

public interface ModelMapping extends BridgeMapping<BusinessModel, UiModel> {
   @Mapping(source = "zip", target = "plz")
   UiModel modelToUiModel(BusinessModel model, @MappingTarget UiModel uiModel);
}

@Test
public testMappingInfo(){
   MapStructParser mappingInfo = new MapStructParser();
   mappingInfo.parseMappingInterface(ModelMapping.class);
   assertEquals("zip", mappingInfo.mapToTargetField("plz"));
}

The mappingInfo.mapToTargetField("plz") returns the mapped field of if the BusinessModel (zip).

๐Ÿ‘‰ Full example: tfr.example.mapstruct.asm.MapStructParserTest

How?

The Mapping annotation from MapStruct uses RetentionPolicy.CLASS, so it's not possible to access it with reflections. This is why we use ASM (bytecode manipulation and analysis framework) to make them available at runtime.
The AnnotationParser is a general purpose annotation parser which provide a list of MethodAnnotationInfo.
The MapStructParser uses the AnnotationParser model to build a MapStructMappingInfo by collecting the Mapping-Annotations.

mapstruct-metadata-example's People

Contributors

tobsef avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.