Commit 1291089b authored by Daniel Bluhm's avatar Daniel Bluhm

Begin transitioning to new writers

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent cacb2c24
......@@ -75,7 +75,7 @@ public class DataElementAnnotationExtractor {
* @return list of generated SourceWriters
* @throws IOException due to {@link ICEAnnotationExtractionService#extract(AnnotationExtractionRequest)}
*/
public List<VelocitySourceWriter> generateWriters(
public List<SelfInitializingWriter> generateWriters(
AnnotationExtractionRequest request
) throws IOException {
AnnotationExtractionResponse response = annotationExtractionService.extract(request);
......
......@@ -64,7 +64,7 @@ public class DataElementImplementationWriter extends ImplementationWriter {
* DataElementImplementationWriter given a map and a JavaFileObject
*/
@Override
public BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer() {
public BiFunction<FileObject, Map, List<SelfInitializingWriter>> getInitializer() {
return (fileObject, context) ->
Arrays.asList(DataElementImplementationWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
......@@ -80,7 +80,7 @@ public class DataElementImplementationWriter extends ImplementationWriter {
*
* @return DataElementImplementationWriter init lambda
*/
public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
public static BiFunction<FileObject, Map, List<SelfInitializingWriter>> getContextInitializer() {
return new DataElementImplementationWriter().getInitializer();
}
......
......@@ -65,7 +65,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter {
* DataElementInterfaceWriter given a map and a JavaFileObject
*/
@Override
public BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer() {
public BiFunction<FileObject, Map, List<SelfInitializingWriter>> getInitializer() {
return (fileObject, context) ->
Arrays.asList(DataElementInterfaceWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
......@@ -80,7 +80,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter {
*
* @return DataElementInterfaceWriter init lambda
*/
public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
public static BiFunction<FileObject, Map, List<SelfInitializingWriter>> getContextInitializer() {
return new DataElementInterfaceWriter().getInitializer();
}
}
......@@ -75,7 +75,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite
* DataElementPersistenceHandlerWriter given a map and a JavaFileObject
*/
@Override
public BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer() {
public BiFunction<FileObject, Map, List<SelfInitializingWriter>> getInitializer() {
return (fileObject, context) -> Arrays.asList(
DataElementPersistenceHandlerWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
......@@ -96,7 +96,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite
*
* @return DataElementPersistenceHandlerWriter init lambda
*/
public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
public static BiFunction<FileObject, Map, List<SelfInitializingWriter>> getContextInitializer() {
return new DataElementPersistenceHandlerWriter().getInitializer();
}
}
/*******************************************************************************
* 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
* DataElementImplementation
*
* @author Daniel Bluhm
*/
public class DataElementTypeScriptWriter extends TypeScriptWriter {
/**
* Template used for this writer.
*/
private static final String TEMPLATE = "templates/TypeScript.vm";
/**
* Constructor
*
* @param packageName
* @param interfaceName
* @param className
* @param fields
* @param generatedFile
*/
@Builder
public DataElementTypeScriptWriter(
String name, @NonNull Fields fields, @NonNull Types types,
FileObject generatedFile
) {
super(name, 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 DataElementTypeScriptWriter() {
super();
}
/**
* Supplies a lambda that will provide a fully initialized
* DataElementImplementationWriter given a map and a JavaFileObject
*/
@Override
public BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer() {
return (fileObject, context) -> {
Fields trimmed = ((Fields) context.get(MetaTemplateProperty.FIELDS)).getNonDefaultFields();
return Arrays.asList(
DataElementTypeScriptWriter.builder()
.name((String) context.get(MetaTemplateProperty.CLASS))
.fields(trimmed)
.types(trimmed.getTypes())
.generatedFile(fileObject).build()
);
};
}
/**
* Static method for cleanly fetching an initializer
*
* @return DataElementTypeScriptWriter init lambda
*/
public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
return new DataElementTypeScriptWriter().getInitializer();
}
}
......@@ -29,12 +29,15 @@ import org.eclipse.ice.dev.annotations.Persisted;
* generate the interface, implementation, and persistence handler.
*
*/
public class DataElementWriterGenerator extends AbstractWriterGenerator {
public class DataElementWriterGenerator
extends AbstractWriterGenerator
implements WriterGenerator<AnnotationExtractionResponse>
{
/**
* Map of file name key to the respective file's writer initializer
*/
private Map<TemplateProperty, BiFunction<FileObject, Map, List<VelocitySourceWriter>>> writerInitializers =
private Map<TemplateProperty, BiFunction<FileObject, Map, List<SelfInitializingWriter>>> writerInitializers =
new HashMap<>();
/**
......@@ -56,20 +59,16 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator {
PersistenceHandlerTemplateProperty.QUALIFIED,
DataElementPersistenceHandlerWriter.getContextInitializer()
);
writerInitializers.put(
MetaTemplateProperty.TYPESCRIPT,
DataElementTypeScriptWriter.getContextInitializer()
);
}
/**
* DataElement specific method of class generation. Includes interfaces,
* implementation, and possibly a persistence handler
*/
public List<VelocitySourceWriter> generateWriters(
public List<SelfInitializingWriter> generateWriters(
Element element, AnnotationExtractionResponse response
) {
List<VelocitySourceWriter> writers = new ArrayList<>();
List<SelfInitializingWriter> writers = new ArrayList<>();
Map<TemplateProperty, Object> classMetadata = response.getClassMetadata();
boolean hasAnnotation = specExtractionHelper.hasAnnotation(element, Persisted.class);
......@@ -77,16 +76,9 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator {
.filter(key -> key != PersistenceHandlerTemplateProperty.QUALIFIED || hasAnnotation)
.forEach(key -> {
try {
FileObject fileObject = null;
if (key == MetaTemplateProperty.TYPESCRIPT) {
fileObject = createResourceForName(
(String) classMetadata.get(MetaTemplateProperty.CLASS)
);
} else {
String name = (String) classMetadata.get(key);
fileObject = createFileObjectForName(name);
}
List<VelocitySourceWriter> newWriters = writerInitializers
FileObject fileObject = createFileObjectForName(name);
List<SelfInitializingWriter> newWriters = writerInitializers
.get(key)
.apply(
fileObject,
......@@ -97,8 +89,13 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator {
e.printStackTrace();
}
});
return writers;
}
@Override
public List<GeneratedFileWriter> generate(AnnotationExtractionResponse response) {
return List.of(
TypeScriptWriter.fromContext(response.getClassMetadata())
);
}
}
......@@ -10,12 +10,6 @@ import java.io.Writer;
*/
public interface FileWriter {
/**
* Get the filename suggested by this file generator.
* @return suggested file name.
*/
public String suggestedFileName();
/**
* Write the file to the open writer.
* @param writer to which the file will be written.
......
package org.eclipse.ice.dev.annotations.processors;
import java.io.IOException;
import java.io.Writer;
import javax.annotation.processing.Filer;
/**
* FileWriter interface for files generated during annotation processing.
*
* @author Daniel Bluhm
*/
public interface GeneratedFileWriter extends FileWriter {
/**
* Open a java.io.Writer for this FileWriter using the annotation processing
* environment filer.
* @param filer Annotation processing environment filer
* @return writer
*/
public Writer openWriter(Filer filer) throws IOException;
}
......@@ -15,14 +15,13 @@ package org.eclipse.ice.dev.annotations.processors;
import lombok.NonNull;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
/**
* Writer for DataElement Implementation classes.
*
* @author Daniel Bluhm
*/
public abstract class ImplementationWriter extends VelocitySourceWriter {
public abstract class ImplementationWriter extends SelfInitializingWriter {
/**
* Context key for package.
......@@ -62,13 +61,12 @@ public abstract class ImplementationWriter extends VelocitySourceWriter {
String packageName, String interfaceName, String className,
@NonNull Fields fields, @NonNull Types types, FileObject generatedFile
) {
super();
super(generatedFile);
this.context.put(PACKAGE, packageName);
this.context.put(INTERFACE, interfaceName);
this.context.put(CLASS, className);
this.context.put(FIELDS, fields);
this.context.put(TYPES, types);
this.generatedFile = generatedFile;
}
protected ImplementationWriter() {
......
......@@ -13,7 +13,6 @@
package org.eclipse.ice.dev.annotations.processors;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import lombok.NonNull;
......@@ -22,7 +21,7 @@ import lombok.NonNull;
*
* @author Daniel Bluhm
*/
public abstract class InterfaceWriter extends VelocitySourceWriter {
public abstract class InterfaceWriter extends SelfInitializingWriter {
/**
* Context key for package.
......@@ -57,16 +56,16 @@ public abstract class InterfaceWriter extends VelocitySourceWriter {
String packageName, String interfaceName, @NonNull Fields fields,
@NonNull Types types, FileObject generatedFile
) {
super();
super(generatedFile);
context.put(PACKAGE, packageName);
context.put(INTERFACE, interfaceName);
context.put(FIELDS, fields);
this.generatedFile = generatedFile;
context.put(TYPES, types);
}
protected InterfaceWriter() {
public InterfaceWriter() {
// TODO Auto-generated constructor stub
super();
}
}
......@@ -21,7 +21,7 @@ import lombok.NonNull;
*
* @author Daniel Bluhm
*/
public abstract class PersistenceHandlerWriter extends VelocitySourceWriter {
public abstract class PersistenceHandlerWriter extends SelfInitializingWriter {
/**
* Context key for package.
......@@ -77,7 +77,7 @@ public abstract class PersistenceHandlerWriter extends VelocitySourceWriter {
*/
public PersistenceHandlerWriter(String packageName, String elementInterface, String className, String interfaceName,
String implementation, String collection, @NonNull Fields fields, @NonNull Types types, FileObject generatedFile) {
super();
super(generatedFile);
this.context.put(PACKAGE, packageName);
this.context.put(ELEMENT_INTERFACE, elementInterface);
this.context.put(CLASS, className);
......@@ -86,7 +86,6 @@ public abstract class PersistenceHandlerWriter extends VelocitySourceWriter {
this.context.put(IMPLEMENTATION, implementation);
this.context.put(FIELDS, fields);
this.context.put(TYPES, types);
this.generatedFile = generatedFile;
}
protected PersistenceHandlerWriter() {
......
package org.eclipse.ice.dev.annotations.processors;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import javax.tools.FileObject;
/**
* Temporary class for keeping things working while changing interfaces.
* @author Daniel Bluhm
*/
public abstract class SelfInitializingWriter extends VelocitySourceWriter {
protected FileObject generatedFile;
public SelfInitializingWriter(FileObject generatedFile) {
super();
this.generatedFile = generatedFile;
}
public SelfInitializingWriter() {
super();
}
public void write() throws IOException {
try (Writer writer = generatedFile.openWriter()) {
this.write(writer);
}
}
public abstract BiFunction<FileObject, Map, List<SelfInitializingWriter>> getInitializer();
}
......@@ -11,17 +11,29 @@
package org.eclipse.ice.dev.annotations.processors;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import javax.tools.FileObject;
import javax.annotation.processing.Filer;
import javax.tools.StandardLocation;
import lombok.Builder;
import lombok.NonNull;
/**
* Writer for TypeScript representation of DataElement.
* @author Daniel Bluhm
*/
public abstract class TypeScriptWriter extends VelocitySourceWriter {
public class TypeScriptWriter
extends VelocitySourceWriter
implements GeneratedFileWriter
{
/**
* Template used for this writer.
*/
private static final String TYPESCRIPT_TEMPLATE = "templates/TypeScript.vm";
/**
* Context key for name.
......@@ -56,6 +68,11 @@ public abstract class TypeScriptWriter extends VelocitySourceWriter {
Map.entry("java.util.Date", "Date")
);
/**
* Name of file generated.
*/
private String filename;
/**
* Create Writer.
* @param name of TypeScript class generated.
......@@ -63,9 +80,9 @@ public abstract class TypeScriptWriter extends VelocitySourceWriter {
* @param types of fields.
* @throws UnsupportedOperationException When any field is not supported.
*/
@Builder
public TypeScriptWriter(
String name, @NonNull Fields fields, @NonNull Types types,
FileObject generatedFile
String name, @NonNull Fields fields, @NonNull Types types
) {
super();
for (Field field : fields) {
......@@ -76,14 +93,39 @@ public abstract class TypeScriptWriter extends VelocitySourceWriter {
));
}
}
this.template = TYPESCRIPT_TEMPLATE;
this.filename = name;
this.context.put(NAME, name);
this.context.put(FIELDS, fields);
this.context.put(TYPES, types);
this.context.put(PRIMITIVE_MAP, primitiveMap);
this.generatedFile = generatedFile;
}
public TypeScriptWriter() {
// TODO Auto-generated constructor stub
@Override
public Writer openWriter(Filer filer) throws IOException {
return filer.createResource(
StandardLocation.SOURCE_OUTPUT,
"",
String.format("frontend/%s.ts", filename)
).openWriter();
}
/**
* Initialze from context map.
*
* TODO Move this logic elsewhere.
*
* @param context map of extracted properties.
* @return initialzed TypeScriptWriter
*/
public static TypeScriptWriter fromContext(
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();
}
}
\ No newline at end of file
......@@ -11,15 +11,9 @@
package org.eclipse.ice.dev.annotations.processors;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
......@@ -31,28 +25,15 @@ import org.apache.velocity.context.Context;
*
* @author Daniel Bluhm
*/
public abstract class VelocitySourceWriter {
public abstract class VelocitySourceWriter implements FileWriter {
protected String template;
protected Map<String, Object> context;
protected FileObject generatedFile;
public VelocitySourceWriter() {
this.context = new HashMap<>();
}
/**
* Opens a writer and then proceed to write a source file
*
* @param generatedFile
* @throws IOException
*/
public void write(FileObject generatedFile) throws IOException {
try (Writer writer = generatedFile.openWriter()) {
write(writer);
}
}
/**
* Write the Java Source file to the open writer.
*
......@@ -68,11 +49,4 @@ public abstract class VelocitySourceWriter {
// Write template from context.
Velocity.mergeTemplate(template, "UTF-8", velocityContext, writer);
}
public void write() throws IOException {