Commit 9ca21465 authored by Daniel Bluhm's avatar Daniel Bluhm

Convert ImplementationWriter to new writer

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 1291089b
......@@ -11,11 +11,13 @@
package org.eclipse.ice.dev.annotations.processors;
import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.processing.Filer;
import javax.lang.model.element.Element;
import org.eclipse.ice.dev.annotations.DataField;
......@@ -89,13 +91,25 @@ public class DataElementAnnotationExtractor {
* @throws IOException
*/
public void generateAndWrite(AnnotationExtractionRequest request) throws IOException {
this.generateWriters(request).forEach(writer -> {
try {
writer.write();
} catch (IOException e) {
e.printStackTrace();
}
});
AnnotationExtractionResponse response = annotationExtractionService.extract(request);
Filer filer = writerGenerator.processingEnv.getFiler();
writerGenerator.generateWriters(request.getElement(),response)
.forEach(writer -> {
try {
writer.write();
} catch (IOException e) {
e.printStackTrace();
}
});
writerGenerator.generate(response)
.forEach(writer -> {
try (Writer file = writer.openWriter(filer)) {
writer.write(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}
/**
......
/*******************************************************************************
* 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;
/**
* Implementation of the VelocitySourceWriter that handles generating the
* DataElementImplementation
*
* @author Michael Walsh
*/
public class DataElementImplementationWriter extends ImplementationWriter {
/**
* Location of DataElement template for use with velocity.
*/
private static final String IMPL_TEMPLATE = "templates/DataElement.vm";
/**
* Constructor
*
* @param packageName
* @param interfaceName
* @param className
* @param fields
* @param generatedFile
*/
@Builder
public DataElementImplementationWriter(
String packageName, String interfaceName, String className, Fields
fields, Types types, FileObject generatedFile
) {
super(packageName, interfaceName, className, fields, types, generatedFile);
this.template = IMPL_TEMPLATE;
}
/**
* Private argless constructor purely for use of the static method to have
* access to the inherited getInitializer() method
*/
private DataElementImplementationWriter() {
super();
}
/**
* Supplies a lambda that will provide a fully initialized
* DataElementImplementationWriter given a map and a JavaFileObject
*/
@Override
public BiFunction<FileObject, Map, List<SelfInitializingWriter>> getInitializer() {
return (fileObject, context) ->
Arrays.asList(DataElementImplementationWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
.interfaceName((String) context.get(MetaTemplateProperty.INTERFACE))
.className((String) context.get(MetaTemplateProperty.CLASS))
.fields((Fields) context.get(MetaTemplateProperty.FIELDS))
.types(((Fields) context.get(MetaTemplateProperty.FIELDS)).getTypes())
.generatedFile(fileObject).build());
}
/**
* Static method for cleanly fetching an initializer
*
* @return DataElementImplementationWriter init lambda
*/
public static BiFunction<FileObject, Map, List<SelfInitializingWriter>> getContextInitializer() {
return new DataElementImplementationWriter().getInitializer();
}
}
......@@ -15,7 +15,10 @@ import java.util.ArrayList;
import java.util.HashMap;
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;
......@@ -51,10 +54,6 @@ public class DataElementWriterGenerator
MetaTemplateProperty.QUALIFIED,
DataElementInterfaceWriter.getContextInitializer()
);
writerInitializers.put(
MetaTemplateProperty.QUALIFIEDIMPL,
DataElementImplementationWriter.getContextInitializer()
);
writerInitializers.put(
PersistenceHandlerTemplateProperty.QUALIFIED,
DataElementPersistenceHandlerWriter.getContextInitializer()
......@@ -94,8 +93,12 @@ public class DataElementWriterGenerator
@Override
public List<GeneratedFileWriter> generate(AnnotationExtractionResponse response) {
return List.of(
TypeScriptWriter.fromContext(response.getClassMetadata())
);
List<GeneratedFileWriter> writers = new ArrayList<>();
writers.add(ImplementationWriter.fromContext(response.getClassMetadata()));
writers.add(TypeScriptWriter.fromContext(response.getClassMetadata()));
return writers
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}
......@@ -12,16 +12,29 @@
package org.eclipse.ice.dev.annotations.processors;
import lombok.Builder;
import lombok.NonNull;
import javax.tools.FileObject;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import javax.annotation.processing.Filer;
/**
* Writer for DataElement Implementation classes.
*
*
* @author Daniel Bluhm
*/
public abstract class ImplementationWriter extends SelfInitializingWriter {
public class ImplementationWriter
extends VelocitySourceWriter
implements GeneratedFileWriter
{
/**
* Location of DataElement template for use with velocity.
*/
private static final String IMPL_TEMPLATE = "templates/DataElement.vm";
/**
* Context key for package.
......@@ -48,20 +61,27 @@ public abstract class ImplementationWriter extends SelfInitializingWriter {
*/
private static final String CLASS = "class";
/**
* Name of class to be generated.
*/
private String className;
/**
* Constructor
*
*
* @param packageName
* @param interfaceName
* @param className
* @param fields
* @param generatedFile
*/
@Builder
public ImplementationWriter(
String packageName, String interfaceName, String className,
@NonNull Fields fields, @NonNull Types types, FileObject generatedFile
@NonNull Fields fields, @NonNull Types types
) {
super(generatedFile);
this.template = IMPL_TEMPLATE;
this.className = className;
this.context.put(PACKAGE, packageName);
this.context.put(INTERFACE, interfaceName);
this.context.put(CLASS, className);
......@@ -69,8 +89,28 @@ public abstract class ImplementationWriter extends SelfInitializingWriter {
this.context.put(TYPES, types);
}
protected ImplementationWriter() {
@Override
public Writer openWriter(Filer filer) throws IOException {
return filer.createSourceFile(className).openWriter();
}
/**
* Map from context to implementation writer.
*
* TODO move this logic elsewhere.
*
* @param context extracted data.
* @return initialized ImplementationWriter
*/
public static ImplementationWriter fromContext(
Map<TemplateProperty, Object> context
) {
return ImplementationWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
.interfaceName((String) context.get(MetaTemplateProperty.INTERFACE))
.className((String) context.get(MetaTemplateProperty.CLASS))
.fields((Fields) context.get(MetaTemplateProperty.FIELDS))
.types(((Fields) context.get(MetaTemplateProperty.FIELDS)).getTypes())
.build();
}
}
......@@ -122,10 +122,16 @@ public class TypeScriptWriter
Map<TemplateProperty, Object> context
) {
Fields trimmed = ((Fields) context.get(MetaTemplateProperty.FIELDS)).getNonDefaultFields();
return TypeScriptWriter.builder()
.name((String) context.get(MetaTemplateProperty.CLASS))
.fields(trimmed)
.types(trimmed.getTypes())
.build();
TypeScriptWriter instance = null;
try {
instance = TypeScriptWriter.builder()
.name((String) context.get(MetaTemplateProperty.CLASS))
.fields(trimmed)
.types(trimmed.getTypes())
.build();
} catch (UnsupportedOperationException e) {
// TODO Handle this better
}
return instance;
}
}
\ No newline at end of file
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