Commit e8d5db61 authored by Daniel Bluhm's avatar Daniel Bluhm

Restore typescript generation

Fix is kludgey -- needs to be revisited
Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent b815efb6
......@@ -13,7 +13,9 @@ package org.eclipse.ice.dev.annotations.processors;
import java.io.IOException;
import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
/**
* Abstract class for service classes that generate
......@@ -47,4 +49,17 @@ public abstract class AbstractWriterGenerator implements WriterGenerator {
public JavaFileObject createFileObjectForName(String name) throws IOException {
return processingEnv.getFiler().createSourceFile(name);
}
/**
* Generate resource file objec
*
* @param name of file
* @return JavaFileObject used to write generated class file to
* @throws IOException file writing
*/
public FileObject createResourceForName(String name) throws IOException {
return processingEnv.getFiler().createResource(
StandardLocation.SOURCE_OUTPUT, "", "frontend/" + name + ".ts"
);
}
}
......@@ -15,6 +15,7 @@ import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import lombok.Builder;
......@@ -46,7 +47,7 @@ public class DataElementImplementationWriter extends ImplementationWriter {
*/
@Builder
public DataElementImplementationWriter(String packageName, String interfaceName, String className, Fields fields,
Types types, JavaFileObject generatedFile) {
Types types, FileObject generatedFile) {
super(packageName, interfaceName, className, fields, types, generatedFile);
this.template = IMPL_TEMPLATE;
}
......@@ -64,7 +65,7 @@ public class DataElementImplementationWriter extends ImplementationWriter {
* DataElementImplementationWriter given a map and a JavaFileObject
*/
@Override
public BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getInitializer() {
public BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer() {
return (fileObject, context) ->
Arrays.asList(DataElementImplementationWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
......@@ -80,7 +81,7 @@ public class DataElementImplementationWriter extends ImplementationWriter {
*
* @return DataElementImplementationWriter init lambda
*/
public static BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
return new DataElementImplementationWriter().getInitializer();
}
......
......@@ -15,6 +15,7 @@ import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import lombok.Builder;
......@@ -46,7 +47,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter {
*/
@Builder
public DataElementInterfaceWriter(String packageName, String interfaceName, @NonNull Fields fields,
Types types, JavaFileObject generatedFile) {
Types types, FileObject generatedFile) {
super(packageName, interfaceName, fields, types, generatedFile);
this.template = TEMPLATE;
}
......@@ -64,7 +65,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter {
* DataElementInterfaceWriter given a map and a JavaFileObject
*/
@Override
public BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getInitializer() {
public BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer() {
return (fileObject, context) ->
Arrays.asList(DataElementInterfaceWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
......@@ -79,7 +80,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter {
*
* @return DataElementInterfaceWriter init lambda
*/
public static BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
return new DataElementInterfaceWriter().getInitializer();
}
......
......@@ -15,6 +15,7 @@ import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import lombok.Builder;
......@@ -51,7 +52,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite
@Builder
public DataElementPersistenceHandlerWriter(String packageName, String elementInterface, String className,
String interfaceName, String implementation, String collection, @NonNull Fields fields,
Types types, JavaFileObject generatedFile) {
Types types, FileObject generatedFile) {
super(packageName, elementInterface, className, interfaceName, implementation, collection, fields,
types, generatedFile);
this.template = PERSISTENCE_HANDLER_TEMPLATE;
......@@ -70,7 +71,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite
* DataElementPersistenceHandlerWriter given a map and a JavaFileObject
*/
@Override
public BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getInitializer() {
public BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer() {
return (fileObject, context) ->
Arrays.asList(DataElementPersistenceHandlerWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE))
......@@ -89,7 +90,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite
*
* @return DataElementPersistenceHandlerWriter init lambda
*/
public static BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
return new DataElementPersistenceHandlerWriter().getInitializer();
}
......
......@@ -156,33 +156,4 @@ public class DataElementProcessor extends AbstractProcessor {
private boolean valid(Element element) {
return element.getKind() == ElementKind.CLASS;
}
/**
* Write the TypeScript of DataElement annotated class to file.
* @param element
* @param fields
* @throws IOException
*/
private void writeTypeScript(
DataElementSpec element,
Fields fields
) throws IOException {
final FileObject generatedFile = processingEnv.getFiler()
.createResource(
StandardLocation.SOURCE_OUTPUT,
"",
"frontend/" + element.getName() + ".ts"
);
try (Writer writer = generatedFile.openWriter()) {
Fields trimmed = fields.getNonDefaultFields();
TypeScriptWriter.builder()
.name(element.getName())
.fields(trimmed)
.types(trimmed.getTypes())
.build()
.write(writer);
} catch (UnsupportedOperationException e) {
messager.printMessage(Diagnostic.Kind.NOTE, stackTraceToString(e));
}
}
}
/*******************************************************************************
* 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();
}
}
......@@ -19,6 +19,7 @@ import java.util.function.BiFunction;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import org.eclipse.ice.dev.annotations.Persisted;
......@@ -33,7 +34,8 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator implemen
/**
* Map of file name key to the respective file's writer initializer
*/
private Map<TemplateProperty, BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>>> writerInitializers = new HashMap<>();
private Map<TemplateProperty, BiFunction<FileObject, Map, List<VelocitySourceWriter>>> writerInitializers =
new HashMap<>();
/**
* Util instance for extracting specific data from Element objects
......@@ -42,10 +44,22 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator implemen
DataElementWriterGenerator(ProcessingEnvironment processingEnv) {
super(processingEnv);
writerInitializers.put(MetaTemplateProperty.QUALIFIED, DataElementInterfaceWriter.getContextInitializer());
writerInitializers.put(MetaTemplateProperty.QUALIFIEDIMPL, DataElementImplementationWriter.getContextInitializer());
writerInitializers.put(PersistenceHandlerTemplateProperty.QUALIFIED,
DataElementPersistenceHandlerWriter.getContextInitializer());
writerInitializers.put(
MetaTemplateProperty.QUALIFIED,
DataElementInterfaceWriter.getContextInitializer()
);
writerInitializers.put(
MetaTemplateProperty.QUALIFIEDIMPL,
DataElementImplementationWriter.getContextInitializer()
);
writerInitializers.put(
PersistenceHandlerTemplateProperty.QUALIFIED,
DataElementPersistenceHandlerWriter.getContextInitializer()
);
writerInitializers.put(
MetaTemplateProperty.TYPESCRIPT,
DataElementTypeScriptWriter.getContextInitializer()
);
}
/**
......@@ -59,18 +73,29 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator implemen
boolean hasAnnotation = specExtractionHelper.hasAnnotation(element, Persisted.class);
writerInitializers.keySet().stream()
.filter(key -> key != PersistenceHandlerTemplateProperty.QUALIFIED || hasAnnotation).forEach(key -> {
try {
.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);
JavaFileObject fileObject = createFileObjectForName(name);
List<VelocitySourceWriter> newWriters = writerInitializers.get(key).apply(fileObject,
classMetadata);
writers.addAll(newWriters);
} catch (IOException e) {
e.printStackTrace();
fileObject = createFileObjectForName(name);
}
});
List<VelocitySourceWriter> newWriters = writerInitializers
.get(key)
.apply(
fileObject,
classMetadata
);
writers.addAll(newWriters);
} catch (UnsupportedOperationException | IOException e) {
e.printStackTrace();
}
});
return writers;
}
......
......@@ -14,6 +14,7 @@ package org.eclipse.ice.dev.annotations.processors;
import lombok.NonNull;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
/**
......@@ -59,7 +60,7 @@ public abstract class ImplementationWriter extends VelocitySourceWriter {
*/
public ImplementationWriter(
String packageName, String interfaceName, String className,
@NonNull Fields fields, @NonNull Types types, JavaFileObject generatedFile
@NonNull Fields fields, @NonNull Types types, FileObject generatedFile
) {
super();
this.context.put(PACKAGE, packageName);
......
......@@ -12,6 +12,7 @@
package org.eclipse.ice.dev.annotations.processors;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import lombok.NonNull;
......@@ -54,7 +55,7 @@ public abstract class InterfaceWriter extends VelocitySourceWriter {
*/
public InterfaceWriter(
String packageName, String interfaceName, @NonNull Fields fields,
@NonNull Types types, JavaFileObject generatedFile
@NonNull Types types, FileObject generatedFile
) {
super();
context.put(PACKAGE, packageName);
......
......@@ -40,5 +40,6 @@ public enum MetaTemplateProperty implements TemplateProperty {
/**
* String of fully qualified interface name
*/
QUALIFIED;
QUALIFIED,
TYPESCRIPT;
}
......@@ -12,7 +12,7 @@
package org.eclipse.ice.dev.annotations.processors;
import javax.tools.JavaFileObject;
import javax.tools.FileObject;
import lombok.NonNull;
......@@ -76,7 +76,7 @@ public abstract class PersistenceHandlerWriter extends VelocitySourceWriter {
* @param generatedFile
*/
public PersistenceHandlerWriter(String packageName, String elementInterface, String className, String interfaceName,
String implementation, String collection, @NonNull Fields fields, @NonNull Types types, JavaFileObject generatedFile) {
String implementation, String collection, @NonNull Fields fields, @NonNull Types types, FileObject generatedFile) {
super();
this.context.put(PACKAGE, packageName);
this.context.put(ELEMENT_INTERFACE, elementInterface);
......
......@@ -13,19 +13,15 @@ package org.eclipse.ice.dev.annotations.processors;
import java.util.Map;
import lombok.Builder;
import javax.tools.FileObject;
import lombok.NonNull;
/**
* Writer for TypeScript representation of DataElement.
* @author Daniel Bluhm
*/
public class TypeScriptWriter extends VelocitySourceWriter {
/**
* Template used for this writer.
*/
private static final String TEMPLATE = "templates/TypeScript.vm";
public abstract class TypeScriptWriter extends VelocitySourceWriter {
/**
* Context key for name.
......@@ -67,10 +63,10 @@ public 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
) throws UnsupportedOperationException {
String name, @NonNull Fields fields, @NonNull Types types,
FileObject generatedFile
) {
super();
for (Field field : fields) {
if (!primitiveMap.containsKey(field.getType())) {
......@@ -80,10 +76,14 @@ public class TypeScriptWriter extends VelocitySourceWriter {
));
}
}
this.template = TEMPLATE;
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
}
}
\ No newline at end of file
......@@ -18,6 +18,7 @@ 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;
......@@ -34,7 +35,7 @@ public abstract class VelocitySourceWriter {
protected String template;
protected Map<String, Object> context;
protected JavaFileObject generatedFile;
protected FileObject generatedFile;
public VelocitySourceWriter() {
this.context = new HashMap<>();
......@@ -46,7 +47,7 @@ public abstract class VelocitySourceWriter {
* @param generatedFile
* @throws IOException
*/
public void write(JavaFileObject generatedFile) throws IOException {
public void write(FileObject generatedFile) throws IOException {
try (Writer writer = generatedFile.openWriter()) {
write(writer);
}
......@@ -72,6 +73,6 @@ public abstract class VelocitySourceWriter {
write(this.generatedFile);
}
public abstract BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getInitializer();
public abstract BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer();
}
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