Commit 8ab55c2d authored by Daniel Bluhm's avatar Daniel Bluhm

Convert interface writer to new writer

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 9ca21465
/*******************************************************************************
* 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
* DataElementInterfaceWriter
*
* @author Michael Walsh
*/
public class DataElementInterfaceWriter extends InterfaceWriter {
/**
* Location of Interface template for use with velocity.
*
* Use of Velocity ClasspathResourceLoader means files are discovered
* relative to the src/main/resources folder.
*/
private static final String TEMPLATE = "templates/ElementInterface.vm";
/**
* Constructor
*
* @param packageName
* @param interfaceName
* @param fields
* @param generatedFile
*/
@Builder
public DataElementInterfaceWriter(
String packageName, String interfaceName, @NonNull Fields fields,
Types types, FileObject generatedFile
) {
super(packageName, interfaceName, fields, types, generatedFile);
this.template = TEMPLATE;
}
/**
* Private argless constructor purely for use of the static method to have
* access to the inherited getInitializer() method
*/
private DataElementInterfaceWriter() {
super();
}
/**
* Supplies a lambda that will provide a fully initialized
* DataElementInterfaceWriter given a map and a JavaFileObject
*/
@Override
public BiFunction<FileObject, Map, List<SelfInitializingWriter>> getInitializer() {
return (fileObject, context) ->
Arrays.asList(DataElementInterfaceWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
.interfaceName((String) context.get(MetaTemplateProperty.INTERFACE))
.fields((Fields) context.get(MetaTemplateProperty.FIELDS))
.types(new Types(((Fields) context.get(MetaTemplateProperty.FIELDS)).getInterfaceFields()))
.generatedFile(fileObject).build());
}
/**
* Static method for cleanly fetching an initializer
*
* @return DataElementInterfaceWriter init lambda
*/
public static BiFunction<FileObject, Map, List<SelfInitializingWriter>> getContextInitializer() {
return new DataElementInterfaceWriter().getInitializer();
}
}
......@@ -50,10 +50,6 @@ public class DataElementWriterGenerator
DataElementWriterGenerator(ProcessingEnvironment processingEnv) {
super(processingEnv);
writerInitializers.put(
MetaTemplateProperty.QUALIFIED,
DataElementInterfaceWriter.getContextInitializer()
);
writerInitializers.put(
PersistenceHandlerTemplateProperty.QUALIFIED,
DataElementPersistenceHandlerWriter.getContextInitializer()
......@@ -94,6 +90,7 @@ public class DataElementWriterGenerator
@Override
public List<GeneratedFileWriter> generate(AnnotationExtractionResponse response) {
List<GeneratedFileWriter> writers = new ArrayList<>();
writers.add(InterfaceWriter.fromContext(response.getClassMetadata()));
writers.add(ImplementationWriter.fromContext(response.getClassMetadata()));
writers.add(TypeScriptWriter.fromContext(response.getClassMetadata()));
return writers
......
......@@ -12,16 +12,32 @@
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 Interfaces.
*
*
* @author Daniel Bluhm
*/
public abstract class InterfaceWriter extends SelfInitializingWriter {
public class InterfaceWriter
extends VelocitySourceWriter
implements GeneratedFileWriter
{
/**
* Location of Interface template for use with velocity.
*
* Use of Velocity ClasspathResourceLoader means files are discovered
* relative to the src/main/resources folder.
*/
private static final String TEMPLATE = "templates/ElementInterface.vm";
/**
* Context key for package.
......@@ -43,29 +59,54 @@ public abstract class InterfaceWriter extends SelfInitializingWriter {
*/
private static final String TYPES = "types";
/**
* Name of generated interface;
*/
private String interfaceName;
/**
* Constructor
*
*
* @param packageName
* @param interfaceName
* @param fields
* @param generatedFile
*/
@Builder
public InterfaceWriter(
String packageName, String interfaceName, @NonNull Fields fields,
@NonNull Types types, FileObject generatedFile
@NonNull Types types
) {
super(generatedFile);
this.template = TEMPLATE;
this.interfaceName = interfaceName;
context.put(PACKAGE, packageName);
context.put(INTERFACE, interfaceName);
context.put(FIELDS, fields);
context.put(TYPES, types);
}
@Override
public Writer openWriter(Filer filer) throws IOException {
return filer.createSourceFile(interfaceName).openWriter();
}
public InterfaceWriter() {
// TODO Auto-generated constructor stub
super();
/**
* Create InterfaceWriter from context.
*
* TODO move this logic elsewhere.
*
* @param context map of extracted data.
* @return initialized InterfaceWriter.
*/
public static InterfaceWriter fromContext(
Map<TemplateProperty, Object> context
) {
return InterfaceWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
.interfaceName((String) context.get(MetaTemplateProperty.INTERFACE))
.fields((Fields) context.get(MetaTemplateProperty.FIELDS))
.types(new Types(((Fields) context.get(MetaTemplateProperty.FIELDS)).getInterfaceFields()))
.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