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 { ...@@ -75,7 +75,7 @@ public class DataElementAnnotationExtractor {
* @return list of generated SourceWriters * @return list of generated SourceWriters
* @throws IOException due to {@link ICEAnnotationExtractionService#extract(AnnotationExtractionRequest)} * @throws IOException due to {@link ICEAnnotationExtractionService#extract(AnnotationExtractionRequest)}
*/ */
public List<VelocitySourceWriter> generateWriters( public List<SelfInitializingWriter> generateWriters(
AnnotationExtractionRequest request AnnotationExtractionRequest request
) throws IOException { ) throws IOException {
AnnotationExtractionResponse response = annotationExtractionService.extract(request); AnnotationExtractionResponse response = annotationExtractionService.extract(request);
......
...@@ -64,7 +64,7 @@ public class DataElementImplementationWriter extends ImplementationWriter { ...@@ -64,7 +64,7 @@ public class DataElementImplementationWriter extends ImplementationWriter {
* DataElementImplementationWriter given a map and a JavaFileObject * DataElementImplementationWriter given a map and a JavaFileObject
*/ */
@Override @Override
public BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer() { public BiFunction<FileObject, Map, List<SelfInitializingWriter>> getInitializer() {
return (fileObject, context) -> return (fileObject, context) ->
Arrays.asList(DataElementImplementationWriter.builder() Arrays.asList(DataElementImplementationWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE)) .packageName((String) context.get(MetaTemplateProperty.PACKAGE))
...@@ -80,7 +80,7 @@ public class DataElementImplementationWriter extends ImplementationWriter { ...@@ -80,7 +80,7 @@ public class DataElementImplementationWriter extends ImplementationWriter {
* *
* @return DataElementImplementationWriter init lambda * @return DataElementImplementationWriter init lambda
*/ */
public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() { public static BiFunction<FileObject, Map, List<SelfInitializingWriter>> getContextInitializer() {
return new DataElementImplementationWriter().getInitializer(); return new DataElementImplementationWriter().getInitializer();
} }
......
...@@ -65,7 +65,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter { ...@@ -65,7 +65,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter {
* DataElementInterfaceWriter given a map and a JavaFileObject * DataElementInterfaceWriter given a map and a JavaFileObject
*/ */
@Override @Override
public BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer() { public BiFunction<FileObject, Map, List<SelfInitializingWriter>> getInitializer() {
return (fileObject, context) -> return (fileObject, context) ->
Arrays.asList(DataElementInterfaceWriter.builder() Arrays.asList(DataElementInterfaceWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE)) .packageName((String) context.get(MetaTemplateProperty.PACKAGE))
...@@ -80,7 +80,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter { ...@@ -80,7 +80,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter {
* *
* @return DataElementInterfaceWriter init lambda * @return DataElementInterfaceWriter init lambda
*/ */
public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() { public static BiFunction<FileObject, Map, List<SelfInitializingWriter>> getContextInitializer() {
return new DataElementInterfaceWriter().getInitializer(); return new DataElementInterfaceWriter().getInitializer();
} }
} }
...@@ -75,7 +75,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite ...@@ -75,7 +75,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite
* DataElementPersistenceHandlerWriter given a map and a JavaFileObject * DataElementPersistenceHandlerWriter given a map and a JavaFileObject
*/ */
@Override @Override
public BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer() { public BiFunction<FileObject, Map, List<SelfInitializingWriter>> getInitializer() {
return (fileObject, context) -> Arrays.asList( return (fileObject, context) -> Arrays.asList(
DataElementPersistenceHandlerWriter.builder() DataElementPersistenceHandlerWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE)) .packageName((String) context.get(MetaTemplateProperty.PACKAGE))
...@@ -96,7 +96,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite ...@@ -96,7 +96,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite
* *
* @return DataElementPersistenceHandlerWriter init lambda * @return DataElementPersistenceHandlerWriter init lambda
*/ */
public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() { public static BiFunction<FileObject, Map, List<SelfInitializingWriter>> getContextInitializer() {
return new DataElementPersistenceHandlerWriter().getInitializer(); 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; ...@@ -29,12 +29,15 @@ import org.eclipse.ice.dev.annotations.Persisted;
* generate the interface, implementation, and persistence handler. * 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 * 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<>(); new HashMap<>();
/** /**
...@@ -56,20 +59,16 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator { ...@@ -56,20 +59,16 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator {
PersistenceHandlerTemplateProperty.QUALIFIED, PersistenceHandlerTemplateProperty.QUALIFIED,
DataElementPersistenceHandlerWriter.getContextInitializer() DataElementPersistenceHandlerWriter.getContextInitializer()
); );
writerInitializers.put(
MetaTemplateProperty.TYPESCRIPT,
DataElementTypeScriptWriter.getContextInitializer()
);
} }
/** /**
* DataElement specific method of class generation. Includes interfaces, * DataElement specific method of class generation. Includes interfaces,
* implementation, and possibly a persistence handler * implementation, and possibly a persistence handler
*/ */
public List<VelocitySourceWriter> generateWriters( public List<SelfInitializingWriter> generateWriters(
Element element, AnnotationExtractionResponse response Element element, AnnotationExtractionResponse response
) { ) {
List<VelocitySourceWriter> writers = new ArrayList<>(); List<SelfInitializingWriter> writers = new ArrayList<>();
Map<TemplateProperty, Object> classMetadata = response.getClassMetadata(); Map<TemplateProperty, Object> classMetadata = response.getClassMetadata();
boolean hasAnnotation = specExtractionHelper.hasAnnotation(element, Persisted.class); boolean hasAnnotation = specExtractionHelper.hasAnnotation(element, Persisted.class);
...@@ -77,16 +76,9 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator { ...@@ -77,16 +76,9 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator {
.filter(key -> key != PersistenceHandlerTemplateProperty.QUALIFIED || hasAnnotation) .filter(key -> key != PersistenceHandlerTemplateProperty.QUALIFIED || hasAnnotation)
.forEach(key -> { .forEach(key -> {
try { try {
FileObject fileObject = null;
if (key == MetaTemplateProperty.TYPESCRIPT) {
fileObject = createResourceForName(
(String) classMetadata.get(MetaTemplateProperty.CLASS)
);
} else {
String name = (String) classMetadata.get(key); String name = (String) classMetadata.get(key);
fileObject = createFileObjectForName(name); FileObject fileObject = createFileObjectForName(name);
} List<SelfInitializingWriter> newWriters = writerInitializers
List<VelocitySourceWriter> newWriters = writerInitializers
.get(key) .get(key)
.apply( .apply(
fileObject, fileObject,
...@@ -97,8 +89,13 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator { ...@@ -97,8 +89,13 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator {
e.printStackTrace(); e.printStackTrace();
} }
}); });
return writers; return writers;
} }
@Override
public List<GeneratedFileWriter> generate(AnnotationExtractionResponse response) {
return List.of(
TypeScriptWriter.fromContext(response.getClassMetadata())
);
}
} }
...@@ -10,12 +10,6 @@ import java.io.Writer; ...@@ -10,12 +10,6 @@ import java.io.Writer;
*/ */
public interface FileWriter { 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. * Write the file to the open writer.
* @param writer to which the file will be written. * @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; ...@@ -15,14 +15,13 @@ package org.eclipse.ice.dev.annotations.processors;
import lombok.NonNull; import lombok.NonNull;
import javax.tools.FileObject; import javax.tools.FileObject;
import javax.tools.JavaFileObject;
/** /**
* Writer for DataElement Implementation classes. * Writer for DataElement Implementation classes.
* *
* @author Daniel Bluhm * @author Daniel Bluhm
*/ */
public abstract class ImplementationWriter extends VelocitySourceWriter { public abstract class ImplementationWriter extends SelfInitializingWriter {
/** /**
* Context key for package. * Context key for package.
...@@ -62,13 +61,12 @@ public abstract class ImplementationWriter extends VelocitySourceWriter { ...@@ -62,13 +61,12 @@ public abstract class ImplementationWriter extends VelocitySourceWriter {
String packageName, String interfaceName, String className, String packageName, String interfaceName, String className,
@NonNull Fields fields, @NonNull Types types, FileObject generatedFile @NonNull Fields fields, @NonNull Types types, FileObject generatedFile
) { ) {
super(); super(generatedFile);
this.context.put(PACKAGE, packageName); this.context.put(PACKAGE, packageName);
this.context.put(INTERFACE, interfaceName); this.context.put(INTERFACE, interfaceName);
this.context.put(CLASS, className); this.context.put(CLASS, className);
this.context.put(FIELDS, fields); this.context.put(FIELDS, fields);
this.context.put(TYPES, types); this.context.put(TYPES, types);
this.generatedFile = generatedFile;
} }
protected ImplementationWriter() { protected ImplementationWriter() {
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
package org.eclipse.ice.dev.annotations.processors; package org.eclipse.ice.dev.annotations.processors;
import javax.tools.FileObject; import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import lombok.NonNull; import lombok.NonNull;
...@@ -22,7 +21,7 @@ import lombok.NonNull; ...@@ -22,7 +21,7 @@ import lombok.NonNull;
* *
* @author Daniel Bluhm * @author Daniel Bluhm
*/ */
public abstract class InterfaceWriter extends VelocitySourceWriter { public abstract class InterfaceWriter extends SelfInitializingWriter {
/** /**
* Context key for package. * Context key for package.
...@@ -57,16 +56,16 @@ public abstract class InterfaceWriter extends VelocitySourceWriter { ...@@ -57,16 +56,16 @@ public abstract class InterfaceWriter extends VelocitySourceWriter {
String packageName, String interfaceName, @NonNull Fields fields, String packageName, String interfaceName, @NonNull Fields fields,
@NonNull Types types, FileObject generatedFile @NonNull Types types, FileObject generatedFile
) { ) {
super(); super(generatedFile);
context.put(PACKAGE, packageName); context.put(PACKAGE, packageName);
context.put(INTERFACE, interfaceName); context.put(INTERFACE, interfaceName);
context.put(FIELDS, fields); context.put(FIELDS, fields);
this.generatedFile = generatedFile;
context.put(TYPES, types); context.put(TYPES, types);
} }
protected InterfaceWriter() {
public InterfaceWriter() {
// TODO Auto-generated constructor stub
super();
} }
} }
...@@ -21,7 +21,7 @@ import lombok.NonNull; ...@@ -21,7 +21,7 @@ import lombok.NonNull;
* *
* @author Daniel Bluhm * @author Daniel Bluhm
*/ */
public abstract class PersistenceHandlerWriter extends VelocitySourceWriter { public abstract class PersistenceHandlerWriter extends SelfInitializingWriter {
/** /**
* Context key for package. * Context key for package.
...@@ -77,7 +77,7 @@ public abstract class PersistenceHandlerWriter extends VelocitySourceWriter { ...@@ -77,7 +77,7 @@ public abstract class PersistenceHandlerWriter extends VelocitySourceWriter {
*/ */
public PersistenceHandlerWriter(String packageName, String elementInterface, String className, String interfaceName, public PersistenceHandlerWriter(String packageName, String elementInterface, String className, String interfaceName,
String implementation, String collection, @NonNull Fields fields, @NonNull Types types, FileObject generatedFile) { String implementation, String collection, @NonNull Fields fields, @NonNull Types types, FileObject generatedFile) {
super(); super(generatedFile);
this.context.put(PACKAGE, packageName); this.context.put(PACKAGE, packageName);
this.context.put(ELEMENT_INTERFACE, elementInterface); this.context.put(ELEMENT_INTERFACE, elementInterface);
this.context.put(CLASS, className); this.context.put(CLASS, className);
...@@ -86,7 +86,6 @@ public abstract class PersistenceHandlerWriter extends VelocitySourceWriter { ...@@ -86,7 +86,6 @@ public abstract class PersistenceHandlerWriter extends VelocitySourceWriter {
this.context.put(IMPLEMENTATION, implementation); this.context.put(IMPLEMENTATION, implementation);
this.context.put(FIELDS, fields); this.context.put(FIELDS, fields);
this.context.put(TYPES, types); this.context.put(TYPES, types);
this.generatedFile = generatedFile;
} }
protected PersistenceHandlerWriter() { 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 @@ ...@@ -11,17 +11,29 @@
package org.eclipse.ice.dev.annotations.processors; package org.eclipse.ice.dev.annotations.processors;
import java.io.IOException;
import java.io.Writer;
import java.util.Map; import java.util.Map;
import javax.tools.FileObject; import javax.annotation.processing.Filer;
import javax.tools.StandardLocation;
import lombok.Builder;
import lombok.NonNull; import lombok.NonNull;