mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#7 Using Hickory for dealing with MapStruct annotations
This commit is contained in:
parent
40fe92c597
commit
861d92dc64
@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<com.jolira.hickory.version>1.0.0</com.jolira.hickory.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
@ -49,7 +50,11 @@
|
|||||||
<artifactId>fest-assert</artifactId>
|
<artifactId>fest-assert</artifactId>
|
||||||
<version>1.4</version>
|
<version>1.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.jolira</groupId>
|
||||||
|
<artifactId>hickory</artifactId>
|
||||||
|
<version>${com.jolira.hickory.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.groupId}</groupId>
|
<groupId>${project.groupId}</groupId>
|
||||||
<artifactId>mapstruct</artifactId>
|
<artifactId>mapstruct</artifactId>
|
||||||
@ -70,6 +75,11 @@
|
|||||||
<target>1.6</target>
|
<target>1.6</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>build-helper-maven-plugin</artifactId>
|
||||||
|
<version>1.7</version>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-dependency-plugin</artifactId>
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
@ -85,6 +95,11 @@
|
|||||||
<artifactId>maven-license-plugin</artifactId>
|
<artifactId>maven-license-plugin</artifactId>
|
||||||
<version>1.9.0</version>
|
<version>1.9.0</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.bsc.maven</groupId>
|
||||||
|
<artifactId>maven-processor-plugin</artifactId>
|
||||||
|
<version>2.0.2</version>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</pluginManagement>
|
</pluginManagement>
|
||||||
|
|
||||||
|
@ -31,11 +31,20 @@
|
|||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<name>MapStruct Processor</name>
|
<name>MapStruct Processor</name>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<prism.directory>${project.build.directory}/generated-sources/prims</prism.directory>
|
||||||
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.freemarker</groupId>
|
<groupId>org.freemarker</groupId>
|
||||||
<artifactId>freemarker</artifactId>
|
<artifactId>freemarker</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.jolira</groupId>
|
||||||
|
<artifactId>hickory</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.testng</groupId>
|
<groupId>org.testng</groupId>
|
||||||
<artifactId>testng</artifactId>
|
<artifactId>testng</artifactId>
|
||||||
@ -49,7 +58,6 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>${project.groupId}</groupId>
|
<groupId>${project.groupId}</groupId>
|
||||||
<artifactId>mapstruct</artifactId>
|
<artifactId>mapstruct</artifactId>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
@ -80,6 +88,50 @@
|
|||||||
<compilerArgument>-proc:none</compilerArgument>
|
<compilerArgument>-proc:none</compilerArgument>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.bsc.maven</groupId>
|
||||||
|
<artifactId>maven-processor-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<defaultOutputDirectory>${prism.directory}</defaultOutputDirectory>
|
||||||
|
<processors>
|
||||||
|
<processor>net.java.dev.hickory.prism.internal.PrismGenerator</processor>
|
||||||
|
</processors>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>process</id>
|
||||||
|
<phase>generate-sources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>process</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.jolira</groupId>
|
||||||
|
<artifactId>hickory</artifactId>
|
||||||
|
<version>${com.jolira.hickory.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>build-helper-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>add-source</id>
|
||||||
|
<phase>generate-sources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>add-source</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<sources>
|
||||||
|
<source>${prism.directory}</source>
|
||||||
|
</sources>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
@ -23,21 +23,15 @@ import java.util.HashSet;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.annotation.processing.ProcessingEnvironment;
|
import javax.annotation.processing.ProcessingEnvironment;
|
||||||
import javax.lang.model.element.AnnotationMirror;
|
|
||||||
import javax.lang.model.element.AnnotationValue;
|
|
||||||
import javax.lang.model.element.Element;
|
import javax.lang.model.element.Element;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
import javax.lang.model.element.TypeElement;
|
import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.element.VariableElement;
|
import javax.lang.model.element.VariableElement;
|
||||||
import javax.lang.model.type.DeclaredType;
|
|
||||||
import javax.lang.model.type.TypeKind;
|
import javax.lang.model.type.TypeKind;
|
||||||
import javax.lang.model.type.TypeMirror;
|
|
||||||
import javax.lang.model.util.ElementKindVisitor6;
|
import javax.lang.model.util.ElementKindVisitor6;
|
||||||
import javax.lang.model.util.Elements;
|
import javax.lang.model.util.Elements;
|
||||||
import javax.lang.model.util.SimpleAnnotationValueVisitor6;
|
|
||||||
import javax.lang.model.util.Types;
|
import javax.lang.model.util.Types;
|
||||||
import javax.tools.JavaFileObject;
|
import javax.tools.JavaFileObject;
|
||||||
|
|
||||||
@ -61,9 +55,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
|
|
||||||
private final static String IMPLEMENTATION_SUFFIX = "Impl";
|
private final static String IMPLEMENTATION_SUFFIX = "Impl";
|
||||||
|
|
||||||
private final static String MAPPING_ANNOTATION = "org.mapstruct.Mapping";
|
|
||||||
private final static String MAPPINGS_ANNOTATION = "org.mapstruct.Mappings";
|
|
||||||
|
|
||||||
private final ProcessingEnvironment processingEnvironment;
|
private final ProcessingEnvironment processingEnvironment;
|
||||||
private final Types typeUtils;
|
private final Types typeUtils;
|
||||||
private final Elements elementUtils;
|
private final Elements elementUtils;
|
||||||
@ -263,22 +254,17 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<MappedProperty> retrieveMappedProperties(ExecutableElement method) {
|
private List<MappedProperty> retrieveMappedProperties(ExecutableElement method) {
|
||||||
|
|
||||||
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
|
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
|
||||||
|
|
||||||
for ( AnnotationMirror annotationMirror : method.getAnnotationMirrors() ) {
|
MappingPrism mappingAnnotation = MappingPrism.getInstanceOn( method );
|
||||||
|
MappingsPrism mappingsAnnotation = MappingsPrism.getInstanceOn( method );
|
||||||
|
|
||||||
String annotationName = annotationMirror.getAnnotationType()
|
if ( mappingAnnotation != null ) {
|
||||||
.asElement()
|
mappings.put( mappingAnnotation.source(), getMapping( mappingAnnotation ) );
|
||||||
.accept( new NameDeterminationVisitor(), null );
|
}
|
||||||
|
|
||||||
if ( MAPPING_ANNOTATION.equals( annotationName ) ) {
|
if ( mappingsAnnotation != null ) {
|
||||||
Mapping mapping = getMapping( annotationMirror );
|
mappings.putAll( getMappings( mappingsAnnotation ) );
|
||||||
mappings.put( mapping.getSourceName(), mapping );
|
|
||||||
}
|
|
||||||
else if ( MAPPINGS_ANNOTATION.equals( annotationName ) ) {
|
|
||||||
mappings.putAll( getMappings( annotationMirror ) );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return getMappedProperties( method, mappings );
|
return getMappedProperties( method, mappings );
|
||||||
@ -323,35 +309,23 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Mapping> getMappings(AnnotationMirror annotationMirror) {
|
private Map<String, Mapping> getMappings(MappingsPrism mappingsAnnotation) {
|
||||||
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
|
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
|
||||||
|
|
||||||
List<? extends AnnotationValue> values = getAnnotationValueListValue( annotationMirror, "value" );
|
for ( MappingPrism mapping : mappingsAnnotation.value() ) {
|
||||||
|
mappings.put( mapping.source(), getMapping( mapping ) );
|
||||||
for ( AnnotationValue oneAnnotationValue : values ) {
|
|
||||||
AnnotationMirror oneAnnotation = oneAnnotationValue.accept(
|
|
||||||
new AnnotationRetrievingVisitor(),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
Mapping mapping = getMapping( oneAnnotation );
|
|
||||||
mappings.put( mapping.getSourceName(), mapping );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mappings;
|
return mappings;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mapping getMapping(AnnotationMirror annotationMirror) {
|
private Mapping getMapping(MappingPrism mapping) {
|
||||||
String sourcePropertyName = getStringValue( annotationMirror, "source" );
|
Type converterType = typeUtil.retrieveType( mapping.converter() );
|
||||||
String targetPropertyName = getStringValue( annotationMirror, "target" );
|
return new Mapping(
|
||||||
TypeMirror converterTypeMirror = getTypeMirrorValue( annotationMirror, "converter" );
|
mapping.source(),
|
||||||
|
mapping.target(),
|
||||||
Type converterType = null;
|
converterType.getName().equals( "NoOpConverter" ) ? null : converterType
|
||||||
|
);
|
||||||
if ( converterTypeMirror != null ) {
|
|
||||||
converterType = typeUtil.getType( (DeclaredType) converterTypeMirror );
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Mapping( sourcePropertyName, targetPropertyName, converterType );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Parameter retrieveParameter(ExecutableElement method) {
|
private Parameter retrieveParameter(ExecutableElement method) {
|
||||||
@ -374,42 +348,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
return typeUtil.retrieveType( method.getReturnType() );
|
return typeUtil.retrieveType( method.getReturnType() );
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getStringValue(AnnotationMirror annotationMirror, String attributeName) {
|
|
||||||
for ( Entry<? extends ExecutableElement, ? extends AnnotationValue> oneAttribute : annotationMirror.getElementValues()
|
|
||||||
.entrySet() ) {
|
|
||||||
|
|
||||||
if ( oneAttribute.getKey().getSimpleName().contentEquals( attributeName ) ) {
|
|
||||||
return oneAttribute.getValue().accept( new StringValueRetrievingVisitor(), null );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private TypeMirror getTypeMirrorValue(AnnotationMirror annotationMirror, String attributeName) {
|
|
||||||
for ( Entry<? extends ExecutableElement, ? extends AnnotationValue> oneAttribute : annotationMirror.getElementValues()
|
|
||||||
.entrySet() ) {
|
|
||||||
|
|
||||||
if ( oneAttribute.getKey().getSimpleName().contentEquals( attributeName ) ) {
|
|
||||||
return oneAttribute.getValue().accept( new TypeRetrievingVisitor(), null );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<? extends AnnotationValue> getAnnotationValueListValue(AnnotationMirror annotationMirror, String attributeName) {
|
|
||||||
for ( Entry<? extends ExecutableElement, ? extends AnnotationValue> oneAttribute : annotationMirror.getElementValues()
|
|
||||||
.entrySet() ) {
|
|
||||||
|
|
||||||
if ( oneAttribute.getKey().getSimpleName().contentEquals( "value" ) ) {
|
|
||||||
return oneAttribute.getValue().accept( new AnnotationValueListRetrievingVisitor(), null );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<ExecutableElement> getterMethodsIn(Iterable<? extends Element> elements) {
|
private List<ExecutableElement> getterMethodsIn(Iterable<? extends Element> elements) {
|
||||||
List<ExecutableElement> getterMethods = new LinkedList<ExecutableElement>();
|
List<ExecutableElement> getterMethods = new LinkedList<ExecutableElement>();
|
||||||
|
|
||||||
@ -441,46 +379,4 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
|
|
||||||
return setterMethods;
|
return setterMethods;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class NameDeterminationVisitor extends ElementKindVisitor6<String, Void> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String visitType(TypeElement element, Void p) {
|
|
||||||
return element.getQualifiedName().toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class StringValueRetrievingVisitor extends SimpleAnnotationValueVisitor6<String, Void> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String visitString(String value, Void p) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class TypeRetrievingVisitor
|
|
||||||
extends SimpleAnnotationValueVisitor6<TypeMirror, Void> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeMirror visitType(TypeMirror value, Void p) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class AnnotationValueListRetrievingVisitor
|
|
||||||
extends SimpleAnnotationValueVisitor6<List<? extends AnnotationValue>, Void> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<? extends AnnotationValue> visitArray(List<? extends AnnotationValue> value, Void p) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class AnnotationRetrievingVisitor extends SimpleAnnotationValueVisitor6<AnnotationMirror, Void> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AnnotationMirror visitAnnotation(AnnotationMirror value, Void p) {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,18 @@ import javax.lang.model.element.Element;
|
|||||||
import javax.lang.model.element.ElementKind;
|
import javax.lang.model.element.ElementKind;
|
||||||
import javax.lang.model.element.TypeElement;
|
import javax.lang.model.element.TypeElement;
|
||||||
|
|
||||||
|
import net.java.dev.hickory.prism.GeneratePrism;
|
||||||
|
import net.java.dev.hickory.prism.GeneratePrisms;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Mappings;
|
||||||
|
|
||||||
@SupportedAnnotationTypes("org.mapstruct.Mapper")
|
@SupportedAnnotationTypes("org.mapstruct.Mapper")
|
||||||
|
@GeneratePrisms({
|
||||||
|
@GeneratePrism(value = Mapper.class),
|
||||||
|
@GeneratePrism(value = Mapping.class),
|
||||||
|
@GeneratePrism(value = Mappings.class)
|
||||||
|
})
|
||||||
public class MappingProcessor extends AbstractProcessor {
|
public class MappingProcessor extends AbstractProcessor {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -51,7 +51,10 @@ public class TypeUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Type retrieveType(TypeMirror mirror) {
|
public Type retrieveType(TypeMirror mirror) {
|
||||||
if ( mirror.getKind() == TypeKind.DECLARED ) {
|
if ( mirror == null ) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else if ( mirror.getKind() == TypeKind.DECLARED ) {
|
||||||
return getType( ( (DeclaredType) mirror ) );
|
return getType( ( (DeclaredType) mirror ) );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user