Commit c766cc9d authored by Daniel Bluhm's avatar Daniel Bluhm

Convert PersistenceHandlerWriter to new writer

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 8ab55c2d
/*******************************************************************************
* Copyright (c) 2020- UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Michael Walsh - Initial implementation
*******************************************************************************/
package org.eclipse.ice.dev.annotations.processors;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import lombok.Builder;
import lombok.NonNull;
/**
* Implementation of the VelocitySourceWriter that handles generating the
* DataElementPersistenceHandlerWriter
*
* @author Michael Walsh
*/
public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWriter {
/**
* Location of PersistenceHandler template for use with velocity.
*
* Use of Velocity ClasspathResourceLoader means files are discovered relative
* to the src/main/resources folder.
*/
private static final String PERSISTENCE_HANDLER_TEMPLATE = "templates/PersistenceHandler.vm";
/**
* Constructor
*
* @param packageName
* @param elementInterface
* @param className
* @param interfaceName
* @param implementation
* @param collection
* @param fields
* @param generatedFile
*/
@Builder
public DataElementPersistenceHandlerWriter(
String packageName, String elementInterface, String className,
String interfaceName, String implementation, String collection,
@NonNull Fields fields, Types types, FileObject generatedFile
) {
super(
packageName, elementInterface, className, interfaceName,
implementation, collection, fields, types, generatedFile
);
this.template = PERSISTENCE_HANDLER_TEMPLATE;
}
/**
* Private argless constructor purely for use of the static method to have
* access to the inherited getInitializer() method
*/
private DataElementPersistenceHandlerWriter() {
super();
}
/**
* Supplies a lambda that will provide a fully initialized
* DataElementPersistenceHandlerWriter given a map and a JavaFileObject
*/
@Override
public BiFunction<FileObject, Map, List<SelfInitializingWriter>> getInitializer() {
return (fileObject, context) -> Arrays.asList(
DataElementPersistenceHandlerWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
.className((String) context.get(PersistenceHandlerTemplateProperty.CLASS))
.types(((Fields) context.get(MetaTemplateProperty.FIELDS)).getTypes())
.interfaceName((String) context.get(PersistenceHandlerTemplateProperty.INTERFACE))
.fields((Fields) context.get(MetaTemplateProperty.FIELDS))
.types(((Fields) context.get(MetaTemplateProperty.FIELDS)).getTypes())
.elementInterface((String) context.get(PersistenceHandlerTemplateProperty.ELEMENT_INTERFACE))
.collection((String) context.get(PersistenceHandlerTemplateProperty.COLLECTION))
.implementation((String) context.get(PersistenceHandlerTemplateProperty.IMPLEMENTATION))
.generatedFile(fileObject).build()
);
}
/**
* Static method for cleanly fetching an initializer
*
* @return DataElementPersistenceHandlerWriter init lambda
*/
public static BiFunction<FileObject, Map, List<SelfInitializingWriter>> getContextInitializer() {
return new DataElementPersistenceHandlerWriter().getInitializer();
}
}
......@@ -17,13 +17,11 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import org.eclipse.ice.dev.annotations.Persisted;
......@@ -50,10 +48,6 @@ public class DataElementWriterGenerator
DataElementWriterGenerator(ProcessingEnvironment processingEnv) {
super(processingEnv);
writerInitializers.put(
PersistenceHandlerTemplateProperty.QUALIFIED,
DataElementPersistenceHandlerWriter.getContextInitializer()
);
}
/**
......@@ -93,6 +87,10 @@ public class DataElementWriterGenerator
writers.add(InterfaceWriter.fromContext(response.getClassMetadata()));
writers.add(ImplementationWriter.fromContext(response.getClassMetadata()));
writers.add(TypeScriptWriter.fromContext(response.getClassMetadata()));
// TODO This check should be more graceful or happen elsewhere
if (response.getClassMetadata().get(PersistenceHandlerTemplateProperty.COLLECTION) != null) {
writers.add(PersistenceHandlerWriter.fromContext(response.getClassMetadata()));
}
return writers
.stream()
.filter(Objects::nonNull)
......
......@@ -12,16 +12,24 @@
package org.eclipse.ice.dev.annotations.processors;
import javax.tools.FileObject;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import javax.annotation.processing.Filer;
import lombok.Builder;
import lombok.NonNull;
/**
* Writer for DataElement Persistence classes.
*
*
* @author Daniel Bluhm
*/
public abstract class PersistenceHandlerWriter extends SelfInitializingWriter {
public class PersistenceHandlerWriter
extends VelocitySourceWriter
implements GeneratedFileWriter
{
/**
* Context key for package.
......@@ -61,11 +69,20 @@ public abstract class PersistenceHandlerWriter extends SelfInitializingWriter {
/**
* Context key for types.
*/
private static final String TYPES = "types";
private static final String TYPES = "types";
/**
* Path to template of persistence handler.
*/
private static final String TEMPLATE = "templates/PersistenceHandler.vm";
/**
* Name of generated class.
*/
private String className;
/**
* Constructor
*
*
* @param packageName
* @param elementInterface
* @param className
......@@ -75,9 +92,14 @@ public abstract class PersistenceHandlerWriter extends SelfInitializingWriter {
* @param fields
* @param generatedFile
*/
public PersistenceHandlerWriter(String packageName, String elementInterface, String className, String interfaceName,
String implementation, String collection, @NonNull Fields fields, @NonNull Types types, FileObject generatedFile) {
super(generatedFile);
@Builder
public PersistenceHandlerWriter(
String packageName, String elementInterface, String className, String
interfaceName, String implementation, String collection, @NonNull Fields
fields, @NonNull Types types
) {
this.template = TEMPLATE;
this.className = className;
this.context.put(PACKAGE, packageName);
this.context.put(ELEMENT_INTERFACE, elementInterface);
this.context.put(CLASS, className);
......@@ -88,8 +110,29 @@ public abstract class PersistenceHandlerWriter extends SelfInitializingWriter {
this.context.put(TYPES, types);
}
protected PersistenceHandlerWriter() {
@Override
public Writer openWriter(Filer filer) throws IOException {
return filer.createSourceFile(className).openWriter();
}
/**
* Create instance of PersistenceHanlderWriter from context.
* @param context Map of extracted data.
* @return initialized PersistenceHandlerWriter.
*/
public static PersistenceHandlerWriter fromContext(
Map<TemplateProperty, Object> context
) {
return PersistenceHandlerWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
.className((String) context.get(PersistenceHandlerTemplateProperty.CLASS))
.types(((Fields) context.get(MetaTemplateProperty.FIELDS)).getTypes())
.interfaceName((String) context.get(PersistenceHandlerTemplateProperty.INTERFACE))
.fields((Fields) context.get(MetaTemplateProperty.FIELDS))
.types(((Fields) context.get(MetaTemplateProperty.FIELDS)).getTypes())
.elementInterface((String) context.get(PersistenceHandlerTemplateProperty.ELEMENT_INTERFACE))
.collection((String) context.get(PersistenceHandlerTemplateProperty.COLLECTION))
.implementation((String) context.get(PersistenceHandlerTemplateProperty.IMPLEMENTATION))
.build();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment