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>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<com.jolira.hickory.version>1.0.0</com.jolira.hickory.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@ -49,7 +50,11 @@
|
||||
<artifactId>fest-assert</artifactId>
|
||||
<version>1.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.jolira</groupId>
|
||||
<artifactId>hickory</artifactId>
|
||||
<version>${com.jolira.hickory.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
@ -70,6 +75,11 @@
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>build-helper-maven-plugin</artifactId>
|
||||
<version>1.7</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
@ -85,6 +95,11 @@
|
||||
<artifactId>maven-license-plugin</artifactId>
|
||||
<version>1.9.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.bsc.maven</groupId>
|
||||
<artifactId>maven-processor-plugin</artifactId>
|
||||
<version>2.0.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
|
||||
|
@ -31,11 +31,20 @@
|
||||
<packaging>jar</packaging>
|
||||
<name>MapStruct Processor</name>
|
||||
|
||||
<properties>
|
||||
<prism.directory>${project.build.directory}/generated-sources/prims</prism.directory>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jolira</groupId>
|
||||
<artifactId>hickory</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
@ -49,7 +58,6 @@
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
@ -80,6 +88,50 @@
|
||||
<compilerArgument>-proc:none</compilerArgument>
|
||||
</configuration>
|
||||
</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>
|
||||
</build>
|
||||
|
||||
|
@ -23,21 +23,15 @@ import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
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.ExecutableElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
import javax.lang.model.type.DeclaredType;
|
||||
import javax.lang.model.type.TypeKind;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.lang.model.util.ElementKindVisitor6;
|
||||
import javax.lang.model.util.Elements;
|
||||
import javax.lang.model.util.SimpleAnnotationValueVisitor6;
|
||||
import javax.lang.model.util.Types;
|
||||
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 MAPPING_ANNOTATION = "org.mapstruct.Mapping";
|
||||
private final static String MAPPINGS_ANNOTATION = "org.mapstruct.Mappings";
|
||||
|
||||
private final ProcessingEnvironment processingEnvironment;
|
||||
private final Types typeUtils;
|
||||
private final Elements elementUtils;
|
||||
@ -263,22 +254,17 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
||||
}
|
||||
|
||||
private List<MappedProperty> retrieveMappedProperties(ExecutableElement method) {
|
||||
|
||||
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()
|
||||
.asElement()
|
||||
.accept( new NameDeterminationVisitor(), null );
|
||||
if ( mappingAnnotation != null ) {
|
||||
mappings.put( mappingAnnotation.source(), getMapping( mappingAnnotation ) );
|
||||
}
|
||||
|
||||
if ( MAPPING_ANNOTATION.equals( annotationName ) ) {
|
||||
Mapping mapping = getMapping( annotationMirror );
|
||||
mappings.put( mapping.getSourceName(), mapping );
|
||||
}
|
||||
else if ( MAPPINGS_ANNOTATION.equals( annotationName ) ) {
|
||||
mappings.putAll( getMappings( annotationMirror ) );
|
||||
}
|
||||
if ( mappingsAnnotation != null ) {
|
||||
mappings.putAll( getMappings( mappingsAnnotation ) );
|
||||
}
|
||||
|
||||
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>();
|
||||
|
||||
List<? extends AnnotationValue> values = getAnnotationValueListValue( annotationMirror, "value" );
|
||||
|
||||
for ( AnnotationValue oneAnnotationValue : values ) {
|
||||
AnnotationMirror oneAnnotation = oneAnnotationValue.accept(
|
||||
new AnnotationRetrievingVisitor(),
|
||||
null
|
||||
);
|
||||
Mapping mapping = getMapping( oneAnnotation );
|
||||
mappings.put( mapping.getSourceName(), mapping );
|
||||
for ( MappingPrism mapping : mappingsAnnotation.value() ) {
|
||||
mappings.put( mapping.source(), getMapping( mapping ) );
|
||||
}
|
||||
|
||||
return mappings;
|
||||
}
|
||||
|
||||
private Mapping getMapping(AnnotationMirror annotationMirror) {
|
||||
String sourcePropertyName = getStringValue( annotationMirror, "source" );
|
||||
String targetPropertyName = getStringValue( annotationMirror, "target" );
|
||||
TypeMirror converterTypeMirror = getTypeMirrorValue( annotationMirror, "converter" );
|
||||
|
||||
Type converterType = null;
|
||||
|
||||
if ( converterTypeMirror != null ) {
|
||||
converterType = typeUtil.getType( (DeclaredType) converterTypeMirror );
|
||||
}
|
||||
|
||||
return new Mapping( sourcePropertyName, targetPropertyName, converterType );
|
||||
private Mapping getMapping(MappingPrism mapping) {
|
||||
Type converterType = typeUtil.retrieveType( mapping.converter() );
|
||||
return new Mapping(
|
||||
mapping.source(),
|
||||
mapping.target(),
|
||||
converterType.getName().equals( "NoOpConverter" ) ? null : converterType
|
||||
);
|
||||
}
|
||||
|
||||
private Parameter retrieveParameter(ExecutableElement method) {
|
||||
@ -374,42 +348,6 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
||||
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) {
|
||||
List<ExecutableElement> getterMethods = new LinkedList<ExecutableElement>();
|
||||
|
||||
@ -441,46 +379,4 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
||||
|
||||
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.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")
|
||||
@GeneratePrisms({
|
||||
@GeneratePrism(value = Mapper.class),
|
||||
@GeneratePrism(value = Mapping.class),
|
||||
@GeneratePrism(value = Mappings.class)
|
||||
})
|
||||
public class MappingProcessor extends AbstractProcessor {
|
||||
|
||||
/**
|
||||
|
@ -51,7 +51,10 @@ public class TypeUtil {
|
||||
}
|
||||
|
||||
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 ) );
|
||||
}
|
||||
else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user