Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Merge branch 'migrate-features' into merge-json
Browse files Browse the repository at this point in the history
  • Loading branch information
dbluhm committed May 28, 2020
2 parents 2f40ad2 + b64eba2 commit 4b81cf9
Show file tree
Hide file tree
Showing 14 changed files with 374 additions and 243 deletions.
6 changes: 0 additions & 6 deletions org.eclipse.ice.dev.annotations/.classpath
Expand Up @@ -37,17 +37,11 @@
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
Expand Down
3 changes: 1 addition & 2 deletions org.eclipse.ice.dev.annotations/pom.xml
Expand Up @@ -18,8 +18,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<release>11</release>
<annotationProcessorPaths>
<path>
<groupId>com.google.auto.service</groupId>
Expand Down
Expand Up @@ -12,4 +12,5 @@
public @interface DataField {
String fieldName();
Class<?> fieldType() default String.class;
String docString() default "";
}
@@ -1,9 +1,153 @@
package org.eclipse.ice.dev.annotations;

import java.util.UUID;

/**
* Marker interface for DataElements.
*/
public interface IDataElement {

/**
* Get the public identifier of the data element. This is a common id that may
* or may not be unique to this data element.
*
* @return the public id
*/
public long getId();

/**
* Set the public identifier of the data element. This is a common id that may
* or may not be unique to this data element.
*
* @param public_id the public id
* @throws Exception An exception is thrown if the value is null, which is
* unallowable.
*/
public void setId(final long public_id) throws Exception;

/**
* This operation returns the simple name of the data
*
* @return the name of the data, i.e. - "CORD-19" or "Steve"
*/
public String getName();

/**
* Set the simple name of the element
*
* @param elemName a simple name
* @throws Exception An exception is thrown if the value is null, which is
* unallowable.
*/
public void setName(final String name) throws Exception;

/**
* Get the simple description of the data, i.e. - "Machine readable data for
* COVID-19 research."
*
* @return the description of the data
*/
public String getDescription();

/**
* Set the description of the data
*
* @param desc the description
* @throws Exception An exception is thrown if the value is null, which is
* unallowable.
*/
public void setDescription(final String desc) throws Exception;

/**
* Get the comment/tag that annotates this data. This value is different than
* the description in that it provides commentary or a secondary designation in
* the form of an annotation for the data. For example, where the description
* should generally be useful, this value could simply be "2020Data" or any
* other tag of convenience used during processing.
*
* @return the comment
*/
public String getComment();

/**
* Return the comment or tag that annotates the data
*
* @param comment the comment to set
* @throws Exception An exception is thrown if the value is null, which is
* unallowable.
*/
public void setComment(final String comment) throws Exception;

/**
* Get the context of the data in its present state. For example, a single
* physical sample may be used across multiple experiments and the context in
* one case may be "x-ray scattering" whereas in another it could be "neutron
* scattering." Another example is when the same data is being used by two
* clients and this value changes from "ornl.gov" to "lbnl.gov" to indicate that
* a client should dynamically adapt without changing the data otherwise.
*
* @return the context
*/
public String getContext();

/**
* Return the context in which the data exists.
*
* @param context the context to set
* @throws Exception An exception is thrown if the value is null, which is
* unallowable.
*/
public void setContext(final String context) throws Exception;

/**
* True if the element is required by the client, false otherwise. This is only
* for client tracking and may make no sense for different clients.
*
* @return true if required, false if not
*/
public boolean isRequired();

/**
* True if the element is required by the client, false otherwise. This is only
* for client side tracking and may make no sense for different clients.
*
* @param required true if required, false if not
*/
public void setRequired(final boolean required);

/**
* True if the element is something that should be secret by the client, false
* otherwise. This is only for client tracking and may make no sense for
* different clients.
*
* @return true if secret, false if not
*/
public boolean isSecret();

/**
* True if the element is something that should be secret by the client, false
* otherwise. This is only for client tracking and may make no sense for
* different clients.
*
* @param secret true if the element should be treated as a secret, false
* otherwise
*/
public void setSecret(final boolean secret);

/**
* This operation returns the UUID of the data element. The UUID is a private
* unique identifier assigned to all data elements.
*
* @return the UUID for this element
*/
public UUID getUUID();

/**
* Format the DataElement as an output friendly string.
* @return String representation of Data
*/
public String toString();

/**
* This function checks deep equality of DataElements to see if all members are
* equal ("match") with the exception of fields with match set to false (such
Expand Down
Expand Up @@ -6,7 +6,9 @@
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -72,6 +74,21 @@ public static List<AnnotationValue> getAnnotationValuesForMirror(
.collect(Collectors.toList());
}

/**
* Get a list of annotation values from an annotation mirror.
* @param mirror the mirror from which to grab values.
* @return list of AnnotationValue
*/
public static Map<String, Object> getAnnotationValueMapForMirror(
final Elements elementUtils, final AnnotationMirror mirror
) {
return (Map<String, Object>) elementUtils.getElementValuesWithDefaults(mirror).entrySet().stream()
.collect(Collectors.toMap(
entry -> entry.getKey().getSimpleName().toString(),
entry -> entry.getValue().getValue()
));
}

/**
* Return stack trace as string.
* @param e subject exception
Expand Down Expand Up @@ -100,14 +117,11 @@ private static <T extends Throwable> void unwrap(final Optional<T> e) throws T {
protected Elements elementUtils;
protected DataFieldsVisitor fieldsVisitor;

protected DataFieldVisitor fieldVisitor;

@Override
public void init(final ProcessingEnvironment env) {
messager = env.getMessager();
elementUtils = env.getElementUtils();
fieldVisitor = new DataFieldVisitor();
fieldsVisitor = new DataFieldsVisitor(elementUtils, fieldVisitor);
fieldsVisitor = new DataFieldsVisitor(elementUtils);

// Set up Velocity using the Singleton approach; ClasspathResourceLoader allows
// us to load templates from src/main/resources
Expand All @@ -130,7 +144,7 @@ public boolean process(final Set<? extends TypeElement> annotations, final Round
return false;
}

final Fields fields = new Fields();
List<Field> fields = new ArrayList<Field>();
fields.addAll(DefaultFields.get());

try {
Expand Down Expand Up @@ -158,9 +172,9 @@ public boolean process(final Set<? extends TypeElement> annotations, final Round
* @return discovered fields
* @throws IOException
*/
private Fields collectFromDataFieldJson(Element element) throws IOException {
private List<Field> collectFromDataFieldJson(Element element) throws IOException {
final List<? extends AnnotationMirror> mirrors = element.getAnnotationMirrors();
Fields fields = new Fields();
List<Field> fields = new ArrayList<>();
// Iterate through AnnotationValues of AnnotationMirrors for DataFieldJson
for (
final AnnotationValue value : mirrors.stream()
Expand Down Expand Up @@ -197,40 +211,38 @@ private Fields collectFromDataFieldJson(Element element) throws IOException {
* @return discovered fields
* @throws UnexpectedValueError
*/
private Fields collectFromDataFields(Element element) throws UnexpectedValueError {
private List<Field> collectFromDataFields(Element element) throws UnexpectedValueError {
final List<? extends AnnotationMirror> mirrors = element.getAnnotationMirrors();
Fields fields = new Fields();
List<Field> fields = new ArrayList<>();
// Iterate over the AnnotationValues of AnnotationMirrors of type DataFields.
// DataFields present when more than one DataField annotation is used.
for (
final AnnotationValue value : mirrors.stream()
.filter(
mirror -> mirror.getAnnotationType().toString().equals(
DataFields.class.getCanonicalName()
.filter(
mirror -> mirror.getAnnotationType().toString().equals(
DataFields.class.getCanonicalName()
)
)
.map(mirror -> getAnnotationValuesForMirror(elementUtils, mirror))
.flatMap(List::stream) // Flatten List<List<AnnotationValue> to List<AnnotationValue>
.collect(Collectors.toList())
) {
.map(mirror -> getAnnotationValuesForMirror(elementUtils, mirror))
.flatMap(List::stream) // Flatten List<List<AnnotationValue> to List<AnnotationValue>
.collect(Collectors.toList())
) {
// Traditional for-loop used to allow raising an exception with unwrap if the
// field visitor returns an error result
unwrap(value.accept(fieldsVisitor, fields));
}
// Iterate over the AnnotationValues of AnnotationMirrors of type DataField.
// Only present when only one DataField annotation is used.
// Iterate over any DataField Annotations. Only present when only one DataField
// annotation is used.
for (
final AnnotationValue value : mirrors.stream()
.filter(
mirror -> mirror.getAnnotationType().toString().equals(
DataField.class.getCanonicalName()
final AnnotationMirror dataFieldMirror : mirrors.stream()
.filter(
mirror -> mirror.getAnnotationType().toString().equals(
DataField.class.getCanonicalName()
)
)
.map(mirror -> getAnnotationValuesForMirror(elementUtils, mirror))
.flatMap(List::stream)
.collect(Collectors.toList())
) {
unwrap(value.accept(fieldVisitor, fields));
.collect(Collectors.toList())
) {
unwrap(fieldsVisitor.visitAnnotation(dataFieldMirror, fields));
}
return fields;
}
Expand All @@ -242,7 +254,7 @@ private Fields collectFromDataFields(Element element) throws UnexpectedValueErro
* @param fields the fields extracted from DataField annotations on interface
* @throws IOException
*/
private void writeClass(final String interfaceName, final Fields fields) throws IOException {
private void writeClass(final String interfaceName, final List<Field> fields) throws IOException {
// Determine package, class name from annotated interface name
String packageName = null;
final int lastDot = interfaceName.lastIndexOf('.');
Expand All @@ -258,8 +270,7 @@ private void writeClass(final String interfaceName, final Fields fields) throws
context.put(ContextProperty.PACKAGE.key(), packageName);
context.put(ContextProperty.INTERFACE.key(), simpleName);
context.put(ContextProperty.CLASS.key(), generatedSimpleClassName);
// Hand over the list directly so template can #foreach on fields
context.put(ContextProperty.FIELDS.key(), fields.getFields());
context.put(ContextProperty.FIELDS.key(), fields);

// Write to file
final JavaFileObject generatedClassFile = processingEnv.getFiler().createSourceFile(generatedClassName);
Expand Down

0 comments on commit 4b81cf9

Please sign in to comment.