mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
Extracting writing of model to file from visitor
This commit is contained in:
parent
102d467b57
commit
069e4f3a80
@ -15,7 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package de.moapa.maple.ap;
|
package de.moapa.maple.ap;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -41,8 +41,8 @@ import de.moapa.maple.ap.model.Mapper;
|
|||||||
import de.moapa.maple.ap.model.MapperMethod;
|
import de.moapa.maple.ap.model.MapperMethod;
|
||||||
import de.moapa.maple.ap.model.Parameter;
|
import de.moapa.maple.ap.model.Parameter;
|
||||||
import de.moapa.maple.ap.model.Type;
|
import de.moapa.maple.ap.model.Type;
|
||||||
import freemarker.template.Configuration;
|
import de.moapa.maple.ap.writer.DozerModelWriter;
|
||||||
import freemarker.template.Template;
|
import de.moapa.maple.ap.writer.ModelWriter;
|
||||||
|
|
||||||
import static javax.lang.model.util.ElementFilter.methodsIn;
|
import static javax.lang.model.util.ElementFilter.methodsIn;
|
||||||
|
|
||||||
@ -50,8 +50,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 TEMPLATE_NAME = "dozer-mapper-implementation.ftl";
|
|
||||||
|
|
||||||
private final static String MAPPING_ANNOTATION = "de.moapa.maple.Mapping";
|
private final static String MAPPING_ANNOTATION = "de.moapa.maple.Mapping";
|
||||||
|
|
||||||
private final static String MAPPINGS_ANNOTATION = "de.moapa.maple.Mappings";
|
private final static String MAPPINGS_ANNOTATION = "de.moapa.maple.Mappings";
|
||||||
@ -60,19 +58,15 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
|
|
||||||
private final ProcessingEnvironment processingEnvironment;
|
private final ProcessingEnvironment processingEnvironment;
|
||||||
|
|
||||||
private final Configuration configuration;
|
|
||||||
|
|
||||||
private final Types typeUtils;
|
private final Types typeUtils;
|
||||||
|
|
||||||
private final Elements elementUtils;
|
private final Elements elementUtils;
|
||||||
|
|
||||||
public MapperGenerationVisitor(ProcessingEnvironment processingEnvironment, Configuration configuration) {
|
public MapperGenerationVisitor(ProcessingEnvironment processingEnvironment) {
|
||||||
|
|
||||||
this.processingEnvironment = processingEnvironment;
|
this.processingEnvironment = processingEnvironment;
|
||||||
this.typeUtils = processingEnvironment.getTypeUtils();
|
this.typeUtils = processingEnvironment.getTypeUtils();
|
||||||
this.elementUtils = processingEnvironment.getElementUtils();
|
this.elementUtils = processingEnvironment.getElementUtils();
|
||||||
|
|
||||||
this.configuration = configuration;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -87,18 +81,17 @@ public class MapperGenerationVisitor extends ElementKindVisitor6<Void, Void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void writeModelToSourceFile(String fileName, Mapper model) {
|
private void writeModelToSourceFile(String fileName, Mapper model) {
|
||||||
try {
|
|
||||||
JavaFileObject sourceFile = processingEnvironment.getFiler().createSourceFile( fileName );
|
|
||||||
BufferedWriter writer = new BufferedWriter( sourceFile.openWriter() );
|
|
||||||
|
|
||||||
Template template = configuration.getTemplate( TEMPLATE_NAME );
|
JavaFileObject sourceFile;
|
||||||
template.process( model, writer );
|
try {
|
||||||
writer.flush();
|
sourceFile = processingEnvironment.getFiler().createSourceFile( fileName );
|
||||||
writer.close();
|
|
||||||
}
|
}
|
||||||
catch ( Exception e ) {
|
catch ( IOException e ) {
|
||||||
throw new RuntimeException( e );
|
throw new RuntimeException( e );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ModelWriter modelWriter = new DozerModelWriter();
|
||||||
|
modelWriter.writeModel( sourceFile, model );
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mapper retrieveModel(TypeElement element) {
|
private Mapper retrieveModel(TypeElement element) {
|
||||||
|
@ -15,7 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
package de.moapa.maple.ap;
|
package de.moapa.maple.ap;
|
||||||
|
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.annotation.processing.AbstractProcessor;
|
import javax.annotation.processing.AbstractProcessor;
|
||||||
import javax.annotation.processing.ProcessingEnvironment;
|
import javax.annotation.processing.ProcessingEnvironment;
|
||||||
@ -26,9 +25,6 @@ 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 freemarker.template.Configuration;
|
|
||||||
import freemarker.template.DefaultObjectWrapper;
|
|
||||||
|
|
||||||
@SupportedAnnotationTypes("de.moapa.maple.Mapper")
|
@SupportedAnnotationTypes("de.moapa.maple.Mapper")
|
||||||
public class MappingProcessor extends AbstractProcessor {
|
public class MappingProcessor extends AbstractProcessor {
|
||||||
|
|
||||||
@ -37,16 +33,9 @@ public class MappingProcessor extends AbstractProcessor {
|
|||||||
*/
|
*/
|
||||||
private static final boolean ANNOTATIONS_CLAIMED_EXCLUSIVELY = false;
|
private static final boolean ANNOTATIONS_CLAIMED_EXCLUSIVELY = false;
|
||||||
|
|
||||||
private Configuration configuration;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void init(ProcessingEnvironment processingEnv) {
|
public synchronized void init(ProcessingEnvironment processingEnv) {
|
||||||
|
|
||||||
super.init( processingEnv );
|
super.init( processingEnv );
|
||||||
|
|
||||||
configuration = new Configuration();
|
|
||||||
configuration.setClassForTemplateLoading( MappingProcessor.class, "/" );
|
|
||||||
configuration.setObjectWrapper( new DefaultObjectWrapper() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -68,7 +57,7 @@ public class MappingProcessor extends AbstractProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for ( Element oneAnnotatedElement : roundEnvironment.getElementsAnnotatedWith( oneAnnotation ) ) {
|
for ( Element oneAnnotatedElement : roundEnvironment.getElementsAnnotatedWith( oneAnnotation ) ) {
|
||||||
oneAnnotatedElement.accept( new MapperGenerationVisitor( processingEnv, configuration ), null );
|
oneAnnotatedElement.accept( new MapperGenerationVisitor( processingEnv ), null );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,59 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package de.moapa.maple.ap.writer;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import javax.tools.JavaFileObject;
|
||||||
|
|
||||||
|
import de.moapa.maple.ap.model.Mapper;
|
||||||
|
import freemarker.template.Configuration;
|
||||||
|
import freemarker.template.DefaultObjectWrapper;
|
||||||
|
import freemarker.template.Template;
|
||||||
|
|
||||||
|
public class DozerModelWriter implements ModelWriter {
|
||||||
|
|
||||||
|
private final static String TEMPLATE_NAME = "dozer-mapper-implementation.ftl";
|
||||||
|
|
||||||
|
private static final Configuration configuration;
|
||||||
|
|
||||||
|
static {
|
||||||
|
|
||||||
|
configuration = new Configuration();
|
||||||
|
configuration.setClassForTemplateLoading( DozerModelWriter.class, "/" );
|
||||||
|
configuration.setObjectWrapper( new DefaultObjectWrapper() );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeModel(JavaFileObject sourceFile, Mapper model) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
BufferedWriter writer = new BufferedWriter( sourceFile.openWriter() );
|
||||||
|
|
||||||
|
Template template = configuration.getTemplate( TEMPLATE_NAME );
|
||||||
|
template.process( model, writer );
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
catch ( RuntimeException e ) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
catch ( Exception e ) {
|
||||||
|
throw new RuntimeException( e );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package de.moapa.maple.ap.writer;
|
||||||
|
|
||||||
|
import javax.tools.JavaFileObject;
|
||||||
|
|
||||||
|
import de.moapa.maple.ap.model.Mapper;
|
||||||
|
|
||||||
|
public interface ModelWriter {
|
||||||
|
|
||||||
|
void writeModel(JavaFileObject sourceFile, Mapper model);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user