Commit 1b0f1382 authored by Daniel Bluhm's avatar Daniel Bluhm

Simplify writers by taking metadata directly

Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent 4ddedec2
...@@ -13,7 +13,7 @@ import org.slf4j.LoggerFactory; ...@@ -13,7 +13,7 @@ import org.slf4j.LoggerFactory;
/** /**
* Abstract base class for AnnotationExtractors providing common methods to * Abstract base class for AnnotationExtractors providing common methods to
* all extractors. * all extractors.
* *
* @author Daniel Bluhm * @author Daniel Bluhm
* *
* @param <T> Type of data extracted from element. * @param <T> Type of data extracted from element.
...@@ -78,11 +78,11 @@ public abstract class AbstractAnnotationExtractor<T> implements AnnotationExtrac ...@@ -78,11 +78,11 @@ public abstract class AbstractAnnotationExtractor<T> implements AnnotationExtrac
/** /**
* Find and return annotation mirror of a given type if present. * Find and return annotation mirror of a given type if present.
* *
* When an annotation value is a Class, Annotation Mirrors must be used to * When an annotation value is a Class, Annotation Mirrors must be used to
* retrieve a TypeMirror to the class (using {@link * retrieve a TypeMirror to the class (using {@link
* Elements#getElementValuesWithDefaults(AnnotationMirror)}). * Elements#getElementValuesWithDefaults(AnnotationMirror)}).
* *
* If the annotation in question has no Class values, it is recommended to * If the annotation in question has no Class values, it is recommended to
* use the Annotation directly with {@link #getAnnotation(Element, Class)}. * use the Annotation directly with {@link #getAnnotation(Element, Class)}.
* *
......
...@@ -23,18 +23,54 @@ import lombok.Getter; ...@@ -23,18 +23,54 @@ import lombok.Getter;
@Getter @Getter
@Builder @Builder
public class DataElementMetadata { public class DataElementMetadata {
/**
* Suffix for Implementation name.
*/
private static final String IMPL_SUFFIX = "Implementation";
/** /**
* Base name of classes to be generated. * Base name of classes to be generated.
*/ */
protected String name; private String name;
/** /**
* Package of classes to be generated. * Package of classes to be generated.
*/ */
protected String packageName; private String packageName;
/** /**
* Collected fields of the DataElement. * Collected fields of the DataElement.
*/ */
protected Fields fields; private Fields fields;
/**
* Fully qualified name of this DataElement.
* @return fully qualified name.
*/
public String getFullyQualifiedName() {
String value = null;
if (packageName != null) {
value = String.format("%s.%s", packageName, name);
} else {
value = name;
}
return value;
}
/**
* Name of implementation for this DataElement.
* @return Implementation name.
*/
public String getImplementationName() {
return name + IMPL_SUFFIX;
}
/**
* Fully qualified name of this DataElement's implementation.
* @return fully qualified implementation name.
*/
public String getFullyQualifiedImplName() {
return getFullyQualifiedName() + IMPL_SUFFIX;
}
} }
\ No newline at end of file
...@@ -42,26 +42,10 @@ public class DataElementWriterGenerator implements WriterGenerator { ...@@ -42,26 +42,10 @@ public class DataElementWriterGenerator implements WriterGenerator {
@Override @Override
public List<GeneratedFileWriter> generate() { public List<GeneratedFileWriter> generate() {
List<GeneratedFileWriter> writers = new ArrayList<>(); List<GeneratedFileWriter> writers = new ArrayList<>();
Fields nonDefaults = data.getFields().getNonDefaultFields(); writers.add(new InterfaceWriter(data));
writers.add(InterfaceWriter.builder() writers.add(new ImplementationWriter(data));
.packageName(data.getPackageName())
.interfaceName(data.getName())
.fields(nonDefaults)
.types(nonDefaults.getTypes())
.build());
writers.add(ImplementationWriter.builder()
.packageName(data.getPackageName())
.interfaceName(data.getName())
.className(data.getName() + "Implementation")
.fields(data.getFields())
.types(data.getFields().getTypes())
.build());
try { try {
writers.add(TypeScriptWriter.builder() writers.add(new TypeScriptWriter(data));
.name(data.getName())
.fields(nonDefaults)
.types(nonDefaults.getTypes())
.build());
} catch (UnsupportedOperationException e) { } catch (UnsupportedOperationException e) {
logger.info("Failed to create typescript writer for element:", e); logger.info("Failed to create typescript writer for element:", e);
} }
......
...@@ -12,9 +12,6 @@ ...@@ -12,9 +12,6 @@
package org.eclipse.ice.dev.annotations.processors; package org.eclipse.ice.dev.annotations.processors;
import lombok.Builder;
import lombok.NonNull;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
...@@ -74,22 +71,14 @@ public class ImplementationWriter ...@@ -74,22 +71,14 @@ public class ImplementationWriter
* @param fields * @param fields
* @param generatedFile * @param generatedFile
*/ */
@Builder public ImplementationWriter(DataElementMetadata data) {
public ImplementationWriter(
String packageName, String interfaceName, String className,
@NonNull Fields fields, @NonNull Types types
) {
super(IMPL_TEMPLATE); super(IMPL_TEMPLATE);
if (packageName != null) { this.fqn = data.getFullyQualifiedImplName();
this.fqn = String.format("%s.%s", packageName, className); this.context.put(PACKAGE, data.getPackageName());
} else { this.context.put(INTERFACE, data.getName());
this.fqn = className; this.context.put(CLASS, data.getImplementationName());
} this.context.put(FIELDS, data.getFields());
this.context.put(PACKAGE, packageName); this.context.put(TYPES, data.getFields().getTypes());
this.context.put(INTERFACE, interfaceName);
this.context.put(CLASS, className);
this.context.put(FIELDS, fields);
this.context.put(TYPES, types);
} }
@Override @Override
......
...@@ -17,9 +17,6 @@ import java.io.Writer; ...@@ -17,9 +17,6 @@ import java.io.Writer;
import javax.annotation.processing.Filer; import javax.annotation.processing.Filer;
import lombok.Builder;
import lombok.NonNull;
/** /**
* Writer for DataElement Interfaces. * Writer for DataElement Interfaces.
* *
...@@ -72,21 +69,16 @@ public class InterfaceWriter ...@@ -72,21 +69,16 @@ public class InterfaceWriter
* @param fields * @param fields
* @param generatedFile * @param generatedFile
*/ */
@Builder
public InterfaceWriter( public InterfaceWriter(
String packageName, String interfaceName, @NonNull Fields fields, DataElementMetadata data
@NonNull Types types
) { ) {
super(TEMPLATE); super(TEMPLATE);
if (packageName != null) { this.fqn = data.getFullyQualifiedName();
this.fqn = String.format("%s.%s", packageName, interfaceName); Fields fields = data.getFields().getNonDefaultFields();
} else { context.put(PACKAGE, data.getPackageName());
this.fqn = interfaceName; context.put(INTERFACE, data.getName());
}
context.put(PACKAGE, packageName);
context.put(INTERFACE, interfaceName);
context.put(FIELDS, fields); context.put(FIELDS, fields);
context.put(TYPES, types); context.put(TYPES, fields.getTypes());
} }
@Override @Override
......
...@@ -17,9 +17,6 @@ import java.io.Writer; ...@@ -17,9 +17,6 @@ import java.io.Writer;
import javax.annotation.processing.Filer; import javax.annotation.processing.Filer;
import lombok.Builder;
import lombok.NonNull;
/** /**
* Writer for DataElement Persistence classes. * Writer for DataElement Persistence classes.
* *
...@@ -45,11 +42,6 @@ public class PersistenceHandlerWriter ...@@ -45,11 +42,6 @@ public class PersistenceHandlerWriter
*/ */
private static final String CLASS = "class"; private static final String CLASS = "class";
/**
* Context key for interface of PersistenceHandlers
*/
private static final String INTERFACE = "interface";
/** /**
* Context key for collection. * Context key for collection.
*/ */
...@@ -79,38 +71,27 @@ public class PersistenceHandlerWriter ...@@ -79,38 +71,27 @@ public class PersistenceHandlerWriter
* Fully qualified name of the class for file output. * Fully qualified name of the class for file output.
*/ */
private String fqn; private String fqn;
/** /**
* Constructor * Create instance of persistence handler writer from metadata.
* * @param dataElement DataElementMetadata
* @param packageName * @param persistence PersistenceMetadata
* @param elementInterface
* @param className
* @param interfaceName
* @param implementation
* @param collection
* @param fields
* @param generatedFile
*/ */
@Builder
public PersistenceHandlerWriter( public PersistenceHandlerWriter(
String packageName, String elementInterface, String className, String DataElementMetadata dataElement,
interfaceName, String implementation, String collection, @NonNull Fields PersistenceMetadata persistence
fields, @NonNull Types types
) { ) {
super(TEMPLATE); super(TEMPLATE);
if (packageName != null) { this.fqn = persistence.getHandlerName(
this.fqn = String.format("%s.%s", packageName, className); dataElement.getFullyQualifiedName()
} else { );
this.fqn = className; this.context.put(PACKAGE, dataElement.getPackageName());
} this.context.put(ELEMENT_INTERFACE, dataElement.getName());
this.context.put(PACKAGE, packageName); this.context.put(CLASS, persistence.getHandlerName(dataElement.getName()));
this.context.put(ELEMENT_INTERFACE, elementInterface); this.context.put(COLLECTION, persistence.getCollection());
this.context.put(CLASS, className); this.context.put(IMPLEMENTATION, dataElement.getImplementationName());
this.context.put(INTERFACE, interfaceName); this.context.put(FIELDS, dataElement.getFields());
this.context.put(COLLECTION, collection); this.context.put(TYPES, dataElement.getFields().getTypes());
this.context.put(IMPLEMENTATION, implementation);
this.context.put(FIELDS, fields);
this.context.put(TYPES, types);
} }
@Override @Override
......
...@@ -21,8 +21,22 @@ import lombok.Getter; ...@@ -21,8 +21,22 @@ import lombok.Getter;
@Getter @Getter
@Builder @Builder
public class PersistenceMetadata { public class PersistenceMetadata {
/**
* Suffix to PersistenceHandler class names.
*/
private static final String SUFFIX = "PersistenceHandler";
/** /**
* Collection name for persistence. * Collection name for persistence.
*/ */
private String collection; private String collection;
/**
* Get Persistence Handler class name using data element metadata.
* @param dataElementData DataElementMetadata
* @return persistence handler class name.
*/
public String getHandlerName(String dataElementName) {
return dataElementName + SUFFIX;
}
} }
...@@ -35,17 +35,7 @@ public class PersistenceWriterGenerator implements WriterGenerator { ...@@ -35,17 +35,7 @@ public class PersistenceWriterGenerator implements WriterGenerator {
@Override @Override
public List<GeneratedFileWriter> generate() { public List<GeneratedFileWriter> generate() {
List<GeneratedFileWriter> writers = List.of( List<GeneratedFileWriter> writers = List.of(
PersistenceHandlerWriter.builder() new PersistenceHandlerWriter(dataElement, persistence)
.packageName(dataElement.getPackageName())
.elementInterface(dataElement.getName())
.className(dataElement.getName() + "PersistenceHandler")
// TODO Just move interface name into template (it's static)
.interfaceName("IPersistenceHandler")
.implementation(dataElement.getName()+ "Implementation")
.collection(persistence.getCollection())
.fields(dataElement.getFields())
.types(dataElement.getFields().getTypes())
.build()
); );
return writers; return writers;
} }
......
...@@ -18,9 +18,6 @@ import java.util.Map; ...@@ -18,9 +18,6 @@ import java.util.Map;
import javax.annotation.processing.Filer; import javax.annotation.processing.Filer;
import javax.tools.StandardLocation; import javax.tools.StandardLocation;
import lombok.Builder;
import lombok.NonNull;
/** /**
* Writer for TypeScript representation of DataElement. * Writer for TypeScript representation of DataElement.
* @author Daniel Bluhm * @author Daniel Bluhm
...@@ -80,11 +77,11 @@ public class TypeScriptWriter ...@@ -80,11 +77,11 @@ public class TypeScriptWriter
* @param types of fields. * @param types of fields.
* @throws UnsupportedOperationException When any field is not supported. * @throws UnsupportedOperationException When any field is not supported.
*/ */
@Builder
public TypeScriptWriter( public TypeScriptWriter(
String name, @NonNull Fields fields, @NonNull Types types DataElementMetadata data
) { ) {
super(TYPESCRIPT_TEMPLATE); super(TYPESCRIPT_TEMPLATE);
Fields fields = data.getFields().getNonDefaultFields();
for (Field field : fields) { for (Field field : fields) {
if (!primitiveMap.containsKey(field.getType())) { if (!primitiveMap.containsKey(field.getType())) {
throw new UnsupportedOperationException(String.format( throw new UnsupportedOperationException(String.format(
...@@ -93,10 +90,10 @@ public class TypeScriptWriter ...@@ -93,10 +90,10 @@ public class TypeScriptWriter
)); ));
} }
} }
this.filename = name; this.filename = data.getName();
this.context.put(NAME, name); this.context.put(NAME, data.getName());
this.context.put(FIELDS, fields); this.context.put(FIELDS, fields);
this.context.put(TYPES, types); this.context.put(TYPES, fields.getTypes());
this.context.put(PRIMITIVE_MAP, primitiveMap); this.context.put(PRIMITIVE_MAP, primitiveMap);
} }
......
...@@ -66,7 +66,7 @@ public abstract class VelocitySourceWriter implements FileWriter { ...@@ -66,7 +66,7 @@ public abstract class VelocitySourceWriter implements FileWriter {
/** /**
* Template for writing. Should be filled by concrete classes. * Template for writing. Should be filled by concrete classes.
*/ */
protected String template; private String template;
/** /**
* Context for template. Should be filled by concrete classes. * Context for template. Should be filled by concrete classes.
......
...@@ -13,11 +13,11 @@ import com.mongodb.client.MongoDatabase; ...@@ -13,11 +13,11 @@ import com.mongodb.client.MongoDatabase;
#imports #imports
/** /**
* This is an implementation of $interface<$elementInterface> * This is an implementation of IPersistenceHandler<$elementInterface>
* that satisfies the dependencies of the {@code @Persisted} Annotation and was * that satisfies the dependencies of the {@code @Persisted} Annotation and was
* auto-generated by the ICE Framework. * auto-generated by the ICE Framework.
*/ */
public class $class implements $interface<$elementInterface> { public class $class implements IPersistenceHandler<$elementInterface> {
/** /**
* The name of the collection backing this PersistenceHandler. * The name of the collection backing this PersistenceHandler.
......
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