Commit 6c789a5d authored by Daniel Bluhm's avatar Daniel Bluhm
Browse files

Refine DefaultFields


Signed-off-by: Daniel Bluhm's avatarDaniel Bluhm <bluhmdj@ornl.gov>
parent bb4ecaaa
......@@ -26,6 +26,11 @@
<artifactId>auto-service</artifactId>
<version>1.0-rc6</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
......
......@@ -126,7 +126,7 @@ public class DataElementProcessor extends AbstractProcessor {
}
final Fields fields = new Fields();
fields.addAll(new DefaultFields());
fields.addAll(DefaultFields.get());
final List<? extends AnnotationMirror> mirrors = elem.getAnnotationMirrors();
try {
......
......@@ -22,6 +22,11 @@ import org.eclipse.ice.dev.annotations.processors.Fields;
*/
class DataFieldVisitor extends SimpleAnnotationValueVisitor8<Optional<UnexpectedValueError>, Fields> {
/**
* Check if the given type mirror represents a primitive type.
* @param t type to check
* @return true if primitive, false otherwise
*/
private static boolean isPrimitiveType(TypeMirror t) {
TypeKind kind = t.getKind();
return
......@@ -72,7 +77,7 @@ class DataFieldVisitor extends SimpleAnnotationValueVisitor8<Optional<Unexpected
if (!f.isBuilding()) {
f.begin();
}
f.setClassName(t.toString());
f.setType(t.toString());
f.setPrimitive(isPrimitiveType(t));
if (f.isComplete()) {
f.finish();
......
package org.eclipse.ice.dev.annotations.processors;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
public class DefaultFields extends Fields {
/**
* Helper class describing all the fields included in @DataElement by default.
*
*/
public class DefaultFields {
private static Field privateId = new Field("privateId", "UUID", "UUID.randomUUID()", false, false, false);
private static Field id = new Field("id", "long", "0L", false, true);
private static Field name = new Field("name", "String", "\"name\"", false, false);
private static Field description = new Field("description", "String", "\"description\"", false, false);
private static Field comment = new Field("comment", "String", "\"no comment\"", false, false);
private static Field context = new Field("context", "String", "\"default\"", false, false);
private static Field required = new Field("required", "boolean", "false", false, true);
private static Field secret = new Field("secret", "boolean", "false", false, true);
private static Field validator = new Field("validator", "JavascriptValidator<${class}>", null, true, false);
private static Field privateId = Field.builder()
.name("privateId")
.type(UUID.class)
.defaultValue(Field.raw("UUID.randomUUID()"))
.match(false)
.build();
public DefaultFields() {
this.fields = Arrays.asList(privateId, id, name, description, comment, context, required, secret, validator);
private static Field id = Field.builder()
.name("id")
.type(long.class)
.defaultValue(0L)
.primitive(true)
.build();
private static Field name = Field.builder()
.name("name")
.type(String.class)
.defaultValue("name")
.build();
private static Field description = Field.builder()
.name("description")
.type(String.class)
.defaultValue("description")
.build();
private static Field comment = Field.builder()
.name("comment")
.type(String.class)
.defaultValue("no comment")
.build();
private static Field context = Field.builder()
.name("context")
.type(String.class)
.defaultValue("default")
.build();
private static Field required = Field.builder()
.name("required")
.type(boolean.class)
.defaultValue(false)
.primitive(true)
.build();
private static Field secret = Field.builder()
.name("secret")
.type(boolean.class)
.defaultValue(false)
.primitive(true)
.build();
private static Field validator = Field.builder()
.name("validator")
.type(Field.raw("JavascriptValidator<$class>"))
.nullable(true)
.build();
public static List<Field> get() {
return Arrays.asList(
privateId, id, name, description, comment,
context, required, secret, validator
);
}
}
package org.eclipse.ice.dev.annotations.processors;
import lombok.Builder;
import lombok.Data;
import lombok.NonNull;
/**
* Container for Field information, taken from DataField Annotations, in
* simplified String form.
*/
@Data
@Builder
public class Field {
/**
* Name of the field.
*/
String name;
String className;
String defaultValue;
boolean nullable;
boolean primitive;
boolean match;
public Field(String name, String className, String defaultValue, boolean nullable, boolean primitive) {
this.name = name;
this.className = className;
this.defaultValue = defaultValue;
this.nullable = nullable;
this.primitive = primitive;
this.match = true;
}
public Field(String name, String className, String defaultValue, boolean nullable, boolean primitive, boolean match) {
this.name = name;
this.className = className;
this.defaultValue = defaultValue;
this.nullable = nullable;
this.primitive = primitive;
this.match = match;
}
public Field() {
this.nullable = false;
this.primitive = false;
this.match = true;
}
public boolean isMatch() {
return match;
}
public void setMatch(boolean match) {
this.match = match;
}
public boolean isPrimitive() {
return primitive;
}
public void setPrimitive(boolean primitive) {
this.primitive = primitive;
}
/**
* String representation of the field's type.
*/
String type;
public boolean isNullable() {
return nullable;
}
public void setNullable(boolean nullable) {
this.nullable = nullable;
}
public String getDefaultValue() {
return defaultValue;
}
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
public String getClassName() {
return className;
}
public String getName() {
return name;
}
/**
* The default value of this field.
*/
String defaultValue;
public void setClassName(final String className) {
this.className = className;
}
/**
* Whether or not this field can be null.
*/
boolean nullable;
public void setName(final String name) {
this.name = name;
}
/**
* Whether or not the type of this field is a primitive type.
*/
boolean primitive;
@Override
public String toString() {
return "Field (name=" + name + ", className=" + className + ")";
/**
* Whether or not this field should be included in the matches method checks.
*/
@Builder.Default boolean match = true;
public static class FieldBuilder {
public FieldBuilder defaultValue(long value) {
this.defaultValue = Long.toString(value) + "L";
return this;
}
public FieldBuilder defaultValue(String value) {
this.defaultValue = "\"" + value + "\"";
return this;
}
public FieldBuilder defaultValue(boolean value) {
this.defaultValue = Boolean.toString(value);
return this;
}
public FieldBuilder defaultValue(Raw value) {
this.defaultValue = value.getValue();
return this;
}
public FieldBuilder type(Class<?> type) {
this.type = type.getName().toString();
return this;
}
public FieldBuilder type(Raw type) {
this.type = type.getValue();
return this;
}
}
@Data public static class Raw {
@NonNull String value;
}
public static Raw raw(String value) {
return new Raw(value);
}
}
\ No newline at end of file
......@@ -7,13 +7,8 @@ import java.util.List;
* A simple container for fields discovered on a DataElement.
*/
class Fields {
/**
* Container for name and class name attributes of DataField in string
* representation.
*/
protected List<Field> fields;
protected Field building;
protected Field.FieldBuilder building;
public Fields() {
this.fields = new ArrayList<>();
......@@ -21,11 +16,11 @@ class Fields {
}
public void begin() {
this.building = new Field();
this.building = Field.builder();
}
public void finish() {
this.fields.add(this.building);
this.fields.add(this.building.build());
this.building = null;
}
......@@ -38,19 +33,20 @@ class Fields {
}
public boolean isComplete() {
return (this.building.getClassName() != null) && (this.building.getName() != null);
Field partial = this.building.build();
return (partial.getType() != null) && (partial.getName() != null);
}
public void setClassName(final String className) {
this.building.setClassName(className);
public void setType(final String type) {
this.building.type(Field.raw(type));
}
public void setName(final String name) {
this.building.setName(name);
this.building.name(name);
}
public void setPrimitive(boolean primitive) {
this.building.setPrimitive(primitive);
this.building.primitive(primitive);
}
@Override
......
......@@ -34,7 +34,7 @@ public class ${class} implements ${interface}, Serializable, IDataElement {
private static final Logger logger = LoggerFactory.getLogger(${class}.class);
#foreach($field in $fields)
#if(!${field.Nullable} && !${field.Primitive}) @NonNull #else #{end}protected #evaluate(${field.ClassName}) ${field.Name}#if(${field.DefaultValue}) = #evaluate(${field.DefaultValue})#end;
#if(!${field.Nullable} && !${field.Primitive}) @NonNull #else #{end}protected #evaluate(${field.Type}) ${field.Name}#if(${field.DefaultValue}) = #evaluate(${field.DefaultValue})#end;
#end
public $class($class other) throws Exception {
......@@ -84,6 +84,7 @@ public class ${class} implements ${interface}, Serializable, IDataElement {
#end ## if nullable
#end ## if match
#end ## foreach
return true;
}
......@@ -133,7 +134,7 @@ public class ${class} implements ${interface}, Serializable, IDataElement {
${field.Name} = null;
}
#else
${field.Name} = mapper.treeToValue(${field.Name}Node, ${field.ClassName}.class);
${field.Name} = mapper.treeToValue(${field.Name}Node, ${field.Type}.class);
#end
#end
......
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