Commit e8d5db61 authored by Daniel Bluhm's avatar Daniel Bluhm
Browse files

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; ...@@ -13,7 +13,9 @@ package org.eclipse.ice.dev.annotations.processors;
import java.io.IOException; import java.io.IOException;
import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.FileObject;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
/** /**
* Abstract class for service classes that generate * Abstract class for service classes that generate
...@@ -47,4 +49,17 @@ public abstract class AbstractWriterGenerator implements WriterGenerator { ...@@ -47,4 +49,17 @@ public abstract class AbstractWriterGenerator implements WriterGenerator {
public JavaFileObject createFileObjectForName(String name) throws IOException { public JavaFileObject createFileObjectForName(String name) throws IOException {
return processingEnv.getFiler().createSourceFile(name); 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; ...@@ -15,6 +15,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import javax.tools.FileObject;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import lombok.Builder; import lombok.Builder;
...@@ -46,7 +47,7 @@ public class DataElementImplementationWriter extends ImplementationWriter { ...@@ -46,7 +47,7 @@ public class DataElementImplementationWriter extends ImplementationWriter {
*/ */
@Builder @Builder
public DataElementImplementationWriter(String packageName, String interfaceName, String className, Fields fields, 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); super(packageName, interfaceName, className, fields, types, generatedFile);
this.template = IMPL_TEMPLATE; this.template = IMPL_TEMPLATE;
} }
...@@ -64,7 +65,7 @@ public class DataElementImplementationWriter extends ImplementationWriter { ...@@ -64,7 +65,7 @@ public class DataElementImplementationWriter extends ImplementationWriter {
* DataElementImplementationWriter given a map and a JavaFileObject * DataElementImplementationWriter given a map and a JavaFileObject
*/ */
@Override @Override
public BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getInitializer() { public BiFunction<FileObject, Map, List<VelocitySourceWriter>> 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 +81,7 @@ public class DataElementImplementationWriter extends ImplementationWriter { ...@@ -80,7 +81,7 @@ public class DataElementImplementationWriter extends ImplementationWriter {
* *
* @return DataElementImplementationWriter init lambda * @return DataElementImplementationWriter init lambda
*/ */
public static BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getContextInitializer() { public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
return new DataElementImplementationWriter().getInitializer(); return new DataElementImplementationWriter().getInitializer();
} }
......
...@@ -15,6 +15,7 @@ import java.util.List; ...@@ -15,6 +15,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import javax.tools.FileObject;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import lombok.Builder; import lombok.Builder;
...@@ -46,7 +47,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter { ...@@ -46,7 +47,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter {
*/ */
@Builder @Builder
public DataElementInterfaceWriter(String packageName, String interfaceName, @NonNull Fields fields, public DataElementInterfaceWriter(String packageName, String interfaceName, @NonNull Fields fields,
Types types, JavaFileObject generatedFile) { Types types, FileObject generatedFile) {
super(packageName, interfaceName, fields, types, generatedFile); super(packageName, interfaceName, fields, types, generatedFile);
this.template = TEMPLATE; this.template = TEMPLATE;
} }
...@@ -64,7 +65,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter { ...@@ -64,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<JavaFileObject, Map, List<VelocitySourceWriter>> getInitializer() { public BiFunction<FileObject, Map, List<VelocitySourceWriter>> 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))
...@@ -79,7 +80,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter { ...@@ -79,7 +80,7 @@ public class DataElementInterfaceWriter extends InterfaceWriter {
* *
* @return DataElementInterfaceWriter init lambda * @return DataElementInterfaceWriter init lambda
*/ */
public static BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getContextInitializer() { public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
return new DataElementInterfaceWriter().getInitializer(); return new DataElementInterfaceWriter().getInitializer();
} }
......
...@@ -15,6 +15,7 @@ import java.util.List; ...@@ -15,6 +15,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import javax.tools.FileObject;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import lombok.Builder; import lombok.Builder;
...@@ -51,7 +52,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite ...@@ -51,7 +52,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite
@Builder @Builder
public DataElementPersistenceHandlerWriter(String packageName, String elementInterface, String className, public DataElementPersistenceHandlerWriter(String packageName, String elementInterface, String className,
String interfaceName, String implementation, String collection, @NonNull Fields fields, 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, super(packageName, elementInterface, className, interfaceName, implementation, collection, fields,
types, generatedFile); types, generatedFile);
this.template = PERSISTENCE_HANDLER_TEMPLATE; this.template = PERSISTENCE_HANDLER_TEMPLATE;
...@@ -70,7 +71,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite ...@@ -70,7 +71,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite
* DataElementPersistenceHandlerWriter given a map and a JavaFileObject * DataElementPersistenceHandlerWriter given a map and a JavaFileObject
*/ */
@Override @Override
public BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getInitializer() { public BiFunction<FileObject, Map, List<VelocitySourceWriter>> getInitializer() {
return (fileObject, context) -> return (fileObject, context) ->
Arrays.asList(DataElementPersistenceHandlerWriter.builder() Arrays.asList(DataElementPersistenceHandlerWriter.builder()
.packageName((String) context.get(MetaTemplateProperty.PACKAGE)) .packageName((String) context.get(MetaTemplateProperty.PACKAGE))
...@@ -89,7 +90,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite ...@@ -89,7 +90,7 @@ public class DataElementPersistenceHandlerWriter extends PersistenceHandlerWrite
* *
* @return DataElementPersistenceHandlerWriter init lambda * @return DataElementPersistenceHandlerWriter init lambda
*/ */
public static BiFunction<JavaFileObject, Map, List<VelocitySourceWriter>> getContextInitializer() { public static BiFunction<FileObject, Map, List<VelocitySourceWriter>> getContextInitializer() {
return new DataElementPersistenceHandlerWriter().getInitializer(); return new DataElementPersistenceHandlerWriter().getInitializer();
} }
......
...@@ -156,33 +156,4 @@ public class DataElementProcessor extends AbstractProcessor { ...@@ -156,33 +156,4 @@ public class DataElementProcessor extends AbstractProcessor {
private boolean valid(Element element) { private boolean valid(Element element) {
return element.getKind() == ElementKind.CLASS; 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; ...@@ -19,6 +19,7 @@ import java.util.function.BiFunction;
import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element; import javax.lang.model.element.Element;
import javax.tools.FileObject;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import org.eclipse.ice.dev.annotations.Persisted; import org.eclipse.ice.dev.annotations.Persisted;
...@@ -33,7 +34,8 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator implemen ...@@ -33,7 +34,8 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator implemen
/** /**
* 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<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 * Util instance for extracting specific data from Element objects
...@@ -42,10 +44,22 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator implemen ...@@ -42,10 +44,22 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator implemen
DataElementWriterGenerator(ProcessingEnvironment processingEnv) { DataElementWriterGenerator(ProcessingEnvironment processingEnv) {
super(processingEnv); super(processingEnv);
writerInitializers.put(MetaTemplateProperty.QUALIFIED, DataElementInterfaceWriter.getContextInitializer()); writerInitializers.put(
writerInitializers.put(MetaTemplateProperty.QUALIFIEDIMPL, DataElementImplementationWriter.getContextInitializer()); MetaTemplateProperty.QUALIFIED,
writerInitializers.put(PersistenceHandlerTemplateProperty.QUALIFIED, DataElementInterfaceWriter.getContextInitializer()
DataElementPersistenceHandlerWriter.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 ...@@ -59,18 +73,29 @@ public class DataElementWriterGenerator extends AbstractWriterGenerator implemen
boolean hasAnnotation = specExtractionHelper.hasAnnotation(element, Persisted.class); boolean hasAnnotation = specExtractionHelper.hasAnnotation(element, Persisted.class);
writerInitializers.keySet().stream() writerInitializers.keySet().stream()
.filter(key -> key != PersistenceHandlerTemplateProperty.QUALIFIED || hasAnnotation).forEach(key -> { .filter(key -> key != PersistenceHandlerTemplateProperty.QUALIFIED || hasAnnotation)
try { .forEach(key -> {
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);
JavaFileObject fileObject = createFileObjectForName(name); fileObject = createFileObjectForName(name);
List<VelocitySourceWriter> newWriters = writerInitializers.get(key).apply(fileObject,
classMetadata);
writers.addAll(newWriters);
} catch (IOException e) {
e.printStackTrace();
} }
}); List<VelocitySourceWriter> newWriters = writerInitializers
.get(key)
.apply(
fileObject,
classMetadata
);
writers.addAll(newWriters);
} catch (UnsupportedOperationException | IOException e) {
e.printStackTrace();
}
});
return writers; return writers;
} }
......
...@@ -14,6 +14,7 @@ package org.eclipse.ice.dev.annotations.processors; ...@@ -14,6 +14,7 @@ package org.eclipse.ice.dev.annotations.processors;
import lombok.NonNull; import lombok.NonNull;
import javax.tools.FileObject;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
/** /**
...@@ -59,7 +60,7 @@ public abstract class ImplementationWriter extends VelocitySourceWriter { ...@@ -59,7 +60,7 @@ public abstract class ImplementationWriter extends VelocitySourceWriter {
*/ */
public ImplementationWriter( public ImplementationWriter(
String packageName, String interfaceName, String className, String packageName, String interfaceName, String className,
@NonNull Fields fields, @NonNull Types types, JavaFileObject generatedFile @NonNull Fields fields, @NonNull Types types, FileObject generatedFile
) { ) {
super(); super();
this.context.put(PACKAGE, packageName); this.context.put(PACKAGE, packageName);
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
package org.eclipse.ice.dev.annotations.processors; package org.eclipse.ice.dev.annotations.processors;
import javax.tools.FileObject;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
import lombok.NonNull; import lombok.NonNull;
...@@ -54,7 +55,7 @@ public abstract class InterfaceWriter extends VelocitySourceWriter { ...@@ -54,7 +55,7 @@ public abstract class InterfaceWriter extends VelocitySourceWriter {
*/ */
public InterfaceWriter( public InterfaceWriter(
String packageName, String interfaceName, @NonNull Fields fields, String packageName, String interfaceName, @NonNull Fields fields,
@NonNull Types types, JavaFileObject generatedFile @NonNull Types types, FileObject generatedFile
) { ) {
super(); super();
context.put(PACKAGE, packageName); context.put(PACKAGE, packageName);
......
...@@ -40,5 +40,6 @@ public enum MetaTemplateProperty implements TemplateProperty { ...@@ -40,5 +40,6 @@ public enum MetaTemplateProperty implements TemplateProperty {
/** /**
* String of fully qualified interface name * String of fully qualified interface name
*/ */
QUALIFIED; QUALIFIED,
TYPESCRIPT;
} }
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
package org.eclipse.ice.dev.annotations.processors; package org.eclipse.ice.dev.annotations.processors;
import javax.tools.JavaFileObject; import javax.tools.FileObject;
import lombok.NonNull; import lombok.NonNull;
...@@ -76,7 +76,7 @@ public abstract class PersistenceHandlerWriter extends VelocitySourceWriter { ...@@ -76,7 +76,7 @@ public abstract class PersistenceHandlerWriter extends VelocitySourceWriter {
* @param generatedFile * @param generatedFile
*/ */
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, JavaFileObject generatedFile) { String implementation, String collection, @NonNull Fields fields, @NonNull Types types, FileObject generatedFile) {
super(); super();
this.context.put(PACKAGE, packageName); this.context.put(PACKAGE, packageName);
this.context.put(ELEMENT_INTERFACE, elementInterface); this.context.put(ELEMENT_INTERFACE, elementInterface);
......
...@@ -13,19 +13,15 @@ package org.eclipse.ice.dev.annotations.processors; ...@@ -13,19 +13,15 @@ package org.eclipse.ice.dev.annotations.processors;
import java.util.Map; import java.util.Map;
import lombok.Builder; import javax.tools.FileObject;
import lombok.NonNull; import lombok.NonNull;